Thanks!
This is what I have:
from random import randint
from datetime import date
from bokeh.models import ColumnDataSource, TableColumn, DateFormatter, DataTable, CustomJS
from bokeh.layouts import column
from bokeh.models.widgets import TextInput
from bokeh.client import push_session
from bokeh.plotting import curdoc
selected_source = ColumnDataSource({‘column’: [-1], ‘row’: [-1]})
source = ColumnDataSource(dict(dates = [date(2014, 3, i + 1) for i in range(10)], downloads = [randint(0, 100) for i in range(10)]))
columns = [TableColumn(field = “dates”, title = “Date”, formatter = DateFormatter()), TableColumn(field = “downloads”, title = “Downloads”)]
data_table = DataTable(source = source, columns = columns, width = 400, height = 280, editable = True)
text_row = TextInput(value = None, title = “Row index:”)
text_column = TextInput(value = None, title = “Column Index:”)
text_date = TextInput(value = None, title = “Date:”)
text_downloads = TextInput(value = None, title = “Downloads:”)
def table_click(row, column):
print ('Row %s Column %s clicked' % (row, column))
source_code = “”"
var grid = document.getElementsByClassName(‘grid-canvas’)[0].children;
var row = ‘’;
var col = ‘’;
for (var i=0,max=grid.length;i<max;i++)
{
if (grid[i].outerHTML.includes('active'))
{
row=i;
for (var j = 0, jmax = grid[i].children.length; j < jmax; j++)
{
if(grid[i].children[j].outerHTML.includes('active'))
{ col = j }
}
}
}
new_data = {‘column’: [col], ‘row’: [row]};
source.data = new_data
“”"
source.callback = CustomJS(args = dict(source = selected_source), code = source_code)
def function_source(attr, old, new):
try:
selected_index = source.selected["1d"]["indices"][0]
text_row.value = str(selected_source.data["row"][0])
text_date.value = str(source.data["dates"][selected_index])
text_downloads.value = str(source.data["downloads"][selected_index])
text_column.value = str(selected_source.data["column"][0])
source.selected.update({"0d":{"indices": []}, "1d":{"indices":[]}, "2d":{"indices":[]}})
table_click(selected_source.data["row"][0], selected_source.data["column"][0])
except IndexError:
pass
source.on_change(‘selected’, function_source)
session = push_session(document = curdoc())
curdoc().add_root(column(data_table, text_row, text_column, text_date, text_downloads))
session.show()
session.loop_until_closed()
``
···
On Monday, December 18, 2017 at 8:31:16 PM UTC+1, Sébastien Roche wrote:
I have not tested that bit I guess In the JS callback you could update another ColumnDataSource with the column and row values, then in your python callback you could read that source to determine which function to use.
Le vendredi 15 décembre 2017 17:53:32 UTC-5, Tony Halik a écrit :
Hi Sebastien,
Is there any alternative to do the same using Python callback instead of javascript?
I need to call different Python functions for column 1 and column 2…
from random import randint
from datetime import date
from bokeh.models import ColumnDataSource, TableColumn, DateFormatter, DataTable
from bokeh.layouts import column
from bokeh.models.widgets import TextInput, Button
from bokeh.client import push_session
from bokeh.plotting import curdoc
data = dict(dates = [date(2014, 3, i + 1) for i in range(10)], downloads = [randint(0, 100) for i in range(10)], identities = [‘id_’ + str(x) for x 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, editable = True)
text_row = TextInput(value = None, title = “Row index:”)
text_column = TextInput(value = None, title = “Column Index:”)
text_date = TextInput(value = None, title = “Date:”)
text_downloads = TextInput(value = None, title = “Downloads:”)
def function_source(attr, old, new):
try:
selected_index = source.selected["1d"]["indices"][0]
text_row.value = str(selected_index)
text_date.value = str(source.data["dates"][selected_index])
text_downloads.value = str(source.data["downloads"][selected_index])
except IndexError:
pass
source.on_change(‘selected’, function_source)
session = push_session(document = curdoc())
curdoc().add_root(column(data_table, text_row, text_column, text_date, text_downloads))
session.show()
session.loop_until_closed()
``
On Thursday, August 3, 2017 at 10:16:51 AM UTC+2, Fatih Kılıç wrote:
Thank you for your help Sebastien.
I did in an easy way. I give the snippet code below and shortly explain, maybe someone else might need it later.
def table_select_callback(attr, old, new):
selected_row = new['1d']['indices'][0]
global valueMAC
valueMAC = table_source.data['con_sta'][selected_row]
table_source.on_change(‘selected’, table_select_callback)
table_source is a ColumnDataSource tied to a data table. When any cell is clicked on the data table, the row is selected then con_sta attribute is assigned to the global variable “valueMAC”. con_sta is the name of column. My use case: I set it as global variable so I am able to use it to fetch data from SQL by filtering for my plots for a selected specific device.
26 Temmuz 2017 Çarşamba 19:31:28 UTC+2 tarihinde Sébastien Roche yazdı:
I have an ugly way to get the row and column of a selected cell from data tables, and it doesn’t work when the table is sorted
from bokeh.io import show
from bokeh.layouts import widgetbox
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import DataTable,TableColumn
column_list = [‘col1’,‘col2’,‘col3’]
source = ColumnDataSource(data = {key:range(10) for key in column_list})
columns = [TableColumn(field=col, title=col) for col in column_list]
data_table = DataTable(source=source, columns=columns, width=400, height=280,selectable=True)
source_code = “”"
var grid = document.getElementsByClassName(‘grid-canvas’)[0].children;
var row = ‘’;
var col = ‘’;
for (var i=0,max=grid.length;i<max;i++){
if (grid[i].outerHTML.includes('active')){
row=i;
for (var j=0,jmax=grid[i].children.length;j<jmax;j++){
if(grid[i].children[j].outerHTML.includes('active')){col=j}
}
}
}
console.log(‘row’,row);
console.log(‘col’,col);
cb_obj.selected[‘1d’].indices = ;
“”"
source.callback = CustomJS(code= source_code)
show(widgetbox(data_table))
``