【问题标题】:How can I activate/ deactivate a HoverTool with a bokeh button widget?如何使用散景按钮小部件激活/停用 HoverTool?
【发布时间】:2020-07-25 05:38:30
【问题描述】:

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

这里是一些示例代码:

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

【问题讨论】:

    标签: javascript python bokeh interactive


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      p.toolbar_location = None
      

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

      【讨论】:

        猜你喜欢
        • 2020-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-01
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        相关资源
        最近更新 更多