plotly.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Component for displaying a plotly graph."""
  2. from typing import Dict
  3. from plotly.graph_objects import Figure
  4. from reflex.components.component import NoSSRComponent
  5. from reflex.components.tags import Tag
  6. from reflex.vars import Var
  7. class PlotlyLib(NoSSRComponent):
  8. """A component that wraps a plotly lib."""
  9. library = "react-plotly.js"
  10. class Plotly(PlotlyLib):
  11. """Display a plotly graph."""
  12. tag = "Plot"
  13. # The figure to display. This can be a plotly figure or a plotly data json.
  14. data: Var[Figure]
  15. # The layout of the graph.
  16. layout: Var[Dict]
  17. # The width of the graph.
  18. width: Var[str]
  19. # The height of the graph.
  20. height: Var[str]
  21. # If true, the graph will resize when the window is resized.
  22. use_resize_handler: Var[bool]
  23. def _render(self) -> Tag:
  24. if (
  25. isinstance(self.data, Figure)
  26. and self.layout is None
  27. and self.width is not None
  28. ):
  29. layout = Var.create({"width": self.width, "height": self.height})
  30. assert layout is not None
  31. self.layout = layout
  32. return super()._render()