瀏覽代碼

Allow plotly figures as state vars (#440)

lawrence-axb 2 年之前
父節點
當前提交
d1ff7d481f
共有 3 個文件被更改,包括 17 次插入4 次删除
  1. 1 1
      CONTRIBUTING.md
  2. 13 2
      pynecone/utils.py
  3. 3 1
      tests/test_state.py

+ 1 - 1
CONTRIBUTING.md

@@ -93,6 +93,6 @@ find pynecone tests -name "*.py" -not -path pynecone/pc.py | xargs poetry run da
 ```
 Finally run `black` to format your code.
 ``` bash
-poetry run black pynecone
+poetry run black pynecone tests
 ```
 That's it you can now submit your pr. Thanks for contributing to Pynecone!

+ 13 - 2
pynecone/utils.py

@@ -1100,6 +1100,18 @@ def is_dataframe(value: Type) -> bool:
     return value.__name__ == "DataFrame"
 
 
+def is_figure(value: Type) -> bool:
+    """Check if the given value is a figure.
+
+    Args:
+        value: The value to check.
+
+    Returns:
+        Whether the value is a figure.
+    """
+    return value.__name__ == "Figure"
+
+
 def is_valid_var_type(var: Type) -> bool:
     """Check if the given value is a valid prop type.
 
@@ -1109,7 +1121,7 @@ def is_valid_var_type(var: Type) -> bool:
     Returns:
         Whether the value is a valid prop type.
     """
-    return _issubclass(var, StateVar) or is_dataframe(var)
+    return _issubclass(var, StateVar) or is_dataframe(var) or is_figure(var)
 
 
 def format_state(value: Any) -> Dict:
@@ -1279,7 +1291,6 @@ def fix_events(events: Optional[List[Event]], token: str) -> List[Event]:
     # Fix the events created by the handler.
     out = []
     for e in events:
-
         # If it is already an event, don't modify it.
         if isinstance(e, Event):
             name = e.name

+ 3 - 1
tests/test_state.py

@@ -8,6 +8,7 @@ from pynecone.constants import RouteVar
 from pynecone.event import Event
 from pynecone.state import State
 from pynecone.var import BaseVar, ComputedVar
+from plotly.graph_objects import Figure
 
 
 class Object(Base):
@@ -27,6 +28,7 @@ class TestState(State):
     mapping: Dict[str, List[int]] = {"a": [1, 2, 3], "b": [4, 5, 6]}
     obj: Object = Object()
     complex: Dict[int, Object] = {1: Object(), 2: Object()}
+    fig: Figure = Figure()
 
     @ComputedVar
     def sum(self) -> float:
@@ -195,6 +197,7 @@ def test_class_vars(test_state):
         "complex",
         "sum",
         "upper",
+        "fig",
     }
 
 
@@ -622,7 +625,6 @@ def test_get_token(test_state):
 
 
 def test_get_current_page(test_state):
-
     assert test_state.get_current_page() == ""
 
     route = "mypage/subpage"