From b40eafdbb01df1762c862ae11a1836298d10b270 Mon Sep 17 00:00:00 2001 From: Nikhil Rao Date: Tue, 10 Jan 2023 18:13:22 -0800 Subject: [PATCH] Add var length operation (#245) --- pynecone/.templates/web/pcversion.txt | 2 +- pynecone/.templates/web/utils/state.js | 4 +--- pynecone/components/component.py | 10 +++++++++- pynecone/state.py | 8 ++++---- pynecone/var.py | 16 ++++++++++++++++ 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/pynecone/.templates/web/pcversion.txt b/pynecone/.templates/web/pcversion.txt index a34eaa5d0..6a36bb4db 100644 --- a/pynecone/.templates/web/pcversion.txt +++ b/pynecone/.templates/web/pcversion.txt @@ -1 +1 @@ -0.1.11 \ No newline at end of file +0.1.12 \ No newline at end of file diff --git a/pynecone/.templates/web/utils/state.js b/pynecone/.templates/web/utils/state.js index b81e5e674..0cb2f4fb2 100644 --- a/pynecone/.templates/web/utils/state.js +++ b/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)); } diff --git a/pynecone/components/component.py b/pynecone/components/component.py index 98bd77a2c..e2e6863b8 100644 --- a/pynecone/components/component.py +++ b/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: diff --git a/pynecone/state.py b/pynecone/state.py index 146c4a54d..d01dd0095 100644 --- a/pynecone/state.py +++ b/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. diff --git a/pynecone/var.py b/pynecone/var.py index 7ea673018..d9c83f31f 100644 --- a/pynecone/var.py +++ b/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.