Throttling in Bokeh application

夙愿已清 提交于 2019-11-29 09:49:51

UPDATE for Bokeh 1.2

As of Bokeh 1.2, the 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.


HISTORICAL ANSWER:

As of release 0.12 this is still a bit clunky to accomplish, but not impossible. There is a "mouseup" policy on sliders, but this currently only applies to CustomJS callbacks. However, if that is combined with a "fake" data source, we can communicate and trigger just the last value:

from bokeh.io import curdoc
from bokeh.layouts import column
from bokeh.plotting import figure
from bokeh.models.callbacks import CustomJS
from bokeh.models.sources import ColumnDataSource
from bokeh.models.widgets import Slider

# this is the real callback that we want to happen on slider mouseup
def cb(attr, old, new):
    print("UPDATE", source.data['value'])

# This data source is just used to communicate / trigger the real callback
source = ColumnDataSource(data=dict(value=[]))
source.on_change('data', cb)

# a figure, just for example
p = figure(x_range=(0,1), y_range=(0,1))

# add a slider with a CustomJS callback and a mouseup policy to update the source
slider = Slider(start=1, end=10, value=1, step=0.1, callback_policy='mouseup')
slider.callback = CustomJS(args=dict(source=source), code="""
    source.data = { value: [cb_obj.value] }
""")

curdoc().add_root(column(slider, p))

# make sure to add the source explicitly
curdoc().add_root(source)

As I said, this is not ideal. There are some open feature requests that could improve this situation in the future. However the team is quite small, so if you have the ability to contribute, please don't hesitate to reach out (only new contributors can help accelerate development of new features)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!