在Bokeh应用程序中限制

时间:2016-07-14 13:45:38

标签: python bokeh throttling

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

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:15)

Bokeh 1.2的更新

从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机制。

HISTORICAL ANSWER:

从发布0.12开始,这仍然有点笨拙,但并非不可能。滑块上有"mouseup"政策,但目前仅适用于CustomJS回调。但是,如果将其与假冒"数据源,我们可以通信并触发最后一个值:

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)

正如我所说,这并不理想。有一些开放功能请求可以在将来改善这种情况。但是团队规模很小,所以如果你有能力做出贡献,请不要犹豫不决(只有新的贡献者可以帮助加快新功能的开发)

答案 1 :(得分:2)

使用Bokeh 2.x的用户会收到此错误:

#<Sketchup::Group:0x000001ba478591e0>
#<Sketchup::Face:0x000001ba4810ca58>
#<Sketchup::Face:0x000001ba4810c828>
#<Sketchup::Face:0x000001ba4810c350>
#<Sketchup::Face:0x000001ba4810c2d8>
#<Sketchup::Face:0x000001ba4810c1e8>
#<Sketchup::Face:0x000001ba4810c1c0>
(0 mm, 0 mm, 300 mm)
(0 mm, 0 mm, 0 mm)
(300 mm, 0 mm, 0 mm)
(300 mm, 0 mm, 300 mm)
---
300 mm
300 mm
300 mm
300 mm

运行此代码时:

#<Sketchup::Group:0x000001ba478591e0>
#<Sketchup::Face:0x000001ba4810ca58>
#<Sketchup::Face:0x000001ba4810c828>
#<Sketchup::Face:0x000001ba4810c350>
#<Sketchup::Face:0x000001ba4810c2d8>
#<Sketchup::Face:0x000001ba4810c1e8>
#<Sketchup::Face:0x000001ba4810c1c0>
(0 mm, 0 mm, 300 mm)
(0 mm, 0 mm, 0 mm)
(300 mm, 0 mm, 0 mm)
(300 mm, 0 mm, 300 mm)
---
300 mm
300 mm
300 mm
300 mm

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

insert(word){ const wordArray = word.word.split("").map(x=>x.toUpperCase()); this.__insert(word,wordArray,this.root); } __insert(wordObject,charArray,refNode){ if(charArray.length>0){ const nextRef = refNode[charArray[0]]|| new WordNode(refNode.word+charArray[0], null); !refNode[charArray[0]]?refNode[charArray[0]]=nextRef:null; charArray.shift(); this.__insert(wordObject,charArray,nextRef); } else { refNode.__setDef(wordObject.definition); } }

url = link + str(self.kwargs['pk'])

还必须执行以下操作:

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