簡體   English   中英

如何使用 python 回調 select 散景圓形字形?

[英]How can I select a bokeh circle glyphs with a python callback?

我正在運行一個帶有簡單圓圈字形的散景服務器和一個到 select 單個圓圈的 TapTool。

現在我想要一個 select 所有字形的按鈕並更新 plot 中的選擇。

這是我的嘗試:

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

我現在的問題如下:我如何 select 所有帶有 python 回調的 Bokeh 圓形字形與手動使用相同字形的 select 相同?

在 Bokeh 中, TapTool讓您 select 只有一個字形並靜音其他字形。 在您的示例中,您仍然可以 select 在 Python 回調中的兩個圓圈,方法是執行r.data_source.selected.indices = [0,1]但有什么目的?

對於多字形選擇,您可以使用BoxSelectToolLassoSelectToolPolySelectTool

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM