I need to get the index of a selected data point in a Bokeh plot, which needs to be the input of another function in my web application.
I am using Bokeh's Taptool CustomJS Callback. However, I could not find a way to get the actual index of the selected point besides 'console.log'. Is there some way to return this index to outside the JavaScript?
Below is my code. I am new to Javascript and Bokeh. Thanks for any help in advance.
codes = """
var index_selected = source.selected['1d']['indices'][0];
source.trigger('change');
console.log(index_selected);
"""
taptool.callback = CustomJS(args=dict(source=source),code = codes)
This works with multi_line:
selected_src = ColumnDataSource(dict(indices=[])
def handle_selection_change(_attr, # should be 'data'
old_indices,
new_indices):
...
selected_src.on_change('data', handle_selection_change)
taptool.callback = CustomJS(
args=dict(source=source, selected=selected_src),
code = """
selected.data = { "indices" : source.selected.indices };
""")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With