【问题标题】:Bokeh custom ToolTips {safe} tag displays nothing散景自定义工具提示 {safe} 标记不显示任何内容
【发布时间】:2025-12-25 14:00:10
【问题描述】:

当使用最新的 Bokeh 和 py35 运行以下 Bokeh code 时,@fonts{safe} 根本不会出现在工具提示中。但是,当{safe} 被删除时,@fonts 会出现(当然不会呈现)。有谁知道我可以做些什么来使{safe} 工作?或者还有其他方法可以让 HTML 在 ToolTips 中呈现?

from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool

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'],
            imgs = [
                'http://docs.bokeh.org/static/snake.jpg',
                'http://docs.bokeh.org/static/snake2.png',
                'http://docs.bokeh.org/static/snake3D.png',
                'http://docs.bokeh.org/static/snake4_TheRevenge.png',
                'http://docs.bokeh.org/static/snakebite.jpg'
            ],
            fonts=['<i>italics</i>',
                   '<pre>pre</pre>',
                   '<b>bold</b>',
                   '<small>small</small>',
                   '<del>del</del>'
                   ]
        )
    )

hover = HoverTool(
        tooltips="""
        <div>
            <div>
                <img
                    src="@imgs" height="42" alt="@imgs" width="42"
                    style="float: left; margin: 0px 15px 15px 0px;"
                    border="2"
                ></img>
            </div>
            <div>
                <span style="font-size: 17px; font-weight: bold;">@desc</span>
                <span style="font-size: 15px; color: #966;">[$index]</span>
            </div>
            <div>
                <span>@fonts{safe}</span>
            </div>
            <div>
                <span style="font-size: 15px;">Location</span>
                <span style="font-size: 10px; color: #696;">($x, $y)</span>
            </div>
        </div>
        """
    )

p = figure(plot_width=400, plot_height=400, tools=[hover],
           title="Mouse over the dots")

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

show(p)

【问题讨论】:

    标签: javascript python plot tooltip bokeh


    【解决方案1】:

    它是a bug in the code。我进行了建议的更改here 并创建了这个pull request。从 2017 年 1 月 11 日起,该修复程序将无法通过 pip 安装,但如果您关注 @bryevdv's advice,您可以通过 conda developer channel 安装它,或者像我通过 dev guide 一样从头开始构建它。

    【讨论】: