散景:每个字形一个 url

Bokeh: One url per glyph

我有一组数据点,每个数据点都有一个 url 是独一无二的。我想要做的是能够散点图我的数据,然后在单击字形时打开关联的 url。我已经阅读了讨论 and followed the example here,但都没有让我到达我想去的地方。

我曾尝试将 url 保存在标签 属性 中,以供 TapTool 调用:

from bokeh.models import OpenURL, TapTool
from bokeh.plotting import figure, show

p = figure(plot_width = 1200,
           plot_height = 700,
           tools = 'tap')

p.circle(data_x,
         data_y,
         tags = list(data_urls))

taptool = p.select(type = TapTool, arg = "tag")
taptool.callback = OpenURL(url = '@tag')

show(p)

我无法在 Bokeh 文档中找到任何地方来解释 assemble 我想要的行为所需的具体细节。至少不是我能理解的术语。

有人能给我指出正确的方向吗?谢谢!

tags 属性 是不相关的,并且基本上已废弃。您需要将 URL 放在绘图数据源的一列中,以便 OpenURL 回调可以访问它:

from bokeh.models import ColumnDataSource, OpenURL, TapTool
from bokeh.plotting import figure, show

p = figure(plot_width=400, plot_height=400,
           tools="tap", title="Click the Dots")

source = ColumnDataSource(data=dict(
    x=[1, 2, 3, 4, 5],
    y=[2, 5, 8, 2, 7],
    color=["navy", "orange", "olive", "firebrick", "gold"]
    ))

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

# use the "color" column of the CDS to complete the URL
# e.g. if the glyph at index 10 is selected, then @color
# will be replaced with source.data['color'][10]
url = "http://www.colors.commutercreative.com/@color/"
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)

show(p)

此示例在此处记录(并实时):

https://docs.bokeh.org/en/latest/docs/user_guide/interaction/callbacks.html#openurl