简体   繁体   中英

How can I activate/ deactivate a HoverTool with a bokeh button widget?

I am trying to link a HoverTool to a Toggle Button in bokeh. I want to generate a stand alone dashboard as an html so I would like to use some cutom javascript to toggle the hovertool using a toggle button.

Here is some sample code:

from bokeh.plotting import ColumnDataSource, figure, output_file, show
from bokeh.layouts import row
from bokeh.models import Toggle, CustomJS

output_file("toolbar.html")

source = ColumnDataSource(
    data=dict(x=[1, 2, 3, 4, 5], y=[2, 5, 8, 2, 7], desc=["A", "b", "C", "d", "E"],)
)

TOOLTIPS = [
    ("index", "$index"),
    ("(x,y)", "($x, $y)"),
    ("desc", "@desc"),
]

p = figure(
    plot_width=400, plot_height=400, tooltips=TOOLTIPS, title="Mouse over the dots"
)

p.circle("x", "y", size=20, source=source)

# I would like to have some js code activate and deactivate the Hovertool
# and delete the option for the hovertool from the plots sidebar
button = Toggle(label="HoverTool", button_type="success")


show(row(p, button))

As of Bokeh 2.0.1, dynamically adding/removing tools from the toolbar is not supported. You can activate/deactivate the tool this way:

button = Toggle(label="HoverTool", button_type="success", active=True)

cb = CustomJS(args=dict(button=button, hover=p.hover[0]), code="""
hover.active = button.active
""")

button.js_on_click(cb)

To complement bigreddot's answer, you can also add

p.toolbar_location = None

It will completely remove the toolbar though, not just the HoverTool button. The HoverTool can still be toggle with the button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM