Disabling / suspending py callbacks

Hi all, in my jupyter & bokeh-based apps, I’d like to occasionally update the value of a few widgets from a function or callback, without triggering the on_change callback registered to those widgets. Specifically, have it set up to save / restore the widget states in / from a config file. Each widget on_change triggers a plot update; setting new values on several widgets ends up repeatedly triggering updates for no good reason.

The desired behavior would be:

[user interacts with widget] → run on_change callback as normal

[code interacts with widget] → do NOT run on_change cb

Is this possible, e.g. through the subscribed_events property? I was not able to find any examples.

Guessing there’d be more efficient ways to structure my callbacks that also sidestep this, but likely beyond my capabilities.

Many thanks for any insight!

Seems like you might want Document.hold and Document.unhold. There is an example here:

bokeh/hold_app.py at branch-2.4 · bokeh/bokeh · GitHub

Though just to be clear, events will fire when unhold is called. But it is possible to get single “net” events for the entire duration, rather than every discrete event that happened while hold was active. If that is not what you want, then your best bet is probably to manage your own flags manually. I.e. callbacks check the value of some flag that you set elsewhere, first thing, and return immediately without doing any work if they are set. That won’t prevent the callbacks from being called but should have the same practical effect.

The other alternative is simply to manually remove the callbacks you don’t want to trigger with remove_on_change, and add them back later when you want to them to trigger again. But this capability is not used much by anyone, so YMMV.

Is this possible, e.g. through the subscribed_events property?

Users should never touch subscribed_events. It’s only public due to a current design limitation, but I would make it private as soon as it is feasible.

Thanks Bryan, great suggestions - appreciate it.

The hold - unhold is cool. In my case adding a simple if flag: update to the callbacks turned out to be the easiest. You’re right, it doesn’t really matter if the callback is triggered with only minimal code to run; turned out to be way less work than I’d thought to restructure it this way. Brilliant.