test_plotly.py 854 B

12345678910111213141516171819202122232425262728293031323334
  1. import numpy as np
  2. import plotly.graph_objects as go
  3. import pytest
  4. from reflex.components.graphing.plotly import serialize_figure # type: ignore
  5. from reflex.utils.serializers import serialize
  6. @pytest.fixture
  7. def plotly_fig() -> go.Figure:
  8. """Get a plotly figure.
  9. Returns:
  10. A random plotly figure.
  11. """
  12. # Generate random data.
  13. data = np.random.randint(0, 10, size=(10, 4))
  14. trace = go.Scatter(
  15. x=list(range(len(data))), y=data[:, 0], mode="lines", name="Trace 1"
  16. )
  17. # Create a graph.
  18. return go.Figure(data=[trace])
  19. def test_serialize_plotly(plotly_fig: go.Figure):
  20. """Test that serializing a plotly figure works.
  21. Args:
  22. plotly_fig: The figure to serialize.
  23. """
  24. value = serialize(plotly_fig)
  25. assert isinstance(value, list)
  26. assert value == serialize_figure(plotly_fig)