您可以创建一个新的ColumnDataSource 来构建DataTable 并在每次进行选择时更新此数据表的数据:
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.models.widgets.tables import DataTable, TableColumn
from bokeh.layouts import row
from bokeh.io import curdoc
source = ColumnDataSource(dict(
x=[1, 2, 3, 4, 5, 6],
y=[1, 2, 3, 4, 5, 6],
))
p = figure(
plot_height=300,
tools='lasso_select'
)
rc = p.scatter(
x='x',
y='y',
size=20,
color='red',
source=source,
fill_alpha=1.0,
line_alpha=1.0,
)
columns = [
TableColumn(field="value", title="Value"),
]
init_cds = ColumnDataSource(data=dict(value=['']))
table = DataTable(
source=init_cds,
columns=columns,
reorderable=False,
)
def update_table(attr, old, new):
print(new.indices)
if new.indices != []:
new_vals_dict = {'value': new.indices}
else:
new_vals_dict = {'value': ['']}
table.source.data = new_vals_dict
source.on_change('selected', update_table)
curdoc().add_root(row(children=[p, table]))
注意:如果您更新到最新的 Bokeh 版本,您可以使用新的 Selection 对象。您只需要像这样访问选定的索引:new.indices