来自工具的散景服务器回调 [英] Bokeh Server callback from tools

查看:39
本文介绍了来自工具的散景服务器回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Python 有点陌生,目前正在使用 Bokeh 进行交互式绘图可视化,我需要在其中显示多个相关图表.为此,我使用了散景服务器.

I'm kinda new on Python, and currently working on a interactive plot visualization using Bokeh where I need to show multiple related charts. To accomplish this i'm using bokeh server.

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

I've been reading the docs and some examples but i've been unable to find an example of a python callback (executed in the server) triggered by a selection on the plot. Basically what i would like to do is something like:

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

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

and then when executing the server and clicking on a patch:

2017-02-14 16:32:00,000 在补丁 A 上执行的 TapTool 回调

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

这种行为是否可以通过散景来实现?

is this behavior something that can be achieved with bokeh?

推荐答案

由项目维护者编辑.

在选择 1.0 之前存在一些混淆和回归.对于任何 1.0 后版本,对于大多数用例,您现在希望在 selected'indices' 属性上使用回调:

There was some confusion and regressions around selections leading up to 1.0. For any post 1.0 version, for most use case you would now want to use a callback on the 'indices' property of selected:

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

这种用法现在在集成测试下得到持续和严格的维护,并且是任何 1.0 后 Bokeh 版本都应该使用的.

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

The selected event can be linked to an update function as follows:

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

更新较新的散景版本.在 0.12.16 版本上测试

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

As @Karel Marik mentions glyphs can not mixed direct values assignment and ColumnDataSource at the same time. So the previous code does not work. Here is an update using only source which also includes code to print the multiple selections (made with shift + click):

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

这篇关于来自工具的散景服务器回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆