Allowing hover_data and custom_data to pass string of column name instead of requiring a list. #4083
+36
−19
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Howdy y'all!
Quick background
I often get tripped up when using the
hover_data
argument since nearly all arguments can be a single string of a column name except it (andcustom_data
). To make matters a bit worse the error message is quite confusing, example below.This produces the following error
which is pretty misleading since I did pass 'day'!
This has been brought up in the following issues:
custom_data
#2177Instead of making the error message more clear, I think we should just allow a single string of a column name to be consistent with nearly all other args.
Source of problem
It appears the source of the problem is
hover_data
(andcustom_data
) are both inarray_attrables
and get cast as a list usinglist(arg)
. And ifarg
is a string like'my_col'
this turns it into['m', 'y', '_', 'c', 'o', 'l']
which is undesirable.Simple Solution
Thus my simple band-aid solution is checking if the field is in

["custom_data", "hover_data"]
and ifargs[field] in args["data_frame"].columns
then we instead turn it into a list simply doingargs[field] = [args[field]]
. This suffices as shown in the screenshot below.Other ideas?
I'm open to ideas if folks prefer we do it in a different way (particularly the hardcoded list might be good define explicitly up top with an informative name) but I wanted to get a simple enough solution proposed that works.
I've also update the docs in this PR to reflect that this change would allow you to pass a string to both
hover_data
andcustom_data
-- I don't know if it's preferable or not for these to be in the same PR.Happy to get any other feedback as well as this is my first attempt at an open source contribution. 😎
Thanks,
Luke Feilberg
Documentation PR
doc/README.md
filedoc-prod
branch OR it targets themaster
branchpx
example if at all possibleplotly.graph_objects as go
/plotly.express as px
/plotly.io as pio
df
fig = <something>
call is high up in each new/modified example (eitherpx.<something>
ormake_subplots
orgo.Figure
)fig.add_*
andfig.update_*
rather thango.Figure(data=..., layout=...)
in every new/modified examplefig.add_shape
andfig.update_xaxes
are used instead of bigfig.update_layout
calls in every new/modified examplefig.show()
is at the end of each new/modified exampleplotly.plot()
andplotly.iplot()
are not used in any new/modified exampleCode PR
plotly.graph_objects
, my modifications concern thecodegen
files and not generated files.modified existing tests.
new tutorial notebook (please see the doc checklist as well).