Pass BoxSelectTool cb_data selection to Python code in Bokeh Server

I have a bokeh plot where I need the user to interactively select a certain x-range. With this selection, I need to execute complex Python code - a CustomJS callback is not an option.

The BoxSelectTool seems perfect for this - from the look and feel of it, it is exactly what I need (See screenshot below).

According to the documentation, after finishing a selection, a custom callback can be invoked that tells me the selection range cb_data . However, I cannot find a way to pass that to a Python function - it looks like I can only add a CustomJS callback as callback.

What is the preferred way of passing the selection to custom Python code in Bokeh server? Is there any other tool that works like the BoxSelectTool ?

I managed to emulate the behaviour by activating the BoxSelectTool, ignoring it, and instead reading the raw data generated by the events (as documented here ):

def cb_on_pan_start(event):
  print("start{} _ ".format(event.x))
fig_speeds.on_event(PanStart, cb_on_pan_start)

def cb_on_pan_end(event):
  print("{} ".format(event.x))
fig_speeds.on_event(PanEnd, cb_on_pan_end)

The cb_data parameter is something that only applies to a handful of CustomJS callbacks.

There is however, a SelectionGeometry event, which would seem to be more directly what you are seeking:

p.on_event(events.SelectionGeometry, ...)
1 Like

This is actually the better solution, thank you. Tested it and it works as expected in Bokeh 1.2.0.

Edit: This is supposed to be a reply to your comment, but that doesn’t seem to work…

It seems to have replied here in the topic correctly? If you were wanting to quote part of my reply you can do that by highlighting/selecting the text you want to quote first. Or there is a quote button in the reply editor you can use manually.