Преглед на файлове

Fix event handler lambda args (#106)

Nikhil Rao преди 2 години
родител
ревизия
a9afe819db
променени са 2 файла, в които са добавени 23 реда и са изтрити 4 реда
  1. 19 1
      pynecone/event.py
  2. 4 3
      pynecone/utils.py

+ 19 - 1
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)

+ 4 - 3
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: