そして、ここではあなたもクリックされている表の列を区別することができる方法である。
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()