Explorar o código

Add var length operation (#245)

Nikhil Rao %!s(int64=2) %!d(string=hai) anos
pai
achega
b40eafdbb0

+ 1 - 1
pynecone/.templates/web/pcversion.txt

@@ -1 +1 @@
-0.1.11
+0.1.12

+ 1 - 3
pynecone/.templates/web/utils/state.js

@@ -70,9 +70,6 @@ export const applyDelta = (state, delta) => {
  */
 export const applyEvent = async (event, router, socket) => {
   // Handle special events
-
-  event.router_data = (({ pathname, query }) => ({ pathname, query }))(router);
-
   if (event.name == "_redirect") {
     router.push(event.payload.path);
     return;
@@ -90,6 +87,7 @@ export const applyEvent = async (event, router, socket) => {
 
   // Send the event to the server.
   event.token = getToken();
+  event.router_data = (({ pathname, query }) => ({ pathname, query }))(router);
   if (socket) {
     socket.send(JSON.stringify(event));
   }

+ 9 - 1
pynecone/components/component.py

@@ -439,6 +439,14 @@ class CustomComponent(Component):
             type_ = props[key]
             if utils._issubclass(type_, EventChain):
                 value = self._create_event_chain(key, value)
+                self.props[utils.to_camel_case(key)] = value
+                continue
+            type_ = utils.get_args(type_)[0]
+            if utils._issubclass(type_, Base):
+                try:
+                    value = BaseVar(name=value.json(), type_=type_, is_local=True)
+                except:
+                    value = Var.create(value)
             else:
                 value = Var.create(value, is_string=type(value) is str)
             self.props[utils.to_camel_case(key)] = value
@@ -480,7 +488,7 @@ class CustomComponent(Component):
         return (
             {self}
             | super().get_custom_components()
-            | self.get_component().get_custom_components()
+            # | self.get_component().get_custom_components()
         )
 
     def _render(self) -> Tag:

+ 4 - 4
pynecone/state.py

@@ -261,15 +261,15 @@ class State(Base, ABC):
             field.required = False
             field.default = default_value
 
-    def get_current_page(cls) -> str:
+    def get_current_page(self) -> str:
         """Obtain the path of current page from the router data.
 
         Returns:
             The current page.
         """
-        return cls.router_data.get("pathname", "")
+        return self.router_data.get("pathname", "")
 
-    def get_query_params(cls) -> Dict[str, str]:
+    def get_query_params(self) -> Dict[str, str]:
         """Obtain the query parameters for the queried page.
 
         The query object contains both the URI parameters and the GET parameters.
@@ -277,7 +277,7 @@ class State(Base, ABC):
         Returns:
             The dict of query parameters.
         """
-        return cls.router_data.get("query", {})
+        return self.router_data.get("query", {})
 
     def __getattribute__(self, name: str) -> Any:
         """Get the state var.

+ 16 - 0
pynecone/var.py

@@ -279,6 +279,22 @@ class Var(ABC):
         """
         return self.operation(fn="Math.abs")
 
+    def length(self) -> Var:
+        """Get the length of a list var.
+
+        Returns:
+            A var with the absolute value.
+
+        Raises:
+            ValueError: If the var is not a list.
+        """
+        if not utils._issubclass(self.type_, List):
+            raise ValueError(f"Cannot get length of non-list var {self}.")
+        return BaseVar(
+            name=f"{self.full_name}.length",
+            type_=int,
+        )
+
     def __eq__(self, other: Var) -> Var:
         """Perform an equality comparison.