Python/Bokeh - DataTable 在行选择时触发函数
Python/Bokeh - DataTable triggers a function on row selection
我正在尝试创建一个 DataTable,它会在用户按下特定行时 运行 回调。回调需要具有第一列的值(针对所选行)。
尝试了一些东西,但没有一个奏效:
from bokeh.layouts import widgetbox, row, column
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import Button, RadioButtonGroup, RadioGroup, Tabs, \
TextInput, Panel, Div, Select, DataTable, TableColumn, DateFormatter, \
Slider
from bokeh.plotting import curdoc, show
import db_lib # database interaction
import dataAnalyzer
testBox = TextInput(title="test box", value = "start text")
# getting the data here:
data = {}
data["patients"] = {'kyma PT #': [1297, 1301, 1305, 1312], 'client PT #': [15072, 15255, 15228, 15077], 'patient name': ['John', 'David', 'Mark', 'Martin']}
patients_col = [
TableColumn(field="kyma PT #", title="Kyma PT #", width = 50),
TableColumn(field="client PT #", title="Client PT #", width = 50),
TableColumn(field="patient name", title="Patient Name", width = 200),
]
patients_src = ColumnDataSource(data["patients"])
# method 1
source_code = """
row = cb_obj.indices[0]
testBox.update(value=patients_src.data["kyma PT #"][row])
"""
callback = CustomJS(args = dict(source = patients_src), code = source_code)
patients_src.selected.js_on_change('indices', callback)
layout = column(testBox)
show(layout)
这个显示错误:"Instance of 'Instance' has no 'js_on_change' member",它 运行 没有崩溃,但在 table 中选择没有任何反应。
也试过:
# method 2
def table_select(attr, old, new):
testBox.update(value=patients_src.data["kyma PT #"][row])
patients_src.selected.on_change('indices', table_select)
与第一次尝试相同。
并且:
# method 3
def table_select(attr, old, new):
testBox.update(value=patients_src.data["kyma PT #"][row])
patients_src.on_change('selected', table_select)
此处没有错误,但回调没有运行。
值得注意的是,我也在服务器上 运行 安装它 (curdoc()),但结果是一样的。
知道我在这里做错了什么吗?
您的代码仍然不完整:导入了一些不存在的模块,table 无处可寻。
但是,这些似乎很容易修复。您的代码的主要问题是您正在为 CustomJS
编写 JavaScript 代码,就好像它是 Python 代码一样。
这是您的代码的工作版本:
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, DataTable
from bokeh.models.widgets import TextInput, TableColumn
from bokeh.plotting import show
testBox = TextInput(title="test box", value="start text")
patients_col = [
TableColumn(field="kyma PT #", title="Kyma PT #", width=50),
TableColumn(field="client PT #", title="Client PT #", width=50),
TableColumn(field="patient name", title="Patient Name", width=200),
]
patients_src = ColumnDataSource({'kyma PT #': [1297, 1301, 1305, 1312],
'client PT #': [15072, 15255, 15228, 15077],
'patient name': ['John', 'David', 'Mark', 'Martin']})
source_code = """
const row = cb_obj.indices[0];
testBox.value = source.data["kyma PT #"][row].toString();
"""
callback = CustomJS(args=dict(source=patients_src, testBox=testBox), code=source_code)
patients_src.selected.js_on_change('indices', callback)
layout = column(testBox, DataTable(columns=patients_col, source=patients_src))
show(layout)
我正在尝试创建一个 DataTable,它会在用户按下特定行时 运行 回调。回调需要具有第一列的值(针对所选行)。
尝试了一些东西,但没有一个奏效:
from bokeh.layouts import widgetbox, row, column
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.models.widgets import Button, RadioButtonGroup, RadioGroup, Tabs, \
TextInput, Panel, Div, Select, DataTable, TableColumn, DateFormatter, \
Slider
from bokeh.plotting import curdoc, show
import db_lib # database interaction
import dataAnalyzer
testBox = TextInput(title="test box", value = "start text")
# getting the data here:
data = {}
data["patients"] = {'kyma PT #': [1297, 1301, 1305, 1312], 'client PT #': [15072, 15255, 15228, 15077], 'patient name': ['John', 'David', 'Mark', 'Martin']}
patients_col = [
TableColumn(field="kyma PT #", title="Kyma PT #", width = 50),
TableColumn(field="client PT #", title="Client PT #", width = 50),
TableColumn(field="patient name", title="Patient Name", width = 200),
]
patients_src = ColumnDataSource(data["patients"])
# method 1
source_code = """
row = cb_obj.indices[0]
testBox.update(value=patients_src.data["kyma PT #"][row])
"""
callback = CustomJS(args = dict(source = patients_src), code = source_code)
patients_src.selected.js_on_change('indices', callback)
layout = column(testBox)
show(layout)
这个显示错误:"Instance of 'Instance' has no 'js_on_change' member",它 运行 没有崩溃,但在 table 中选择没有任何反应。 也试过:
# method 2
def table_select(attr, old, new):
testBox.update(value=patients_src.data["kyma PT #"][row])
patients_src.selected.on_change('indices', table_select)
与第一次尝试相同。 并且:
# method 3
def table_select(attr, old, new):
testBox.update(value=patients_src.data["kyma PT #"][row])
patients_src.on_change('selected', table_select)
此处没有错误,但回调没有运行。
值得注意的是,我也在服务器上 运行 安装它 (curdoc()),但结果是一样的。 知道我在这里做错了什么吗?
您的代码仍然不完整:导入了一些不存在的模块,table 无处可寻。
但是,这些似乎很容易修复。您的代码的主要问题是您正在为 CustomJS
编写 JavaScript 代码,就好像它是 Python 代码一样。
这是您的代码的工作版本:
from bokeh.layouts import column
from bokeh.models import ColumnDataSource, CustomJS, DataTable
from bokeh.models.widgets import TextInput, TableColumn
from bokeh.plotting import show
testBox = TextInput(title="test box", value="start text")
patients_col = [
TableColumn(field="kyma PT #", title="Kyma PT #", width=50),
TableColumn(field="client PT #", title="Client PT #", width=50),
TableColumn(field="patient name", title="Patient Name", width=200),
]
patients_src = ColumnDataSource({'kyma PT #': [1297, 1301, 1305, 1312],
'client PT #': [15072, 15255, 15228, 15077],
'patient name': ['John', 'David', 'Mark', 'Martin']})
source_code = """
const row = cb_obj.indices[0];
testBox.value = source.data["kyma PT #"][row].toString();
"""
callback = CustomJS(args=dict(source=patients_src, testBox=testBox), code=source_code)
patients_src.selected.js_on_change('indices', callback)
layout = column(testBox, DataTable(columns=patients_col, source=patients_src))
show(layout)