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.
``` bash
poetry run black pynecone
poetry run black pynecone tests
```
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"
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

View File

@ -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"