OK, It took me a bit to find the Selection thingy, but I got the tap tool to work. Any idea why no line_indices change message when I play with the BoxSelect instead?
import random
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import Line, CustomJS
from bokeh.events import SelectionGeometry
from bokeh.models.tools import BoxSelectTool, TapTool
from palettable.cartocolors.qualitative import Bold_8
output_notebook()
# create a new plot (with a title) using figure
plot = figure(plot_width=500,
plot_height=300,
)
x = list(range(10))
change_script = """
console.log("change called ", whichprop, whichline);
// if cb_obj.line_indices.length == 0, whichline's been untapped
// if cb_obj.line_indices.length == 1, whichline's been selected
console.log(cb_obj.line_indices.length)
"""
lines = []
# add a line renderer
for index, color in enumerate(Bold_8.hex_colors[:4]):
y = [ index * 5 + random.choice(x) for i in range(10) ]
name = f"line{index}"
line = plot.line(x, y, line_width=2, color = color, name = name)
line.selection_glyph = Line(line_color='black', line_width=3)
line.data_source.selected.js_on_change(
'line_indices',
CustomJS( code = change_script,
args = dict( whichprop = 'line_indices', whichline = name
)
)
)
select_script = """
console.log("select called")
console.log(cb_obj)
console.log(cb_data)
"""
select_callback = CustomJS(code=select_script)
box_select = BoxSelectTool()
tap_tool = TapTool()
plot.add_tools(tap_tool, box_select)
plot.js_on_event(SelectionGeometry, select_callback)
show(plot);