Skip to content

Layout property not pushed through on rx.plotly #3394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading