从多个小部件触发相同的bokeh回调

时间:2019-09-15 14:56:20

标签: python-3.x bokeh

我一直在构建一个bokeh情节,其中除了情节本身之外还包括两个不同的小部件。一个是下拉菜单,另一个是“清除图”按钮。

现在,“清除绘图”按钮使函数回调,该功能清除了绘图中的所有点。下拉菜单用于选择将更改打印极限的值。后一个小部件,我也想重置剧情,就像单击按钮一样

由于“按钮”小部件具有on_event样式事件,而“选择”小部件具有on_change样式事件,因此我很难弄清楚如何对同一个函数进行回调(清除图),因为两种事件类型在回调中需要不同的参数。

from bokeh.models import Button, ColumnDataSource
from bokeh.events import ButtonClick 
from bokeh.models.widgets import Select

plot_data = ColumnDataSource(dict(id=[],step=[],ratio=[]))

***some code that populates the ColumnDataSoure***

#Defining button function for resetting alarms
button = Button(label="RESET ALARMS", button_type="danger")

def reset_plot(event):
    #Resetting plot
    plot_data.data = {k: [] for k in plot_data.data}

button.on_event(ButtonClick,reset_plot)

我的Select小部件当前看起来像这样:

menu = [(str(item),str(item)) for item in list_of_items]
dropdown = Select(title='Item', value="Item 1",options=menu)

def change_limits(attr,old,new):
   *some code that changes the plotting limits*

dropdown.on_change('value',change_limits) 

我想要的基本上是一个dropdown.on_change("select new item",reset_plot)回调。

是否可以通过某种方式从reset_plot小部件回调到我的Select函数,还是我只需要在change_limits函数中编写相同的功能?

后者可能会起作用,但是将相同的代码片段嵌入两个不同的函数中似乎很笨拙,我想避免这种情况。

1 个答案:

答案 0 :(得分:1)

  

是否可以通过Selectwidget以某种方式回调我的reset_plot函数,还是只需要在change_limits函数中编写相同的功能?

Bokeh服务器回调与任何其他python函数都没有什么不同,它们本身可以被调用,或以与任何其他上下文完全相同的方式调用其他函数。在这种情况下,听起来您应该将通用代码分解为自己的“清除”函数,然后两个回调都可以调用该函数。