3

I want to be able to "reset" a data table. How do I do that?

I have a defined an on_change('selected', my_callback) callback for my data table. The callback function has the following signature:

def my_callback(self, attr, old, new):
    ...

However I'm not able to deselect what has been selected after the my_callback handler is called.

I have tried to manipulate the new['1d'] or old['1d'] dictionaries without success.

What is the proper way to deselect rows in a data table and go back to the original state where nothing was selected?

There's a similar question on G-groups but it had no answers. Thanks

Carmellose
  • 4,815
  • 10
  • 38
  • 56

3 Answers3

2

To deselect DataTable rows programmatically and go back to the original state where nothing was selected, you may use this in python:

source.selected.indices = []

where source is the ColumnDataSource for the DataTable.

WebDev
  • 1,211
  • 11
  • 17
1

Here is a bokeh application example. I do not know what you are trying to do as you have not posted your code exactly. The key to deselecting or changing the selected data from a source is you need to replace the entire object or a change will not be registered. The exact same functionality could be achieved through javascript callbacks.

See a related example here, Bokeh: DataTable - how to set selected rows

from datetime import date
from random import randint
from bokeh.io import curdoc
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, DateFormatter, TableColumn, Button

output_file("data_table.html")

data = dict(
        dates=[date(2014, 3, i+1) for i in range(10)],
        downloads=[randint(0, 100) for i in range(10)],
    )
source = ColumnDataSource(data)

columns = [
        TableColumn(field="dates", title="Date", formatter=DateFormatter()),
        TableColumn(field="downloads", title="Downloads"),
    ]
data_table = DataTable(source=source, columns=columns, width=400, height=280)

def callback():
    source.selected = {'0d': {'glyph': None, 'indices': []},
                       '1d': {'indices': []},
                       '2d': {'indices': {}}}

button = Button(label="Reset all selected cells", button_type="success")
button.on_click(callback)

curdoc().add_root(widgetbox(data_table,button))
Anthonydouc
  • 3,334
  • 1
  • 16
  • 29
0

For some reason source.selected.indices = [] did not work for my DataTable, it worked for clicking on squares on a map though.

source.selected.indices[0]=1000 worked for both (1000 should be any number that is larger than the no. of rows in the datatable, so that it can't occur by clicking)

Code I used was:

def bokeh_changeTabOnClick(source,tabs):
    myCallback = CustomJS(args=dict(source=source, tabs=tabs), code="""
        // get the selected row's index
        var row_index = source.selected.indices[0];
        tabs.active = row_index + 1;
        // Clear the selected indices (otherwise can't click same row/ map square again)
        // source.selected.indices = [];     /* this didn't work with my DataTable for some reason perhaps someone else can explain why
        source.selected.indices[0] = 1000;   /* reset to something that will never occur by clicking so that if row is clicked on it will be a change */  
        """)
    # attach the callback to the source.selected property
    source.selected.js_on_change('indices', myCallback)
        
    return 0
Shara
  • 121
  • 2
  • 10