ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python – 来自工具的Bokeh服务器回调

2019-10-06 01:56:32  阅读:308  来源: 互联网

标签:python python-3-x callback bokeh


我是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的一些混乱和回归.对于任何1.0版本,对于大多数用例,您现在想要在所选的’indices’属性上使用回调:

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

这种用法现在在集成测试下持续严格地维护,并且应该用于任何1.0 Bokeh版本.

所选事件可以链接到更新功能,如下所示:

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.所以前面的代码不起作用.这是仅使用源的更新,其中还包括用于打印多个选项的代码(使用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))

标签:python,python-3-x,callback,bokeh
来源: https://codeday.me/bug/20191006/1858015.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有