我正在运行一个带有简单圆圈字形和TapTool的虚化服务器,用于选择单个圆圈。
现在,我想有一个按钮来选择所有的字形,并更新情节中的选择。
这是我的尝试。
from bokeh import plotting as bplt
from bokeh import layouts as blayouts
from bokeh import models as bmodels
from bokeh import io as bio
from bokeh.server.server import Server
fig = bplt.figure(tools="tap")
source = bmodels.ColumnDataSource(dict(x=[0,1], y=[0,1]))
r = fig.circle('x', 'y', source=source, size=10)
def handler(attr, old, new):
print('attr: {} old: {} new: {}'.format(attr, old, new))
# r.data_source.on_change('selected', handler)
r.data_source.selected.on_change('indices', handler)
button = bmodels.Button(label="select all", button_type="success", width=200)
def callback(event):
'''Here I would like to select all points in the plot with python code'''
# this is my atempt:
print('event: {}'.format(event))
print('data source selected:', r.data_source.selected.indices)
r.data_source.selected.indices = [0]
print('data source selected:', r.data_source.selected.indices)
button.on_click(callback)
def modify(doc):
layout = blayouts.row(fig, button)
doc.add_root(layout)
doc.title = "title"
print('modify', type(doc))
if __name__ == '__main__':
print('Opening Bokeh application on http://localhost:5006/')
server = Server({'/': modify}, num_procs=1)
server.start()
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()
你可以用这个例子来运行
python3 example_code.py
我现在的问题是: 我如何用python回调选择所有Bokeh圆圈的字形,就像我用TapTool手动选择相同的字形一样?
在虚化中 TapTool
让你只选择一个字形而使其他字形静音。在你的例子中,你仍然可以在Python回调中选择两个圆圈,方法是执行 r.data_source.selected.indices = [0,1]
但有什么用呢?
对于多图象选择,你可以使用 BoxSelectTool
, LassoSelectTool
或 PolySelectTool