Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowing hover_data and custom_data to pass string of column name instead of requiring a list. #4083

Merged
merged 5 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/python/plotly/plotly/express/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,11 +1295,14 @@ def build_dataframe(args, constructor):
# make copies of all the fields via dict() and list()
for field in args:
if field in array_attrables and args[field] is not None:
args[field] = (
dict(args[field])
if isinstance(args[field], dict)
else list(args[field])
)
if isinstance(args[field], dict):
args[field] = dict(args[field])
elif field in ["custom_data", "hover_data"] and isinstance(
args[field], str
):
args[field] = [args[field]]
else:
args[field] = list(args[field])

# Cast data_frame argument to DataFrame (it could be a numpy array, dict etc.)
df_provided = args["data_frame"] is not None
Expand Down
8 changes: 4 additions & 4 deletions packages/python/plotly/plotly/express/_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@
"Values from this column or array_like appear in bold in the hover tooltip.",
],
hover_data=[
"list of str or int, or Series or array-like, or dict",
"Either a list of names of columns in `data_frame`, or pandas Series,",
"str, or list of str or int, or Series or array-like, or dict",
"Either a name or list of names of columns in `data_frame`, or pandas Series,",
"or array_like objects",
"or a dict with column names as keys, with values True (for default formatting)",
"False (in order to remove this column from hover information),",
Expand All @@ -211,8 +211,8 @@
"Values from these columns appear as extra data in the hover tooltip.",
],
custom_data=[
colref_list_type,
colref_list_desc,
"str, or list of str or int, or Series or array-like",
"Either name or list of names of columns in `data_frame`, or pandas Series, or array_like objects",
"Values from these columns are extra data, to be used in widgets or Dash callbacks for example. This data is not user-visible but is included in events emitted by the figure (lasso selection etc.)",
],
text=[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ def test_skip_hover():
assert fig.data[0].hovertemplate == "species_id=%{marker.size}<extra></extra>"


def test_hover_data_string_column():
df = px.data.tips()
fig = px.scatter(
df,
x="tip",
y="total_bill",
hover_data="sex",
)
assert "sex" in fig.data[0].hovertemplate


def test_composite_hover():
df = px.data.tips()
hover_dict = OrderedDict(
Expand Down Expand Up @@ -89,17 +100,20 @@ def test_formatted_hover_and_labels():


def test_fail_wrong_column():
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},
# 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)
)
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]},
Expand Down