瀏覽代碼

Fix custom component imports (#246)

Nikhil Rao 2 年之前
父節點
當前提交
7f0aa4f5c4

二進制
pynecone/.templates/web/bun.lockb


+ 3 - 2
pynecone/.templates/web/package.json

@@ -6,9 +6,9 @@
     "prod": "next start"
   },
   "dependencies": {
-    "@chakra-ui/system": "1.12.1",
     "@chakra-ui/icons": "1.1.7",
     "@chakra-ui/react": "1.8.8",
+    "@chakra-ui/system": "1.12.1",
     "@emotion/react": "^11.9.0",
     "@emotion/styled": "^11.8.1",
     "axios": "^0.27.2",
@@ -27,6 +27,7 @@
     "rehype-katex": "^6.0.2",
     "rehype-raw": "^6.1.1",
     "remark-gfm": "^3.0.1",
-    "remark-math": "^5.1.1"
+    "remark-math": "^5.1.1",
+    "victory": "^36.6.8"
   }
 }

+ 1 - 1
pynecone/__init__.py

@@ -7,6 +7,7 @@ from .app import App
 from .base import Base
 from .components import *
 from .components.component import custom_component as component
+from .components.graphing.victory import data
 from .config import Config
 from .constants import Env
 from .event import EventChain, console_log, redirect, window_alert
@@ -14,4 +15,3 @@ from .middleware import Middleware
 from .model import Model, session
 from .state import ComputedVar as var
 from .state import State
-from .components.graphing.victory import data

+ 29 - 8
pynecone/components/component.py

@@ -387,15 +387,24 @@ class Component(Base, ABC):
             self._get_imports(), *[child.get_imports() for child in self.children]
         )
 
-    def get_custom_components(self) -> Set[CustomComponent]:
+    def get_custom_components(
+        self, seen: Optional[Set[str]] = None
+    ) -> Set[CustomComponent]:
         """Get all the custom components used by the component.
 
+        Args:
+            seen: The tags of the components that have already been seen.
+
         Returns:
             The set of custom components.
         """
         custom_components = set()
+
+        # Store the seen components in a set to avoid infinite recursion.
+        if seen is None:
+            seen = set()
         for child in self.children:
-            custom_components |= child.get_custom_components()
+            custom_components |= child.get_custom_components(seen=seen)
         return custom_components
 
 
@@ -479,17 +488,29 @@ class CustomComponent(Component):
         """
         return set()
 
-    def get_custom_components(self) -> Set[CustomComponent]:
+    def get_custom_components(
+        self, seen: Optional[Set[str]] = None
+    ) -> Set[CustomComponent]:
         """Get all the custom components used by the component.
 
+        Args:
+            seen: The tags of the components that have already been seen.
+
         Returns:
             The set of custom components.
         """
-        return (
-            {self}
-            | super().get_custom_components()
-            # | self.get_component().get_custom_components()
-        )
+        assert self.tag is not None, "The tag must be set."
+
+        # Store the seen components in a set to avoid infinite recursion.
+        if seen is None:
+            seen = set()
+        custom_components = {self} | super().get_custom_components(seen=seen)
+
+        # Avoid adding the same component twice.
+        if self.tag not in seen:
+            seen.add(self.tag)
+            custom_components |= self.get_component().get_custom_components(seen=seen)
+        return custom_components
 
     def _render(self) -> Tag:
         """Define how to render the component in React.

+ 8 - 8
pynecone/components/graphing/__init__.py

@@ -2,19 +2,19 @@
 
 from .plotly import Plotly
 from .victory import (
-    Chart,
-    Line,
-    Scatter,
     Area,
     Bar,
-    Pie,
-    Polar,
-    Candlestick,
     BoxPlot,
-    Histogram,
-    ErrorBar,
+    Candlestick,
+    Chart,
     ChartGroup,
     ChartStack,
+    ErrorBar,
+    Histogram,
+    Line,
+    Pie,
+    Polar,
+    Scatter,
     Voronoi,
 )
 

+ 1 - 1
pynecone/components/graphing/victory.py

@@ -1,6 +1,6 @@
 """Victory graphing components."""
 
-from typing import Any, Dict, Union, List, Optional
+from typing import Any, Dict, List, Optional, Union
 
 from pynecone.components.component import Component
 from pynecone.components.tags import Tag

+ 2 - 2
pynecone/var.py

@@ -286,10 +286,10 @@ class Var(ABC):
             A var with the absolute value.
 
         Raises:
-            ValueError: If the var is not a list.
+            TypeError: 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}.")
+            raise TypeError(f"Cannot get length of non-list var {self}.")
         return BaseVar(
             name=f"{self.full_name}.length",
             type_=int,

+ 1 - 1
tests/components/graphing/test_victory_data.py

@@ -2,13 +2,13 @@ from typing import List, Set, Type
 
 import pytest
 
+from pynecone import data
 from pynecone.components.component import Component, CustomComponent, ImportDict
 from pynecone.components.layout.box import Box
 from pynecone.event import EVENT_TRIGGERS, EventHandler
 from pynecone.state import State
 from pynecone.style import Style
 from pynecone.var import Var
-from pynecone import data
 
 x_num = [1, 2, 3, 4, 5]
 x_str = ["Cats", "Dogs", "Birds", "Fish", "Reptiles"]