Bokeh小部件回调以选择所有复选框

时间:2016-09-18 19:06:27

标签: python bokeh

我在尝试设置Bokeh CheckboxGroup小部件时遇到了一些问题。它自己的Checkbox组很大(50个州),我想将选择初始化为所有活动状态。

另外(更重要的是)由于这个小组旨在高度互动,我想在“全选”和“全部清除”中添加按钮。我知道我需要一些回调机制来执行此操作,但在搜索示例,文档和stackoverflow之后,无法弄清楚如何操作。我在下面提供了我的代码的简化版本。我的偏好是使用标准小部件Callback而不是JS回调。

任何帮助表示赞赏!

from bokeh.plotting import curdoc, output_file
from bokeh.models.widgets import Button, CheckboxGroup
from bokeh.layouts import widgetbox, row
from bokeh.models import ColumnDataSource, Callback 

output_file("states.html", title="states")

states = ["Alabama", "Alaska ", "Arizona", "Arkansas", "California", \
        "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", \
        "Hawaii", "Idaho ", "Illinois", "Indiana", "Iowa", "Kansas", \
        "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", \
        "Michigan ", "Minnesota", "Mississippi", "Missouri", "Montana",\
        "Nebraska", "Nevada ", "New Hampshire", "New Jersey",\
        "New Mexico", "New York", "North Carolina", "North Dakota", \
        "Ohio", "Oklahoma","Oregon", "Pennsylvania", "Rhode Island", \
        "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah",\
        "Vermont", "Virginia","Washington", "West Virginia", \
        "Wisconsin", "Wyoming"]

states = CheckboxGroup(
        labels = states,
        active=[0,1])

select_all = Button(label="select all")
# need some help here

group = widgetbox(select_all, states)

layout = row(group)

curdoc().add_root(layout)
curdoc().title = "states"

1 个答案:

答案 0 :(得分:5)

Bokeh服务器的基本功能是在Python和JS端保持所有Bokeh对象同步。 active的{​​{1}}属性指定在任何时候检查哪个盒装,而不仅仅是初始化。因此,要检查所有框,您只需在回调中正确设置它:

CheckboxGroup