From a9afe819db6981a6ba3df261ef1f4d9e76237dff Mon Sep 17 00:00:00 2001 From: Nikhil Rao Date: Wed, 14 Dec 2022 23:43:30 -0800 Subject: [PATCH] Fix event handler lambda args (#106) --- pynecone/event.py | 20 +++++++++++++++++++- pynecone/utils.py | 7 ++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/pynecone/event.py b/pynecone/event.py index eb483c1b2..08f5983ff 100644 --- a/pynecone/event.py +++ b/pynecone/event.py @@ -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) diff --git a/pynecone/utils.py b/pynecone/utils.py index ee0cc28ef..65c8f0afa 100644 --- a/pynecone/utils.py +++ b/pynecone/utils.py @@ -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: