5

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.

1

2 Answers 2

13

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)
6

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)
6
  • Will this work in a notebook or does this require an active bokeh server?
    – MRocklin
    Aug 18, 2016 at 18:58
  • This will work in a notebook/html with CustomJS callback. source.callback = CustomJS(code=.....) I'm not aware of server style callbacks working generally in notebooks.
    – birdsarah
    Aug 18, 2016 at 19:28
  • 1
    How can you get the selected row data? Jan 11, 2017 at 8:44
  • 1
    bokeh.pydata.org/en/latest/docs/reference/models/… source.data gives you the data dictionary from which you extract the relevant data.
    – birdsarah
    Jan 13, 2017 at 16:36
  • 1
    Is this still working? attempted to add a callback on nonexistent DataTable.selected property
    – user5128720
    Nov 27, 2017 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.