-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathtest_px_hover.py
196 lines (179 loc) · 6.05 KB
/
test_px_hover.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import plotly.express as px
import narwhals.stable.v1 as nw
import numpy as np
import pandas as pd
import pytest
from collections import OrderedDict # an OrderedDict is needed for Python 2
def test_skip_hover(backend):
df = px.data.iris(return_type=backend)
fig = px.scatter(
df,
x="petal_length",
y="petal_width",
size="species_id",
hover_data={"petal_length": None, "petal_width": None},
)
assert fig.data[0].hovertemplate == "species_id=%{marker.size}<extra></extra>"
def test_hover_data_string_column(backend):
df = px.data.tips(return_type=backend)
fig = px.scatter(
df,
x="tip",
y="total_bill",
hover_data="sex",
)
assert "sex" in fig.data[0].hovertemplate
def test_composite_hover(backend):
df = px.data.tips(return_type=backend)
hover_dict = OrderedDict(
{"day": False, "time": False, "sex": True, "total_bill": ":.1f"}
)
fig = px.scatter(
df,
x="tip",
y="total_bill",
color="day",
facet_row="time",
hover_data=hover_dict,
)
for el in ["tip", "total_bill", "sex"]:
assert el in fig.data[0].hovertemplate
for el in ["day", "time"]:
assert el not in fig.data[0].hovertemplate
assert ":.1f" in fig.data[0].hovertemplate
def test_newdatain_hover_data():
hover_dicts = [
{"comment": ["a", "b", "c"]},
{"comment": (1.234, 45.3455, 5666.234)},
{"comment": [1.234, 45.3455, 5666.234]},
{"comment": np.array([1.234, 45.3455, 5666.234])},
{"comment": pd.Series([1.234, 45.3455, 5666.234])},
]
for hover_dict in hover_dicts:
fig = px.scatter(x=[1, 2, 3], y=[3, 4, 5], hover_data=hover_dict)
assert (
fig.data[0].hovertemplate
== "x=%{x}<br>y=%{y}<br>comment=%{customdata[0]}<extra></extra>"
)
fig = px.scatter(
x=[1, 2, 3], y=[3, 4, 5], hover_data={"comment": (True, ["a", "b", "c"])}
)
assert (
fig.data[0].hovertemplate
== "x=%{x}<br>y=%{y}<br>comment=%{customdata[0]}<extra></extra>"
)
hover_dicts = [
{"comment": (":.1f", (1.234, 45.3455, 5666.234))},
{"comment": (":.1f", [1.234, 45.3455, 5666.234])},
{"comment": (":.1f", np.array([1.234, 45.3455, 5666.234]))},
{"comment": (":.1f", pd.Series([1.234, 45.3455, 5666.234]))},
]
for hover_dict in hover_dicts:
fig = px.scatter(
x=[1, 2, 3],
y=[3, 4, 5],
hover_data=hover_dict,
)
assert (
fig.data[0].hovertemplate
== "x=%{x}<br>y=%{y}<br>comment=%{customdata[0]:.1f}<extra></extra>"
)
def test_formatted_hover_and_labels(backend):
df = px.data.tips(return_type=backend)
fig = px.scatter(
df,
x="tip",
y="total_bill",
hover_data={"total_bill": ":.1f"},
labels={"total_bill": "Total bill"},
)
assert ":.1f" in fig.data[0].hovertemplate
def test_fail_wrong_column():
# Testing for each of bare string, list, and basic dictionary
for hover_data_value in ["d", ["d"], {"d": True}]:
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data=hover_data_value,
)
assert (
"Value of 'hover_data_0' is not the name of a column in 'data_frame'."
in str(err_msg.value)
)
# Testing other dictionary possibilities below
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data={"d": ":.1f"},
)
assert (
"Value of 'hover_data_0' is not the name of a column in 'data_frame'."
in str(err_msg.value)
)
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data={"d": [3, 4, 5]}, # d is too long
)
assert (
"All arguments should have the same length. The length of hover_data key `d` is 3"
in str(err_msg.value)
)
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data={"d": (True, [3, 4, 5])}, # d is too long
)
assert (
"All arguments should have the same length. The length of hover_data key `d` is 3"
in str(err_msg.value)
)
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data={"c": [3, 4]},
)
assert (
"Ambiguous input: values for 'c' appear both in hover_data and data_frame"
in str(err_msg.value)
)
with pytest.raises(ValueError) as err_msg:
px.scatter(
{"a": [1, 2], "b": [3, 4], "c": [2, 1]},
x="a",
y="b",
hover_data={"c": (True, [3, 4])},
)
assert (
"Ambiguous input: values for 'c' appear both in hover_data and data_frame"
in str(err_msg.value)
)
def test_sunburst_hoverdict_color(backend):
df = px.data.gapminder(year=2007, return_type=backend)
fig = px.sunburst(
df,
path=["continent", "country"],
values="pop",
color="lifeExp",
hover_data={"pop": ":,"},
)
assert "color" in fig.data[0].hovertemplate
def test_date_in_hover(constructor):
df = nw.from_native(
constructor({"date": ["2015-04-04 19:31:30+0100"], "value": [3]})
).with_columns(date=nw.col("date").str.to_datetime(format="%Y-%m-%d %H:%M:%S%z"))
fig = px.scatter(df.to_native(), x="value", y="value", hover_data=["date"])
# Check that what gets displayed is the local datetime
assert nw.to_py_scalar(fig.data[0].customdata[0][0]) == nw.to_py_scalar(
df.item(row=0, column="date")
).replace(tzinfo=None)