散景小部件回调以选中所有复选框 [英] Bokeh widget callback to select all checkboxes

查看:40
本文介绍了散景小部件回调以选中所有复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have run into a couple problems in trying to setup a Bokeh CheckboxGroup widget. The Checkbox group it self is large (50 states) and I would like to initialize the selection as all active.

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

Also (and more importantly) since this group is intended to be highly interactive, I would like to add buttons to "Select All" and "Clear All". I understand that I will need some callback mechanism to do this, but after searching the examples, documentation and stackoverflow, was not able to figure out just how. I include a simplified version of my code below. My preference is to use the standard widget Callback rather than the JS callback.

任何帮助表示赞赏!

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"

推荐答案

Bokeh服务器的基本功能是使所有Bokeh对象在Python和JS端均保持同步. CheckboxGroupactive属性指定始终检查选中的框,而不仅仅是初始化.因此,要选中所有复选框,只需在回调中进行适当设置:

The basic function of the Bokeh server is to keep all Bokeh objects in sync on both the Python and JS sides. The active property of the CheckboxGroup specifies which boxed are checked, at all times, not just initialization. So to check all the boxes, you only need to set it appropriately in the callback:

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_list = ["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_list,
        active=[0,1])

select_all = Button(label="select all")

def update():
    states.active = list(range(len(states_list)))
select_all.on_click(update)

group = widgetbox(select_all, states)

layout = row(group)

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

这篇关于散景小部件回调以选中所有复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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