来自工具的 Bokeh 服务器回调

Bokeh Server callback from tools

我是 Python 的新手,目前正在使用 Bokeh 进行交互式绘图可视化,我需要在其中显示多个相关图表。为此,我正在使用散景服务器。

我一直在阅读文档和 some examples 但我一直无法找到由绘图上的选择触发的 python 回调(在服务器中执行)的示例。基本上我想做的是:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = "tap"
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]], y=[[2, 1, 4], [4, 7, 8, 5]], name=['A', 'B']))

p.patches('x', 'y', source=source, color=["firebrick", "navy"], alpha=[0.8, 0.3], line_width=2)


def callback():
    print("TapTool callback executed on Patch {}")


??? <- some code here linking the taptool with the callback function defined above


curdoc().add_root(column(p))

然后在执行服务器并单击补丁时:

2017-02-14 16:32:00,000 TapTool callback executed on Patch A

这种行为可以通过散景实现吗?

由项目维护者编辑。

1.0 之前的选择存在一些混乱和倒退。对于任何 post 1.0 版本,对于大多数用例,您现在希望在 selected'indices' 属性 上使用回调:

source.selected.on_change('indices', callback)

这种用法现在在集成测试中持续严格维护,是任何post 1.0 Bokeh 版本都应该使用的用法。


selected 事件可以链接到更新函数,如下所示:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = "tap"
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]],
                y=[[2, 1, 4], [4, 7, 8, 5]], name=['A', 'B']))

pglyph = p.patches('x', 'y', source=source, color=["firebrick", "navy"],
                                alpha=[0.8, 0.3], line_width=2)

def callback(attr, old, new):
    # The index of the selected glyph is : new['1d']['indices'][0]
    patch_name =  source.data['name'][new['1d']['indices'][0]]
    print("TapTool callback executed on Patch {}".format(patch_name))

pglyph.data_source.on_change('selected',callback)

curdoc().add_root(column(p))

更新了较新的 Bokeh 版本。测试版本 0.12.16

正如@Karel Marik提到的字形不能同时混合直接赋值和ColumnDataSource。所以前面的代码不起作用。这是仅使用 source 的更新,其中还包括用于打印多个选择的代码(通过 shift + 单击进行):

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = ["tap"]
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(
    x=[[1, 3, 2], [3, 4, 6, 6]],
    y=[[2, 1, 4], [4, 7, 8, 5]],
    name=['A', 'B'],color=["firebrick", "navy"],
    alpha=[0.8,0.3],line_width=[3,3]))

pglyph = p.patches('x', 'y', color="color", alpha="alpha",
                   line_width="line_width", source=source)

def callback(attr, old, new):
    # The index of the selected glyph is : new['1d']['indices'][0]
    selections = new['1d']['indices']
    print("Number of selections:{}".format(len(selections)))
    for index in selections:
        patch_name =  source.data['name'][index]
        print("TapTool callback executed on Patch {}".format(patch_name))

pglyph.data_source.on_change('selected',callback)

curdoc().add_root(column(p))

Pablo 发布的解决方案很棒,但不适用于最新的散景版本 (0.12.13)。我遇到运行时错误(无法向字形方法提供用户定义的数据源和可迭代值

以下适合我...

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = "tap"
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(x=[[1, 3, 2], [3, 4, 6, 6]], y=[[2, 1, 4], [4, 7, 8, 5]], alphas = [0.8, 0.3], colors=["firebrick", "navy"], name=['A', 'B']))

pglyph = p.patches(xs='x', ys='y', source=source, line_width=2, alpha = 'alphas', color='colors')

def callback_fcn(attr, old, new):
    # The index of the selected glyph is : new['1d']['indices'][0]
    patch_name =  source.data['name'][new['1d']['indices'][0]]
    print("TapTool callback executed on Patch {}".format(patch_name))

pglyph.data_source.on_change('selected',callback_fcn)

curdoc().add_root(column(p))

自 bokeh 0.13 起,@Karel_Marik a 提出的解决方案需要小的调整:

from bokeh.plotting import figure, curdoc
from bokeh.layouts import column
from bokeh.models import ColumnDataSource

TOOLS = ["tap"]
p = figure(title="Some Figure", tools=TOOLS)

source = ColumnDataSource(dict(
    x=[[1, 3, 2], [3, 4, 6, 6]],
    y=[[2, 1, 4], [4, 7, 8, 5]],
    name=['A', 'B'],color=["firebrick", "navy"],
    alpha=[0.8,0.3],line_width=[3,3]))

pglyph = p.patches('x', 'y', color="color", alpha="alpha",
                   line_width="line_width", source=source)

def callback(attr, old, new):
    # The index of the selected glyph is : new['1d']['indices'][0]
    selections = new['1d']['indices']
    print("Number of selections:{}".format(len(selections)))
    for index in selections:
        patch_name =  source.data['name'][index]
        print("TapTool callback executed on Patch {}".format(patch_name))

pglyph.data_source.on_change('selected',回调)

pglyph.selected.on_change('indices', callback)

curdoc().add_root(column(p))

我希望这对某人有所帮助。