python - 如何从与 Bokeh 的 CustomJS 函数的局部变量同步的 ColumnDataSource 对象获取数据?

标签 python callback bokeh

基于下面的代码示例,我想在 CustomJS 函数中提取数据(例如 x 值)以将其保存在 python 列表 rect_data 中。尽管变量 x 与 ColumnDataSource 对象 source 同步,但当我在下面执行代码的图中绘制矩形选择时,python 列表 rect_data 仍然是一个空列表。我做错了什么,我该如何解决这个问题?

提前致谢!

# You must first run "bokeh serve" to view this example

from bokeh.models import CustomJS, ColumnDataSource, BoxSelectTool, Range1d, Rect
from bokeh.plotting import figure, show
from bokeh.client import push_session
from bokeh.io import curdoc

source = ColumnDataSource(data=dict(x=[], y=[], width=[], height=[]))

callback = CustomJS(args=dict(source=source), code="""

        var data = source.get('data');
        var geometry = cb_data['geometry'];


        var width = geometry['x1'] - geometry['x0'];
        var height = geometry['y1'] - geometry['y0'];
        var x = geometry['x0'] + width/2;
        var y = geometry['y0'] + height/2;


        data['x'].push(x);
        data['y'].push(y);
        data['width'].push(width);
        data['height'].push(height);


        source.trigger('change');
    """)

box_select = BoxSelectTool(callback=callback)

p = figure(plot_width=400,
           plot_height=400,
           tools=[box_select],
           title="Select Below",
           x_range=Range1d(start=0.0, end=1.0),
           y_range=Range1d(start=0.0, end=1.0))


rect = Rect(x='x',
            y='y',
            width='width',
            height='height',
            fill_alpha=0.3,
            fill_color='#009933')



p.add_glyph(source, rect, selection_glyph=rect, nonselection_glyph=rect)

session = push_session(curdoc())

def update():
    global rect_data
    global source    
    rect_data = source.data['x']
    print(rect_data)


curdoc().add_periodic_callback(update,10)
session.show() 
session.loop_until_closed() 

最佳答案

您可以为此目的使用 ToolEvents。请参见下面的示例。

from bokeh.plotting import figure
from bokeh.client import push_session
from bokeh.io import curdoc
from bokeh.models import ColumnDataSource, CustomJS, BoxSelectTool, Range1d, Rect

source = ColumnDataSource(data=dict(x=[], y=[], width=[], height=[]))

callback = CustomJS(args=dict(source=source), code="""

        var data = source.get('data');
        var geometry = cb_data['geometry'];


        var width = geometry['x1'] - geometry['x0'];
        var height = geometry['y1'] - geometry['y0'];
        var x = geometry['x0'] + width/2;
        var y = geometry['y0'] + height/2;


        data['x'].push(x);
        data['y'].push(y);
        data['width'].push(width);
        data['height'].push(height);


        source.trigger('change');
    """)

box_select = BoxSelectTool(callback=callback)

p = figure(plot_width=400,
           plot_height=400,
           tools=[box_select],
           title="Select Below",
           x_range=Range1d(start=0.0, end=1.0),
           y_range=Range1d(start=0.0, end=1.0))


rect = Rect(x='x',
            y='y',
            width='width',
            height='height',
            fill_alpha=0.3,
            fill_color='#009933')


p.add_glyph(source, rect, selection_glyph=rect, nonselection_glyph=rect, name="selectionbox")

session = push_session(curdoc())

def toolEventsCallback(attr, old, new):
    print("callback", new)
    x0 = new[0]['x0']
    x1 = new[0]['x1']
    print("x0=%f  x1=%f" % (x0, x1))

p.tool_events.on_change("geometries", toolEventsCallback)

session.show() 
session.loop_until_closed() 

至少在 Bokeh 0.11.1 中,没有为 ColumnDataSource 将事件发送回 Python。

关于python - 如何从与 Bokeh 的 CustomJS 函数的局部变量同步的 ColumnDataSource 对象获取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35669642/

相关文章:

javascript - 如何从客户端的 Meteor.call 方法返回结果?

javascript - chrome.tabs.executeScript 的回调在我想要它之前被调用,需要解决方法

python-3.x - 使用 bokeh python 将悬停工具添加到第二个 Y 轴

python - 使用Python获取hdfs中文件的文件名和扩展名

python - 如何在开放式简历中使ROI线移动或让用户用鼠标绘制它

javascript - jquery - 从回调函数(在后请求中)返回值到其内部的函数中?

python - 如何在 Bokeh 的条形图中添加数据标签?

python - Bokeh - 子类图

python - 按相似关系过滤图像列表

python - 循环次数可变的嵌套循环