我正在尝试使用一个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‘的实例没有'js_on_change’成员”,它在没有崩溃的情况下运行,但是在表中的选择没有任何作用。也曾尝试过:
# 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())上运行它,但是结果是一样的。知道我在这里做错什么了吗?
发布于 2020-05-04 18:16:54
您的代码仍然是不完整的:有一些没有存在的模块的导入,而且该表在任何地方都找不到。
然而,这些问题似乎很容易解决。代码的主要问题是,您正在为CustomJS编写CustomJS代码,就好像它是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)https://stackoverflow.com/questions/61550108
复制相似问题