Bokeh 应用程序中的节流

Throttling in Bokeh application

我有一个带有滑块小部件的 Bokeh 应用程序,该小部件使用 Slider.on_change 回调来更新我的图表。但是,滑块更新的速度比我的回调函数可以处理的要快得多,因此我需要一种方法来限制传入的更改请求。问题很突出,因为滑块在滑动过程中调用了回调,而只有最后一个滑块值(当用户释放鼠标时)才感兴趣。

我该如何解决这个问题?

对于 Bokeh 2.0 或更新版本,只需在 "value_throttled":

上使用回调
slider.on_change('value_throttled', ...)
slider.js_on_change('value_throttled', ...)

散景的旧答案 1.x

从 Bokeh 1.2 开始,回调策略适用于 JS 回调以及服务器上的 Python 回调。 value 属性 始终无条件更新,但可以根据策略监视新的 value_throttled 属性 的变化:

slider.callback_policy = "mouseup"

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

请注意,旧的 callback 属性 已弃用,将在 Bokeh 2.0 中删除。所有新代码都应使用通用的 on_changejs_on_change 机制。

那些使用 Bokeh 2.x 的人会得到这个错误:

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

当 运行 此代码时:

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

release guide 提到了以下关于 API 删除的内容:

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

还必须执行以下操作:

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