Throttling in Bokeh application
Asked Answered
G

2

13

I have Bokeh application with a Slider widget that uses the Slider.on_change callback to update my graphs. However, the slider updates come in much faster than my callback function can handle so I need a way to throttle the incoming change requests. The problem is very prominent since the slider calls into the callback during sliding, while only the last slider value (when the user releases the mouse) is of interest.

How could I tackle this problem?

Gel answered 14/7, 2016 at 13:45 Comment(0)
E
21

For Bokeh 2.0 or newer, simply use a callback on "value_throttled":

slider.on_change('value_throttled', ...)
slider.js_on_change('value_throttled', ...)

OLD answer for for Bokeh 1.x

As of Bokeh 1.2, a callback policy applies to both JS callbacks as well as Python callbacks on the server. The value property always updates unconditionally on every move, but a new value_throttled property can be watched for changes according to the policy:

slider.callback_policy = "mouseup"

# both of these will respect the callback policy now
slider.js_on_change('value_throttled', ...)
slider.on_change('value_throttled', ...)

Note that the old callback property is deprecated and will be removed in Bokeh 2.0. All new code should use the general on_change and js_on_change mechanisms.

Eyla answered 14/7, 2016 at 16:8 Comment(6)
Thanks for the suggestion. What about creating a worker thread that would update the graphs according to the last reported value? Or would that introduce thread-safety problems? In that case, can I setup a recurrent timer that I could use to update the graphs? (just some thoughts because I foresee more long-running tasks, not only caused by slider updates)Gel
This could probably work but you will need to study/refer to the User's Guide sections on Updating From ThreadsEyla
It is still an open feature request as of 0.12.6 github.com/bokeh/bokeh/issues/4540Eyla
Bokeh 1.2 supports callback policy for Bokeh apps, see updated answer.Eyla
Thanks for the update, can confirm that it works nicely with version 1.2. For everybody else: Make sure you are actually using "value_throttled" and not "value" in your .on_change() function.Incident
For those using Bokeh 2.0.0 and higher see below: https://mcmap.net/q/859558/-throttling-in-bokeh-applicationKristelkristen
K
2

Those using Bokeh 2.x will get this error:

AttributeError: unexpected attribute 'callback_policy' to Slider, possible attributes are align, aspect_ratio, background, bar_color, css_classes, default_size, direction, disabled, end, format, height, height_policy, js_event_callbacks, js_property_callbacks, margin, max_height, max_width, min_height, min_width, name, orientation, show_value, sizing_mode, start, step, subscribed_events, tags, title, tooltips, value, value_throttled, visible, width or width_policy

when running this code:

from bokeh.models.widgets import Slider
slider = Slider(callback_policy='mouseup')

The release guide mentions the following about API removals:

bokeh.models.widgets.sliders

callback, callback_throttle, and callback_policy removed from all sliders. Use value for continuous updates and value_throttled for updates only on mouseup

One also has to do the following:

slider.on_change('value_throttled', ...)
Kristelkristen answered 25/9, 2020 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.