散景应用中的节流 [英] Throttling in Bokeh application

查看:69
本文介绍了散景应用中的节流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Slider小部件的Bokeh应用程序,该小部件使用Slider.on_change回调来更新我的图形.但是,滑块更新的执行速度比我的回调函数处理的速度快得多,因此我需要一种方法来限制传入的更改请求.这个问题非常突出,因为滑块在滑动期间会调用回调,而只有最后一个滑块值(当用户释放鼠标时)才有意义.

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?

推荐答案

散景1.2的更新

从Bokeh 1.2开始,回调策略适用于服务器上的JS回调以及Python回调. value属性始终在每次移动时无条件更新,但是可以根据策略监视新的value_throttled属性的更改:

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', ...)

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

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.

历史答案:

从版本0.12开始,这仍然有些笨拙,但并非没有可能.滑块上有一个"mouseup"策略,但是当前仅适用于CustomJS回调.但是,如果将其与假"数据源结合使用,我们可以进行通信并仅触发最后一个值:

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)

这篇关于散景应用中的节流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆