如何激活/停用带有散景按钮小部件的 HoverTool?

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

我正在尝试 link 将 HoverTool 转换为散景中的切换按钮。我想生成一个独立的仪表板作为 html,所以我想使用一些自定义 javascript 来使用切换按钮切换 hovertool。

下面是一些示例代码:

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))

从 Bokeh 2.0.1 开始,不支持工具栏中的动态 adding/removing 工具。您可以这样 activate/deactivate 该工具:

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)

为了补充 bigreddot 的答案,您还可以添加

p.toolbar_location = None

它会完全删除工具栏,而不仅仅是 HoverTool 按钮。 HoverTool 仍然可以通过按钮切换。