Allow plotly figures as state vars (#440)

This commit is contained in:
lawrence-axb 2023-02-05 09:13:18 +09:00 committed by GitHub
parent dab7e5d2a1
commit d1ff7d481f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 4 deletions

View File

@ -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. Finally run `black` to format your code.
``` bash ``` bash
poetry run black pynecone poetry run black pynecone tests
``` ```
That's it you can now submit your pr. Thanks for contributing to Pynecone! That's it you can now submit your pr. Thanks for contributing to Pynecone!

View File

@ -1100,6 +1100,18 @@ def is_dataframe(value: Type) -> bool:
return value.__name__ == "DataFrame" 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: def is_valid_var_type(var: Type) -> bool:
"""Check if the given value is a valid prop type. """Check if the given value is a valid prop type.
@ -1109,7 +1121,7 @@ def is_valid_var_type(var: Type) -> bool:
Returns: Returns:
Whether the value is a valid prop type. 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: 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. # Fix the events created by the handler.
out = [] out = []
for e in events: for e in events:
# If it is already an event, don't modify it. # If it is already an event, don't modify it.
if isinstance(e, Event): if isinstance(e, Event):
name = e.name name = e.name

View File

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