Bokeh widget callback to select all checkboxes

我只是一个虾纸丫 提交于 2019-12-07 18:08:37

问题


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.

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.

Any help appreciated!

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:


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"


来源:https://stackoverflow.com/questions/39561553/bokeh-widget-callback-to-select-all-checkboxes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!