0

I'm trying to have a DataTable which will run a callback when the user press on a certain row. The call back need to have the value of the first column (for the selected row).

Tried few things but non of them worked:

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)

This one displays an error: "Instance of 'Instance' has no 'js_on_change' member", it runs without crash but selecting in the table does nothing. Also tried:

# 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)

Same thing as the first attempt. And:

# method 3
def table_select(attr, old, new):
    testBox.update(value=patients_src.data["kyma PT #"][row])

patients_src.on_change('selected', table_select)

No error here, but the callback does not run.

Worth noting that I'm also running it on server (curdoc()), but the outcome is the same. Any ideas what I'm doing wrong here?

2
  • Your code is incomplete. Please provide a complete example with some test data, one that can be run as is. Commented May 2, 2020 at 17:05
  • @Eugene Pakhomov - thanks, I added some sample data. Commented May 4, 2020 at 16:02

1 Answer 1

2

Your code is still incomplete: there are some imports of non-existing modules and the table is nowhere to be found.

However, those seemed to be easy to fix. The main issue with your code is that you're writing the JavaScript code for CustomJS as if it was Python code.

Here's a working version of your code:

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Great! thank you for the help. I guess it is obvious that I'm no software eng. :). One thing I still get is the "Instance of 'Instance' has no 'js_on_change' member" on the 'patients_src.selected.js_on_change('indices', callback)' statement. Any ideas?
What Bokeh version do you have?
2.0.2 (running on Tornado 6.0.3)
If you're 100% certain that it's the line in my code above that produces that error and you're using my code verbatim, then I'm at a loss. Given that symptom, you shouldn't be able to use any property of any Bokeh model, because they all would be instances of various property descriptor types instead of containing actual values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.