Change tooltips dynamically

I have this code:

tooltips = [('Pressure', '@y'), ('Force', '@x')]
fig = figure(tooltips=tooltips)

if show_line == 'yes':
    fig.line(x, y, color='red')

if show_vertices == 'yes':
    source = ColumnDataSource(data={'x': vertex_x, 'y': vertex_y, 'desc': ['name_1', 'name_2', ''name_3]})
    tooltips.insert(0, ('Point', '@desc'))
    fig.scatter(x='x', y='y', color='blue', source=source)

I have one figure with both line and points on that line. When the line is shown on the figure I need the hover tool only shows X an Y coordinates. When the vertices are added to the figure I need the hover tool to show a description for each point. What I thought I could do is to insert an extra element in tooltips list under the second IF condition. But this approach didn’t work. The figure in both conditions is showing only [(‘Pressure’, ‘@y’), (‘Force’, ‘@x’)] and not this [(‘Point’, ‘@desc’), (‘Pressure’, ‘@y’), (‘Force’, ‘@x’)]

Is it possible to solve it without CustomJS?



Make two HoverTools, one for each renderer (i.e. one for the points, one for the line). Assign the desired tooltips to each HoverTool, then add both to the figure:

from bokeh.plotting import figure, show
from bokeh.models import HoverTool, ColumnDataSource

d = {'Force':[1,2,3,4],'Pressure':[2,3,5,1],'Description':['Here','is','a','description']}

src = ColumnDataSource(d)

fig = figure()

pt_rend = fig.scatter(x='Force',y='Pressure',source=src)
l_rend = fig.line(x='Force',y='Pressure',source=src)

pt_hvr = HoverTool(renderers=[pt_rend],tooltips=[('Pressure','@Pressure')

l_hvr = HoverTool(renderers=[l_rend],tooltips=[('Pressure','@Pressure')
    pt_hvr, l_hvr)

Now I’m not sure how you are toggling the display between the the points renderer and the line renderer, but if you’re using the ‘visible’ property I believe the HoverTools won’t need their ‘active’ property also toggled → a visible=False renderer will never get its HoverTool triggered so I think you won’t need to do anything else.


Yes, this works.
Thank you very much

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.