plotly.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Component for displaying a plotly graph."""
  2. from typing import Dict
  3. from plotly.graph_objects import Figure
  4. from pynecone.components.component import Component
  5. from pynecone.components.tags import Tag
  6. from pynecone.var import Var
  7. class PlotlyLib(Component):
  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 _get_imports(self):
  24. return {}
  25. def _get_custom_code(self) -> str:
  26. return """import dynamic from 'next/dynamic'
  27. const Plot = dynamic(() => import('react-plotly.js'), { ssr: false });
  28. """
  29. def _render(self) -> Tag:
  30. if (
  31. isinstance(self.data, Figure)
  32. and self.layout is None
  33. and self.width is not None
  34. ):
  35. layout = Var.create({"width": self.width, "height": self.height})
  36. assert layout is not None
  37. self.layout = layout
  38. return super()._render()