Bokeh DataTable Selection 时触发事件

Trigger event when Bokeh DataTable Selection

当我 select 一行(或多行)散景 DataTable 时是否可以触发回调事件?

def update(rows):
   ...

dt = DataTable(...)
dt.on_select(update)

我看到有一个 .on_change 方法可以在特定 属性 上触发,但是我找不到与 selected 相对应的 属性行。

我认为选择一行数据 table 与在数据源上进行选择是一样的。因此,如果您将回调附加到支持 table 的数据源,那么回调应该会起作用。

source = ColumnDataSource(mpg)
columns = [....]
data_table = DataTable(source=source, columns=columns)
source.on_change('selected', callback)

birdsarah 的上述回答在 bokeh 版本 0.12.16 之前都是正确的,但是从 bokeh 版本 0.13 开始,您需要稍微更改 on_change 方法以使其工作:

source = ColumnDataSource(mpg)
columns = [....]
data_table = DataTable(source=source, columns=columns)

def callback(attrname, old, new):
    selectionIndex=source.selected.indices[0]
    print("you have selected the row nr "+str(selectionIndex))
source.selected.on_change('indices', callback)