Throttled update for Selections

Hey, is there a way to throttle the update of a selection. For example if I have a lasso_select and start selecting points than with every new selected or unselected point the callback will be triggered (simple example below). What I want to achieve is that the callback is trigger after a mouse up when I am done with the selection. Is there a way to achieve this?

import pandas as pd
from bokeh.plotting import show, figure
from bokeh.models import ColumnDataSource

source = ColumnDataSource(pd.DataFrame({"x": [1,2], "y": [1,2]}))
source.selected.on_change("indices", lambda attr, old, new: print("test"))

p = figure(tools="lasso_select")
p.scatter("x", "y", source=source)

show(p)

You can set the select_every_mousemove property on the tool to False. That will cause selections to only register only at the end (i.e. on mouseup).

p.select_one(LassonSelectTool).select_every_mousemove = False
2 Likes

Thank you very much, this solved the problem.

What if I need both behaviors? For example I have some callbacks that are expensive and some that are cheap. So I want all cheap callbacks (e.g. triggering update of a label that says how many points are selected) to immediately trigger while dragging the mouse, but the expensive ones (e.g. that cause some computation involving the selected points) to only trigger on mouse button release.

Right now I think I need to do the expensive triggering manually, e.g., by pressing a button that says “run computation on selection”.

Well, there is the SelectionGeometry event that you can respond to with on_event (or with js_on_event). It has a final property that will be set to True when the geometry is “finalized” (e.g. on mouse-up). However, you will have to experiment and try it out because one thing I don’t know offhand is whether this event is guaranteed to be emitted before or after the actual corresponding selection on the data source is updated.

Edit: it does look like the geometry event is sent last, after the data source selections are updated, which is probably what you’d want.

1 Like