Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger event when Bokeh DataTable Selection

Tags:

python

bokeh

Is it possible to trigger a callback event when I select a row (or rows) of a Bokeh DataTable?

def update(rows):
   ...

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

I see that there is an .on_change method that can trigger on a particular property, however I can not find a property that corresponds to the selected rows.

like image 780
MRocklin Avatar asked Aug 18 '16 18:08

MRocklin


2 Answers

The above answer by birdsarah is correct up to bokeh version 0.12.16 but as of bokeh version 0.13 you need to slighty change the on_change method to make it work:

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)
like image 182
Joris Avatar answered Sep 29 '22 19:09

Joris


I believe that selecting a row of a data table is the same as making a selection on a data source. So, if you attach a callback to the datasource powering the table then the callback should work.

source = ColumnDataSource(mpg)
columns = [....]
data_table = DataTable(source=source, columns=columns)
source.on_change('selected', callback)
like image 31
birdsarah Avatar answered Sep 29 '22 19:09

birdsarah