如何使用散焦按钮小部件激活/停用悬停工具



我正在尝试将悬停工具链接到bokeh中的切换按钮。我想将一个独立的面板生成为html,所以我想使用一些cutomjavascript来使用切换按钮切换悬停工具。

以下是一些示例代码:

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开始,不支持从工具栏动态添加/删除工具。您可以通过以下方式激活/停用工具:

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)

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

p.toolbar_location = None

它将完全删除工具栏,而不仅仅是悬停工具按钮。悬停工具仍然可以使用该按钮进行切换。

最新更新