reflex/reflex/components/plotly/plotly.py
HongyuHansonYao d9e718d7bd
Layout property not pushed through on rx.plotly (#3394)
* 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>
2024-05-31 11:48:43 -07:00

52 lines
1.2 KiB
Python

"""Component for displaying a plotly graph."""
from typing import Any, Dict, List
from reflex.components.component import NoSSRComponent
from reflex.vars import Var
try:
from plotly.graph_objects import Figure
except ImportError:
Figure = Any # type: ignore
class PlotlyLib(NoSSRComponent):
"""A component that wraps a plotly lib."""
library = "react-plotly.js@2.6.0"
lib_dependencies: List[str] = ["plotly.js@2.22.0"]
class Plotly(PlotlyLib):
"""Display a plotly graph."""
tag = "Plot"
is_default = True
# The figure to display. This can be a plotly figure or a plotly data json.
data: Var[Figure]
# The layout of the graph.
layout: Var[Dict]
# The config of the graph.
config: Var[Dict]
# 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