Bokeh - check checkboxes with a button/checkbox callback

泪湿孤枕 提交于 2020-01-17 02:50:10

问题


How can I check checkboxes in a CheckBoxGroup by clicking a button or checking a separate checkbox in bokeh?

I am aware of this solution in javascript jquery check uncheck all checkboxes with a button

however the checkboxgroup bokeh object passed in customJS can't be manipulated with .prop ! Also I don't know of a way to access the individuals checkboxes inside the checkboxgroup. I am not sure how to do that with the bokeh checkboxgroup object.

here is what I tried, plots is a list containing different scatter plots in a figure:

checkbox = CheckboxGroup(labels=[str(i) for i in range(len(plots))],active=range(len(plots)),width=200)
iterable = [('p'+str(i),plots[i]) for i in range(len(plots))]+[('checkbox',checkbox)]
code = ''.join(['p'+str(i)+'.visible = '+str(i)+' not in checkbox.active;' for i in range(len(plots))])
checkbox.callback = CustomJS(args={key: value for key,value in iterable},lang="coffeescript", code=code)

checkbox2 = CheckboxGroup(labels=['check all'],active=[0],width=100)

checkbox2.callback = CustomJS(args={'checkbox':checkbox}, code = """
if (0 not in cb_obj.active){
    checkbox.set("active",_.range(27);
}
checkbox.trigger("change");
    """)

range(27) because len(plots)=27. My first checkboxgroup works perfectly fine to trigger on/off the visibility of plots in the figure. However the second checkbox has no effect.


回答1:


I adapted the answer of Bigreddot to this question: Bokeh widget callback to select all checkboxes To have a similar effect from a CustomJS callback.

Assuming a list of plots in a figure "plots", here is an example with checkboxes that trigger line visibility:

N_plots = range(len(plots))
checkbox = CheckboxGroup(labels=[str(i) for i in N_plots],active=N_plots,width=200)

iterable = [('p'+str(i),plots[i]) for i in N_plots]+[('checkbox',checkbox)]

checkbox_code = ''.join(['p'+str(i)+'.visible = checkbox.active.includes('+str(i)+');' for i in N_plots])
checkbox.callback = CustomJS(args={key: value for key,value in iterable}, code=checkbox_code)

Here is a button that can clear all checkboxes:

clear_button = Button(label='Clear all')
clear_button_code = "checkbox.active=[];"+checkbox_code
clear_button.callback = CustomJS(args={key: value for key,value in iterable}, code=clear_button_code)

And here is a button that checks all the checkboxes:

check_button = Button(label='Check all')
check_button_code = "checkbox.active="+str(N_plots)+";"+checkbox_code
check_button.callback = CustomJS(args={key: value for key,value in iterable}, code=check_button_code)


来源:https://stackoverflow.com/questions/39522642/bokeh-check-checkboxes-with-a-button-checkbox-callback

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