Javascript Bokeh Taptool返回所选索引
我需要在Bokeh图中获取选定数据点的索引,这需要作为web应用程序中另一个函数的输入 我正在使用Bokeh的Taptool CustomJS回调。但是,除了“console.log”之外,我找不到一种方法来获取所选点的实际索引。是否有某种方法将此索引返回到JavaScript外部 下面是我的代码。我不熟悉Javascript和Bokeh。提前谢谢你的帮助Javascript Bokeh Taptool返回所选索引,javascript,python,bokeh,Javascript,Python,Bokeh,我需要在Bokeh图中获取选定数据点的索引,这需要作为web应用程序中另一个函数的输入 我正在使用Bokeh的Taptool CustomJS回调。但是,除了“console.log”之外,我找不到一种方法来获取所选点的实际索引。是否有某种方法将此索引返回到JavaScript外部 下面是我的代码。我不熟悉Javascript和Bokeh。提前谢谢你的帮助 codes = """ var index_selected = source.selected['1d']['indices'][
codes = """
var index_selected = source.selected['1d']['indices'][0];
source.trigger('change');
console.log(index_selected);
"""
taptool.callback = CustomJS(args=dict(source=source),code = codes)
下面的示例绘制了两个三角形。点击一个三角形,相应的索引就会被打印出来。不过,这个示例并没有使用CustomJS
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.io import curdoc
def my_tap_handler(attr,old,new):
index = source.selected.indices
print(index)
source = ColumnDataSource(data=dict(
x=[[1,2,3],[7,8,8,]],
y=[[2,1,3],[6,8,7]]
))
p = figure(tools="tap")
renderer = p.patches('x', 'y', source=source)
renderer.data_source.on_change("selected", my_tap_handler)
curdoc().add_root(p)
这适用于多线程:
selected_src = ColumnDataSource(dict(indices=[])
def handle_selection_change(_attr, # should be 'data'
old_indices,
new_indices):
...
selected_src.on_change('data', handle_selection_change)
taptool.callback = CustomJS(
args=dict(source=source, selected=selected_src),
code = """
selected.data = { "indices" : source.selected.indices };
""")
你想对索引做什么?一般来说,一旦到达Bokeh的javascript界面,它就被设计为留在那里。如果需要将值返回给Python,那么还需要从运行Python的服务器运行Bokeh。另外,您使用的是什么版本的Bokeh
source.trigger('change')
已被删除,您应该使用source.change.emit()
。您是否想将索引返回Python?如果是这样,您将不得不运行Bokeh服务器应用程序。当您进行独立的Bokeh输出时,它会生成静态HTML和JS代码,而这些代码没有连接到任何Python进程。很明显,如果您想要Python的答案,您需要运行Bokeh服务器,但这并没有提供任何关于如何执行的线索。这可能适用于修补程序(我没有尝试)但它似乎无法与multi_Line配合使用,因此无法实现此功能。不知道handle\u selection\u change
应该做什么(在函数中填充pass
),但是selected\u src
似乎总是空的。handle\u selection\u change
根据行选择的更改执行任何需要的操作。在我的代码中,我使用new_index['index'I
根据所选行更新另一个控件。我看到我的代码实际上有一些类似{“index”:source.selected.index}
,所以我可能已经将其写了下来。看看这是否解决了它。{“index”:source.selected.index}
确实解决了这个问题。我相应地修改了我的答案。