-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtest_plotly.py
More file actions
77 lines (58 loc) · 2.16 KB
/
Copy pathtest_plotly.py
File metadata and controls
77 lines (58 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import numpy as np
import plotly.graph_objects as go
import pytest
from reflex_base.utils.serializers import serialize, serialize_figure
import reflex as rx
@pytest.fixture
def plotly_fig() -> go.Figure:
"""Get a plotly figure.
Returns:
A random plotly figure.
"""
# Generate random data.
rng = np.random.default_rng()
data = rng.integers(0, 10, size=(10, 4))
trace = go.Scatter(
x=list(range(len(data))), y=data[:, 0], mode="lines", name="Trace 1"
)
# Create a graph.
return go.Figure(data=[trace])
def test_serialize_plotly(plotly_fig: go.Figure):
"""Test that serializing a plotly figure works.
Args:
plotly_fig: The figure to serialize.
"""
value = serialize(plotly_fig)
assert isinstance(value, dict)
assert value == serialize_figure(plotly_fig)
def test_plotly_config_option(plotly_fig: go.Figure):
"""Test that the plotly component can be created with a config option.
Args:
plotly_fig: The figure to display.
"""
# This tests just confirm that the component can be created with a config option.
_ = rx.plotly(data=plotly_fig, config={"showLink": True})
def test_plotly_locale_option_merges_into_config(plotly_fig: go.Figure):
"""Test that locale is passed through plot config.
Args:
plotly_fig: The figure to display.
"""
component = rx.plotly(data=plotly_fig, locale="de")
rendered = component._render()
config_var = rendered.props.get("config")
assert config_var is not None
assert "locale" not in rendered.props
assert "_rxGetPlotlyLocaleConfig" in str(config_var)
assert "de" in str(config_var)
def test_plotly_basic_locale_option_merges_into_config(plotly_fig: go.Figure):
"""Test that locale works for dynamic plotly dist variants too.
Args:
plotly_fig: The figure to display.
"""
component = rx.plotly.basic(data=plotly_fig, locale="fr")
rendered = component._render()
config_var = rendered.props.get("config")
assert config_var is not None
assert "locale" not in rendered.props
assert "_rxGetPlotlyLocaleConfig" in str(config_var)
assert "fr" in str(config_var)