Skip to content

Commit

Permalink
Layout property not pushed through on rx.plotly (#3394)
Browse files Browse the repository at this point in the history
* init fix

* Update reflex/components/plotly/plotly.py

Co-authored-by: Masen Furer <m_github@0x26.net>

* 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

* removed width height prop as they are no longer needed

* updated

* reverted some of the changes

* fixed unit tests

* regen pyi

---------

Co-authored-by: Hongyu Yao <hongyuyao@hongyus-mbp-3.lan>
Co-authored-by: Masen Furer <m_github@0x26.net>
Co-authored-by: Hongyu Yao <hongyuyao@Hongyus-MacBook-Pro-3.local>
  • Loading branch information
4 people authored May 31, 2024
1 parent 34bf250 commit d9e718d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
18 changes: 12 additions & 6 deletions reflex/components/plotly/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,17 @@ 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]

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"{{...{figure._var_name_unwrapped}}}")
)
else:
tag.add_props(data=figure["data"])
return tag
4 changes: 0 additions & 4 deletions reflex/components/plotly/plotly.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions reflex/utils/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/components/graphing/test_plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
4 changes: 3 additions & 1 deletion tests/utils/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]},
Expand Down

0 comments on commit d9e718d

Please sign in to comment.