Success!! @ea42gh 's code pointed me in the right direction (the use of CustomJSHover
for a specific field), but it was limiting in that it only had access to the value from the column (value
). Ultimately what I need it to be able to pass a Python variable (in my case, a dictionary) to the code
in CustomJSHover
.
This old bokeh thread gave me the missing piece: the use of arg
in CustomJSHover
.
I’ve modified @ea42gh 's code to work with what I needed. Note that I don’t know if the use of ColumnDataSource
is strictly needed, but I’m not proficient enough with bokeh to know if there’s a simpler alternative.
Thanks for your help, @ea42gh and @ahuang11 ! This is great. I’d rather not have to dive too much into JavaScript, but here it’s limited to just enough. I can live with that.
import pandas as pd
import holoviews as hv
hv.extension( "bokeh")
from bokeh.models import HoverTool, CustomJSHover, ColumnDataSource
data = pd.DataFrame( { 'x': [1.1, 3.2, 5.8 ], 'state': ['NY', 'NJ', 'PA'], 'stuff': ['a','b','c']} )
mydict = dict(a=1, b=2, c=3)
hover = HoverTool(
tooltips=[
("error", "$x"),
("state", "@state"),
("stuff", "@stuff{custom}")
],
formatters = {
"@stuff" : CustomJSHover(
args=dict(mappedDict=ColumnDataSource(dict(mydict=[mydict]))),
code="return mappedDict.data.mydict[0][value]"
)
}
)
hv.Scatter( data, kdims='x', vdims=['state', 'stuff']).opts(tools=[hover], size=10).redim.range(x=(0,7))