
Suppose that we have a dataset and we plot one column against the other. After looking into the plot we need to select one point and later use it in rest of the program, but not to use it in another plot.
Inspiring by one of the examples in Bokeh JavaScript Callbacks documents, I have this minimal example:
from random import random
from bokeh.layouts import row
from bokeh.models import ColumnDataSource, CustomJS
from bokeh.plotting import figure, output_file, show
x = [random() for x in range(50)]
y = [random() for y in range(50)]
s1 = ColumnDataSource(data=dict(x=x, y=y))
p = figure(width=400, height=400, tools="tap", title="Select Here")
p.circle('x', 'y', source=s1, alpha=0.6, size=10)
s2 = ColumnDataSource(data=dict(x=[], y=[]))
s1.selected.js_on_change('indices', CustomJS(args=dict(s1=s1, s2=s2), code="""
const inds = cb_obj.indices;
const d1 = s1.data;
const d2 = s2.data;
d2['x'] = []
d2['y'] = []
for (let i = 0; i < inds.length; i++) {
d2['x'].push(d1['x'][inds[i]])
d2['y'].push(d1['y'][inds[i]])
}
s2.change.emit();
""")
)
show(p)
In this case, one can easily use the source s2 to plot the selected point, but I need the row in the dataset corresponding to the selected point to use it in the rest of program to compute further things.



