在Bokeh服务器配置中,如何将客户端浏览器中的x,y坐标传递到服务器?

3
我有一个名为getcoords.py的bokeh服务器应用程序。我使用以下命令启动服务器:bokeh serve getcoords.py。我有一个HoverTool,其中包含一个CustomJS回调函数。此外,我还有一个quadglyph,其on_change配置为在服务器端触发selected事件。每次我点击quadglyph时,都会执行onTab函数。当我点击图形时,我希望以某种方式与客户端通信并获取指针坐标。下面是代码:
import bokeh
import bokeh.plotting

p = bokeh.plotting.figure(plot_height=200,x_range=(0,10),y_range=(0,10))
imquad = p.quad(top=[8], bottom=[2], left=[2], right=[8])

sourceXY = bokeh.models.ColumnDataSource(data=dict(x = [0], y = [0]))

callback_hover = bokeh.models.CustomJS(args=dict(sourceXY=sourceXY), code="""
    console.log('Coords:'+sourceXY.data['x'] +','+sourceXY.data['y'])
    sourceXY.data['x'] = [cb_data['geometry'].x];
    sourceXY.data['y'] = [cb_data['geometry'].y];
    sourceXY.trigger('change');
    """)
def onHover(attr, old, new):
    print "Hover"

counter = 0

def onTab(attr, old, new):
    global counter
    print "Tap on quad. Coordinates:",sourceXY.data['x'], sourceXY.data['y']
    sourceXY.data['x'], sourceXY.data['y'] = [counter],[counter]
    counter += 1
    sourceXY.trigger('data',None,None)
    # unselecting imquad to keep triggering on_change:
    new['1d']['indices'] = []

imquad.data_source.on_change('selected',onTab)

hover_tool = bokeh.models.HoverTool(callback=callback_hover)
tap_tool   = bokeh.models.TapTool()
p.add_tools(tap_tool)
p.add_tools(hover_tool)

bokeh.io.curdoc().add_root(p)

这是浏览器显示JavaScript控制台日志的屏幕截图。坐标:0,0 1,1 2,2 3,3 4,4 对应于单击四边形图像时刻,这些值从服务器发送到客户端浏览器。JavaScript CustomJS代码首先显示sourceXY的值,然后用x和y数据坐标替换它。当您移动鼠标时,sourceXY将被更新为这些坐标,并且只要您不点击,那些坐标就会在JS控制台中显示。

enter image description here

这是服务器端控制台的屏幕截图。每次点击四元素时,都会执行onTab(attr, old, new)程序。首先显示sourceXY中存储的值,然后分配一个全局计数器值,该计数器值每次执行程序时增加一。我希望能够从客户端读取sourceXY的值,但我还没有做到。

wirelessprv-XX-XXX-XXX-XXX:GetCoords pablo$ bokeh serve getcoords.py 
2017-02-25 21:26:00,899 Starting Bokeh server version 0.12.4
2017-02-25 21:26:00,911 Starting Bokeh server on port 5006 with applications at paths ['/getcoords']
2017-02-25 21:26:00,912 Starting Bokeh server with process id: 36965
2017-02-25 21:26:01,267 200 GET /getcoords (::1) 85.38ms
2017-02-25 21:26:01,785 WebSocket connection opened
2017-02-25 21:26:01,788 ServerConnection created
Tap on quad. Coordinates: [0] [0]
Tap on quad. Coordinates: [0] [0]
Tap on quad. Coordinates: [1] [1]
Tap on quad. Coordinates: [2] [2]
Tap on quad. Coordinates: [3] [3]

我所尝试的是创建一个名为sourceXY的ColumnDataSource,它在CustomJS客户端中被更新。当我点击glyph时,服务器端的Python代码读取未被更新的服务器端ColumnDataSource的值,然后修改它以测试服务器到客户端的通信。这部分很好用,因为客户端能够读取从服务器发送的x和y。我想知道是否有一种方法可以从客户端向服务器端获取保存在ColumnDataSource中的坐标(或者在单击发生时自身的坐标)。欢迎任何建议和评论。谢谢。
1个回答

3
我找到了一种方法,可以将坐标值从服务器更新到客户端。我发现了这个解决方案,通过HoverToolCustomJS JavaScript回调函数,更新了一个TextInput模型。我将我的解决方案放在这里,以防有人能够受益。
import bokeh
import bokeh.plotting

p = bokeh.plotting.figure(plot_height=200,x_range=(0,10),y_range=(0,10))
imquad = p.quad(top=[8], bottom=[2], left=[2], right=[8])

textxy = bokeh.models.TextInput(title="xy val", value='')

callback_hover = bokeh.models.CustomJS(args=dict(textxy=textxy), code="""
    textxy.value = cb_data['geometry'].x + ',' + cb_data['geometry'].y;
    console.log(textxy.value);
    """)

def onTab(attr, old, new):
    print "tap:",textxy.value
    # unselecting imquad to keep triggering on_change:
    new['1d']['indices'] = []

imquad.data_source.on_change('selected',onTab)

hover_tool = bokeh.models.HoverTool(callback=callback_hover)
tap_tool   = bokeh.models.TapTool()
p.add_tools(tap_tool)
p.add_tools(hover_tool)

bokeh.io.curdoc().add_root(p)

每当我点击 quad 图形时,控制台的输出都会显示正确的坐标:

wirelessprv-XXX-XXX-XXX-XXX:GetCoords pablo$ bokeh serve getcoords.py 
2017-02-26 18:09:44,189 Starting Bokeh server version 0.12.4
2017-02-26 18:09:44,199 Starting Bokeh server on port 5006 with applications at paths ['/getcoords']
2017-02-26 18:09:44,199 Starting Bokeh server with process id: 42626
2017-02-26 18:09:46,841 200 GET /getcoords (::1) 68.90ms
2017-02-26 18:09:47,282 WebSocket connection opened
2017-02-26 18:09:47,283 ServerConnection created
tap: 3.3528435714104643,3.925695345068399
tap: 5.715666419702689,6.670813794893257
tap: 6.649805685306592,3.341627589786514
tap: 7.913641162300107,2.407119181335499
tap: 7.913641162300107,7.66372897887246

针对新版本Bokeh的更新。已在0.12.16版本上测试成功。

使用此方法需要向客户端布局添加TextInput模型。本文中将其作为一行的一部分添加,并将其禁用:

import bokeh
import bokeh.plotting

p = bokeh.plotting.figure(plot_height=200,x_range=(0,10),y_range=(0,10))
imquad = p.quad(top=[8], bottom=[2], left=[2], right=[8])

textxy = bokeh.models.TextInput(title="xy val", value='',disabled=True)

callback_hover = bokeh.models.CustomJS(args=dict(textxy=textxy), code="""
    textxy.value = cb_data['geometry'].x + ',' + cb_data['geometry'].y;
    console.log(textxy.value);
    """)

def onTab(attr, old, new):
    print "tap:",textxy.value

imquad.data_source.on_change('selected',onTab)

hover_tool = bokeh.models.HoverTool(callback=callback_hover)
tap_tool   = bokeh.models.TapTool()
p.add_tools(tap_tool)
p.add_tools(hover_tool)
layout = bokeh.layouts.row(p,textxy)

bokeh.io.curdoc().add_root(layout)

应该提到,一个bokeh.models.Div模型可以像在CustomJS回调函数中更新x,y坐标一样用来发送更新后的坐标。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接