[英]Bokeh - check checkboxes with a button/checkbox callback
How can I check checkboxes in a CheckBoxGroup by clicking a button or checking a separate checkbox in bokeh? 如何通过单击按钮或在bokeh中选中单独的复选框来选中CheckBoxGroup中的复选框?
I am aware of this solution in javascript jquery check uncheck all checkboxes with a button 我在javascript jquery中知道此解决方案, 请通过按钮取消选中所有复选框
however the checkboxgroup bokeh object passed in customJS can't be manipulated with .prop ! 但是在customJS中传递的checkboxgroup bokeh对象不能用.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. 我不确定如何使用bokeh checkboxgroup对象执行此操作。
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. 范围(27),因为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. 我将Bigreddot的答案修改为以下问题: Bokeh小部件回调以选择所有复选框,以使CustomJS回调具有类似的效果。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.