@alikarb I think I have some good news and bad news. The code above indeed has a circular reference that will not work in later versions of Bokeh. The good news is that can be worked around fairly simply by passing the model IDs instead of the objects themselves. Here is a full example using scatter rather than line:
from collections import defaultdict
from bokeh.plotting import figure, show, row
from bokeh.models import CustomJS
import numpy as np
x=np.arange(0, 10)
y1a = np.random.randint(0, 20, np.size(x))
y1b = np.random.randint(0, 20, np.size(x))
y2a = np.random.randint(0, 20, np.size(x))
y2b = np.random.randint(0, 20, np.size(x))
y3a = np.random.randint(0, 20, np.size(x))
y3b = np.random.randint(0, 20, np.size(x))
p1 = figure(width=400, height=300, title="Plot 1", tools="tap,reset")
p2 = figure(width=400, height=300, title="Plot 2", tools="tap,reset")
p3 = figure(width=400, height=300, title="Plot 3", tools="tap,reset")
lines = (
p1.scatter(x, y1a, color='blue', name='blue', selection_line_width=6),
p1.scatter(x, y1b, color='red', name='red', selection_line_width=6),
p2.scatter(x, y2a, color='blue', name='blue', selection_line_width=6),
p2.scatter(x, y2b, color='red', name='red', selection_line_width=6),
p3.scatter(x, y3a, color='blue', name='blue', selection_line_width=6),
p3.scatter(x, y3b, color='red', name='red', selection_line_width=6),
)
code = """
for (const id of source_ids[line.name]) {
const source = line.document.get_model_by_id(id)
source.selected.indices = cb_obj.indices
}
"""
# partition the source ids by name
source_ids = defaultdict(list)
for line in lines:
source_ids[line.name].append(line.data_source.id)
for line in lines:
callback = CustomJS(args=dict(line=line, source_ids=source_ids), code=code)
line.data_source.selected.js_on_change("indices", callback)
show(row(p1, p2, p3))
This gets everything working using Bokeh’s built-in machinery for selection highlighting and un-highlighting out of the box.
The bad news is that I can only get this to work for scatter-like glyphs, and not for line . I think there are just some bugs or under-developed features in the case of lines because selecting lines (as it has turned out) is just not nearly as common a thing users seem to need to do. I hope to write up some GH issues with details around this to hopefully make improvements for the future. But I am not sure I have any other concrete suggestions to make things work well for lines at the moment. If I come up with something useful, I will certainly post it back here.