Skip to content

Commit 53c1829

Browse files
px.Constant
1 parent ece427d commit 53c1829

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

Diff for: packages/python/plotly/plotly/express/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
defaults,
5555
get_trendline_results,
5656
IdentityMap,
57+
Constant,
5758
)
5859

5960
from . import data, colors # noqa: F401
@@ -97,4 +98,5 @@
9798
"set_mapbox_access_token",
9899
"get_trendline_results",
99100
"IdentityMap",
101+
"Constant",
100102
]

Diff for: packages/python/plotly/plotly/express/_core.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ def copy(self):
5959
return self
6060

6161

62+
class Constant(object):
63+
def __init__(self, value, label=None):
64+
self.value = value
65+
self.label = label
66+
67+
6268
MAPBOX_TOKEN = None
6369

6470

@@ -937,6 +943,8 @@ def build_dataframe(args, attrables, array_attrables):
937943
else:
938944
df_output[df_input.columns] = df_input[df_input.columns]
939945

946+
constants = dict()
947+
940948
# Loop over possible arguments
941949
for field_name in attrables:
942950
# Massaging variables
@@ -968,8 +976,15 @@ def build_dataframe(args, attrables, array_attrables):
968976
"pandas MultiIndex is not supported by plotly express "
969977
"at the moment." % field
970978
)
979+
# ----------------- argument is a constant ----------------------
980+
if isinstance(argument, Constant):
981+
col_name = _check_name_not_reserved(
982+
str(argument.label) if argument.label is not None else field,
983+
reserved_names,
984+
)
985+
constants[col_name] = argument.value
971986
# ----------------- argument is a col name ----------------------
972-
if isinstance(argument, str) or isinstance(
987+
elif isinstance(argument, str) or isinstance(
973988
argument, int
974989
): # just a column name given as str or int
975990
if not df_provided:
@@ -1050,6 +1065,9 @@ def build_dataframe(args, attrables, array_attrables):
10501065
else:
10511066
args[field_name][i] = str(col_name)
10521067

1068+
for col_name in constants:
1069+
df_output[col_name] = constants[col_name]
1070+
10531071
args["data_frame"] = df_output
10541072
return args
10551073

Diff for: packages/python/plotly/plotly/tests/test_core/test_px/test_px_input.py

+32-2
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,36 @@ def test_identity_map():
335335
)
336336
assert fig.data[0].marker.color == "red"
337337
assert fig.data[1].marker.color == "blue"
338-
assert "color" not in fig.data[0].hovertemplate
339-
assert "symbol" in fig.data[0].hovertemplate
338+
assert "color=" not in fig.data[0].hovertemplate
339+
assert "symbol=" in fig.data[0].hovertemplate
340+
assert fig.layout.legend.title.text == "symbol"
341+
342+
343+
def test_constants():
344+
fig = px.scatter(x=px.Constant(1), y=[1, 2])
345+
assert fig.data[0].x[0] == 1
346+
assert fig.data[0].x[1] == 1
347+
assert "x=" in fig.data[0].hovertemplate
348+
349+
fig = px.scatter(x=px.Constant(1, label="time"), y=[1, 2])
350+
assert fig.data[0].x[0] == 1
351+
assert fig.data[0].x[1] == 1
352+
assert "x=" not in fig.data[0].hovertemplate
353+
assert "time=" in fig.data[0].hovertemplate
354+
355+
fig = px.scatter(
356+
x=[1, 2],
357+
y=[1, 2],
358+
symbol=["a", "b"],
359+
color=px.Constant("red", label="the_identity_label"),
360+
hover_data=[px.Constant("data", label="the_data")],
361+
color_discrete_map=px.IdentityMap(),
362+
)
363+
assert fig.data[0].marker.color == "red"
364+
assert fig.data[0].customdata[0][0] == "data"
365+
assert fig.data[1].marker.color == "red"
366+
assert "color=" not in fig.data[0].hovertemplate
367+
assert "the_identity_label=" not in fig.data[0].hovertemplate
368+
assert "symbol=" in fig.data[0].hovertemplate
369+
assert "the_data=" in fig.data[0].hovertemplate
340370
assert fig.layout.legend.title.text == "symbol"

0 commit comments

Comments
 (0)