Bokeh - check checkboxes with a button/checkbox callback
<p>How can I check checkboxes in a CheckBoxGroup by clicking a button or checking a separate checkbox in bokeh?</p>

<p>I am aware of this solution in javascript <a href="http://stackoverflow.com/questions/5229023/jquery-check-uncheck-all-checkboxes-with-a-button">jquery check uncheck all checkboxes with a button</a></p>

<p>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.</p>

<p>here is what I tried, plots is a list containing different scatter plots in a figure:</p>

<pre><code>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");
    """)
</code></pre>

<p>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.</p>

<p>I adapted the answer of Bigreddot to this question: <a href="http://stackoverflow.com/questions/39561553/bokeh-widget-callback-to-select-all-checkboxes/39562653#39562653">Bokeh widget callback to select all checkboxes</a> To have a similar effect from a CustomJS callback.</p>

<p>Assuming a Figure "fig" and a list of plots in that figure "plots", here is an example with checkboxes that trigger line visibility:</p>

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

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

checkbox_code = ''.join(['p'+str(i)+'.visible = '+str(i)+'in checkbox.active;' for i in N_plots])
checkbox.callback = CustomJS(args={key: value for key,value in iterable}, lang="coffeescript", code=checkbox_code)
</code></pre>

<p>Here is a button that can clear all checkboxes:</p>

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

<p>And here is a button that checks all the checkboxes:</p>

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

javascript python checkbox bokeh