From 1435b2f17a86e5bb96f0ad44a775058f2ed7e364 Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Tue, 28 May 2024 12:21:04 -0700 Subject: [PATCH 1/8] init fix --- reflex/components/plotly/plotly.py | 9 +++++++++ reflex/utils/serializers.py | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index 7a0dd835f6c..ece9e270ffb 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -43,3 +43,12 @@ class Plotly(PlotlyLib): # If true, the graph will resize when the window is resized. use_resize_handler: Var[bool] + + def _render(self): + tag = super()._render() + if self.layout is None: + tag.remove_props("data", "layout") + tag.special_props.add(Var.create_safe(f"{{...{self.data}}}")) + else: + tag.add_props(data=self.data.to(dict)["data"]) + return tag \ No newline at end of file diff --git a/reflex/utils/serializers.py b/reflex/utils/serializers.py index 1e1de7507b4..f3f9e635f92 100644 --- a/reflex/utils/serializers.py +++ b/reflex/utils/serializers.py @@ -255,7 +255,7 @@ def serialize_enum(en: Enum) -> str: en: The enum to serialize. Returns: - The serialized enum. + The serialized enum. """ return en.value @@ -313,7 +313,7 @@ def serialize_dataframe(df: DataFrame) -> dict: from plotly.io import to_json @serializer - def serialize_figure(figure: Figure) -> list: + def serialize_figure(figure: Figure) -> dict: """Serialize a plotly figure. Args: @@ -322,7 +322,7 @@ def serialize_figure(figure: Figure) -> list: Returns: The serialized figure. """ - return json.loads(str(to_json(figure)))["data"] + return json.loads(str(to_json(figure))) except ImportError: pass From 7351fb71f2e7df72eba96ff7d993568f0f957768 Mon Sep 17 00:00:00 2001 From: HongyuHansonYao <159659100+HongyuHansonYao@users.noreply.github.com> Date: Tue, 28 May 2024 14:14:04 -0700 Subject: [PATCH 2/8] Update reflex/components/plotly/plotly.py Co-authored-by: Masen Furer --- reflex/components/plotly/plotly.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index ece9e270ffb..c8e7bcf1e93 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -48,7 +48,7 @@ def _render(self): tag = super()._render() if self.layout is None: tag.remove_props("data", "layout") - tag.special_props.add(Var.create_safe(f"{{...{self.data}}}")) + tag.special_props.add(Var.create_safe(f"{{...{self.data._var_name_unwrapped}}}")) else: tag.add_props(data=self.data.to(dict)["data"]) return tag \ No newline at end of file From 8177acf4a5db39645710620ecebcfd3fa06333be Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Tue, 28 May 2024 15:20:54 -0700 Subject: [PATCH 3/8] plotly: treat `data` as a `dict`-type Var in _render this allows the data to be passed directly as a figure or from a state var --- reflex/components/plotly/plotly.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index c8e7bcf1e93..1aff2b1261c 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -46,9 +46,12 @@ class Plotly(PlotlyLib): def _render(self): tag = super()._render() + figure = self.data.to(dict) if self.layout is None: tag.remove_props("data", "layout") - tag.special_props.add(Var.create_safe(f"{{...{self.data._var_name_unwrapped}}}")) + tag.special_props.add( + Var.create_safe(f"{{...{figure._var_name_unwrapped}}}") + ) else: - tag.add_props(data=self.data.to(dict)["data"]) - return tag \ No newline at end of file + tag.add_props(data=figure["data"]) + return tag From d81dd72ed03f9bbbbc25fe91f75ea3d89c55051f Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Tue, 28 May 2024 16:02:45 -0700 Subject: [PATCH 4/8] removed width height prop as they are no longer needed --- reflex/components/plotly/plotly.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index 1aff2b1261c..5fe1cd4b06a 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -35,12 +35,6 @@ class Plotly(PlotlyLib): # The config of the graph. config: Var[Dict] - # The width of the graph. - width: Var[str] - - # The height of the graph. - height: Var[str] - # If true, the graph will resize when the window is resized. use_resize_handler: Var[bool] @@ -54,4 +48,4 @@ def _render(self): ) else: tag.add_props(data=figure["data"]) - return tag + return tag \ No newline at end of file From 65f7ce9575a3bf8a6e117777df302b21163c9f8b Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Fri, 31 May 2024 09:27:39 -0700 Subject: [PATCH 5/8] updated --- reflex/components/plotly/plotly.py | 6 +++--- reflex/state.py | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index 5fe1cd4b06a..f266dfb82d7 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -33,10 +33,10 @@ class Plotly(PlotlyLib): layout: Var[Dict] # The config of the graph. - config: Var[Dict] + # config: Var[Dict] # If true, the graph will resize when the window is resized. - use_resize_handler: Var[bool] + # use_resize_handler: Var[bool] def _render(self): tag = super()._render() @@ -48,4 +48,4 @@ def _render(self): ) else: tag.add_props(data=figure["data"]) - return tag \ No newline at end of file + return tag diff --git a/reflex/state.py b/reflex/state.py index e778946c039..66442bd7b55 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -390,6 +390,7 @@ def __init__( from reflex.utils.exceptions import ReflexRuntimeError if not _reflex_internal_init and not is_testing_env(): + breakpoint() raise ReflexRuntimeError( "State classes should not be instantiated directly in a Reflex app. " "See https://reflex.dev/docs/state/ for further information." From e7906603de8e930cf0cfd84fcbb34a41f8448b39 Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Fri, 31 May 2024 09:56:59 -0700 Subject: [PATCH 6/8] reverted some of the changes --- reflex/components/plotly/plotly.py | 4 ++-- reflex/state.py | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/reflex/components/plotly/plotly.py b/reflex/components/plotly/plotly.py index f266dfb82d7..3ee1977c356 100644 --- a/reflex/components/plotly/plotly.py +++ b/reflex/components/plotly/plotly.py @@ -33,10 +33,10 @@ class Plotly(PlotlyLib): layout: Var[Dict] # The config of the graph. - # config: Var[Dict] + config: Var[Dict] # If true, the graph will resize when the window is resized. - # use_resize_handler: Var[bool] + use_resize_handler: Var[bool] def _render(self): tag = super()._render() diff --git a/reflex/state.py b/reflex/state.py index 66442bd7b55..e778946c039 100644 --- a/reflex/state.py +++ b/reflex/state.py @@ -390,7 +390,6 @@ def __init__( from reflex.utils.exceptions import ReflexRuntimeError if not _reflex_internal_init and not is_testing_env(): - breakpoint() raise ReflexRuntimeError( "State classes should not be instantiated directly in a Reflex app. " "See https://reflex.dev/docs/state/ for further information." From 828ad2c47d31e6c674fe94c31ef15d66532a676a Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Fri, 31 May 2024 11:11:52 -0700 Subject: [PATCH 7/8] fixed unit tests --- tests/components/graphing/test_plotly.py | 2 +- tests/utils/test_format.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/components/graphing/test_plotly.py b/tests/components/graphing/test_plotly.py index 0e17789b573..69b046bea34 100644 --- a/tests/components/graphing/test_plotly.py +++ b/tests/components/graphing/test_plotly.py @@ -30,7 +30,7 @@ def test_serialize_plotly(plotly_fig: go.Figure): plotly_fig: The figure to serialize. """ value = serialize(plotly_fig) - assert isinstance(value, list) + assert isinstance(value, dict) assert value == serialize_figure(plotly_fig) diff --git a/tests/utils/test_format.py b/tests/utils/test_format.py index 19f3851759c..e9c79a4a2ca 100644 --- a/tests/utils/test_format.py +++ b/tests/utils/test_format.py @@ -3,12 +3,14 @@ import datetime from typing import Any, List +import plotly.graph_objects as go import pytest from reflex.components.tags.tag import Tag from reflex.event import EventChain, EventHandler, EventSpec, FrontendEvent from reflex.style import Style from reflex.utils import format +from reflex.utils.serializers import serialize_figure from reflex.vars import BaseVar, Var from tests.test_state import ( ChildState, @@ -661,7 +663,7 @@ def test_format_query_params(input, output): 2: {"prop1": 42, "prop2": "hello"}, }, "dt": "1989-11-09 18:53:00+01:00", - "fig": [], + "fig": serialize_figure(go.Figure()), "key": "", "map_key": "a", "mapping": {"a": [1, 2, 3], "b": [4, 5, 6]}, From d6f53f84e7d129b2f3f6abf71154e3c1b6491f6e Mon Sep 17 00:00:00 2001 From: Hongyu Yao Date: Fri, 31 May 2024 11:25:00 -0700 Subject: [PATCH 8/8] regen pyi --- reflex/components/plotly/plotly.pyi | 4 ---- 1 file changed, 4 deletions(-) diff --git a/reflex/components/plotly/plotly.pyi b/reflex/components/plotly/plotly.pyi index 7c8068e1e03..02288804f0b 100644 --- a/reflex/components/plotly/plotly.pyi +++ b/reflex/components/plotly/plotly.pyi @@ -101,8 +101,6 @@ class Plotly(PlotlyLib): data: Optional[Union[Var[Figure], Figure]] = None, # type: ignore layout: Optional[Union[Var[Dict], Dict]] = None, config: Optional[Union[Var[Dict], Dict]] = None, - width: Optional[Union[Var[str], str]] = None, - height: Optional[Union[Var[str], str]] = None, use_resize_handler: Optional[Union[Var[bool], bool]] = None, style: Optional[Style] = None, key: Optional[Any] = None, @@ -164,8 +162,6 @@ class Plotly(PlotlyLib): data: The figure to display. This can be a plotly figure or a plotly data json. layout: The layout of the graph. config: The config of the graph. - width: The width of the graph. - height: The height of the graph. use_resize_handler: If true, the graph will resize when the window is resized. style: The style of the component. key: A unique key for the component.