我有一个使用 Slider 控件的 Bokeh 应用程序,它使用 Slider.on_change 回调函数来更新我的图形。然而,滑块更新速度比我的回调函数处理速度快得多,因此我需要一种方法来限制传入的更改请求。问题非常突出,因为滑块在滑动期间调用回调函数,但只有最后一个滑块值(当用户释放鼠标时)是感兴趣的。
我该如何解决这个问题?
我有一个使用 Slider 控件的 Bokeh 应用程序,它使用 Slider.on_change 回调函数来更新我的图形。然而,滑块更新速度比我的回调函数处理速度快得多,因此我需要一种方法来限制传入的更改请求。问题非常突出,因为滑块在滑动期间调用回调函数,但只有最后一个滑块值(当用户释放鼠标时)是感兴趣的。
我该如何解决这个问题?
"value_throttled"
上使用回调即可:slider.on_change('value_throttled', ...)
slider.js_on_change('value_throttled', ...)
针对 Bokeh 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_change
和js_on_change
机制。使用 Bokeh 2.x 版本时,会出现以下错误:
AttributeError: 在 Slider 中意外出现 'callback_policy' 属性,可能的属性有: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 或 width_policy
运行以下代码时会出现此错误:
from bokeh.models.widgets import Slider
slider = Slider(callback_policy='mouseup')
关于API移除,发布指南中提到了以下内容:
bokeh.models.widgets.sliders
所有滑块中的callback、callback_throttle和callback_policy已被移除。使用value进行连续更新,使用value_throttled仅在鼠标松开时进行更新。
同时还需要执行以下操作:
slider.on_change('value_throttled', ...)
0.12.6
版本,这仍然是一个未解决的功能请求。https://github.com/bokeh/bokeh/issues/4540 - bigreddot.on_change()
函数中实际使用的是"value_throttled"
而不是"value"
。 - Thomas