Fix event handler lambda args (#106)
This commit is contained in:
parent
2c3ef6e23f
commit
a9afe819db
@ -2,6 +2,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import json
|
||||
from typing import Any, Callable, Dict, List, Set, Tuple
|
||||
|
||||
from pynecone.base import Base
|
||||
@ -43,12 +44,29 @@ class EventHandler(Base):
|
||||
|
||||
Returns:
|
||||
The event spec, containing both the function and args.
|
||||
|
||||
Raises:
|
||||
TypeError: If the arguments are invalid.
|
||||
"""
|
||||
# Get the function args.
|
||||
fn_args = inspect.getfullargspec(self.fn).args[1:]
|
||||
|
||||
# Construct the payload.
|
||||
payload = tuple(zip(fn_args, [Var.create(arg).full_name for arg in args])) # type: ignore
|
||||
values = []
|
||||
for arg in args:
|
||||
# If it is a Var, add the full name.
|
||||
if isinstance(arg, Var):
|
||||
values.append(arg.full_name)
|
||||
continue
|
||||
|
||||
# Otherwise, convert to JSON.
|
||||
try:
|
||||
values.append(json.dumps(arg))
|
||||
except TypeError:
|
||||
raise TypeError(
|
||||
f"Arguments to event handlers must be Vars or JSON-serializable. Got {arg} of type {type(arg)}."
|
||||
)
|
||||
payload = tuple(zip(fn_args, values))
|
||||
|
||||
# Return the event spec.
|
||||
return EventSpec(handler=self, args=payload)
|
||||
|
@ -378,9 +378,10 @@ def install_frontend_packages():
|
||||
|
||||
# Install the app packages.
|
||||
packages = get_config().frontend_packages
|
||||
subprocess.run(
|
||||
[get_bun_path(), "add", *packages], cwd=constants.WEB_DIR, stdout=PIPE
|
||||
)
|
||||
if len(packages) > 0:
|
||||
subprocess.run(
|
||||
[get_bun_path(), "add", *packages], cwd=constants.WEB_DIR, stdout=PIPE
|
||||
)
|
||||
|
||||
|
||||
def is_initialized() -> bool:
|
||||
|
Loading…
Reference in New Issue
Block a user