test_plotly.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import numpy as np
  2. import plotly.graph_objects as go
  3. import pytest
  4. import reflex as rx
  5. from reflex.utils.serializers import serialize, serialize_figure
  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, dict)
  26. assert value == serialize_figure(plotly_fig)
  27. def test_plotly_config_option(plotly_fig: go.Figure):
  28. """Test that the plotly component can be created with a config option.
  29. Args:
  30. plotly_fig: The figure to display.
  31. """
  32. # This tests just confirm that the component can be created with a config option.
  33. _ = rx.plotly(data=plotly_fig, config={"showLink": True})