消除bokeh工具栏中的悬停图标

问题描述

我想摆脱散景工具栏上出现的悬停图标,并尝试下面的代码

current_toolbars = plot.toolbar.__getattribute__('tools')
toolbars_to_retain = []
for toolbar_item in current_toolbars :
    if isinstance(toolbar_item,HoverTool) and some_other_condition_for_hover_tool :
       pass
    else :
       toolbars_to_retain.append(toolbar_item)

plot.toolbar.__setattribute__('tools',toolbars_to_retain)

但是问题是,这样做之后,情节的整个工具栏都会崩溃

解决方法

您必须为此提供自定义CSS。请注意,这种隐藏图标的方法很脆弱-它可能会随Bokeh更新而中断,因为它依赖于实现中未记录的部分。

from bokeh.core.templates import FILE
from bokeh.io import save
from bokeh.plotting import figure

p = figure(tools='hover')
p.circle(0,size=100)

template = FILE.environment.from_string("""\
{% extends "file.html" %}
{% block postamble %}
<style>
.bk-toolbar-button.bk-tool-icon-hover {
    display: none;
}
</style>
{% endblock %}
""")

# Cannot use `show` because it doesn't accept a template.
save(p,template=template)