2017-02-26 103 views
0

我有一个名为getcoords.py的散景服务器应用程序。我启动服务器:bokeh serve getcoords.py。我有HoverToolCustomJS回调函数。除此之外,我有一个quadglyphon_change配置为触发在服务器端的selected事件。每次点击quadglyph时,都会执行onTab函数。当我点击字形时,我想以某种方式与客户端进行通信并获取指针坐标。这里是代码:在散景服务器配置中如何从客户端浏览器获取x,y坐标到服务器

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控制台日志。 Coords: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中的值,然后分配一个全局计数器值,每次执行该例程时将增加1。这里是我希望能够从客户端读取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] 

我曾尝试是创建一个在CustomJS客户端更新一个名为ColumnDataSource sourceXY。然后,当我点击标志符时,服务器端的python代码将读取尚未更新的服务器端ColumnDataSource的值,然后修改它以测试服务器到客户端的通信。这部分工作很好,因为客户端能够读取从服务器发送的x和y。 我想知道是否有办法从客户端到服务器端获取保存在ColumnDataSource(或点击发生时的坐标本身)中的坐标。 任何建议,意见,欢迎。谢谢。

回答

0

我找到了一种方法来将坐标值从服务器更新到客户端。我发现该解决方案更新TextInput模型,其中CustomJS来自HoverTool的javascript回调。我会把我的解决方案放在这里,以防有人从中受益。

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) 

控制台的输出显示正确的坐标每次我在quadglyph挖掘时间:

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 
相关问题