Fix event handler lambda args (#106)

This commit is contained in:
Nikhil Rao 2022-12-14 23:43:30 -08:00 committed by GitHub
parent 2c3ef6e23f
commit a9afe819db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -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)

View File

@ -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: