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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.