【问题标题】:Bokeh - check checkboxes with a button/checkbox callbackBokeh - 使用按钮/复选框回调检查复选框
【发布时间】:2017-01-24 03:51:22
【问题描述】:

如何通过单击按钮或选中散景中的单独复选框来选中 CheckBoxGroup 中的复选框?

我知道这个解决方案在 javascript jquery check uncheck all checkboxes with a button

但是,customJS 中传递的 checkboxgroup bokeh 对象不能用 .prop 操作! 另外我不知道如何访问复选框组中的个人复选框。 我不确定如何使用散景复选框组对象来做到这一点。

这是我尝试过的,plots 是一个包含图中不同散点图的列表:

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) 因为 len(plots)=27。 我的第一个复选框组可以很好地触发/关闭图中绘图的可见性。但是第二个复选框无效。

【问题讨论】:

    标签: javascript python checkbox bokeh


    【解决方案1】:

    我将 Bigreddot 的答案改编为这个问题:Bokeh widget callback to select all checkboxes To have a similar effect from a CustomJS callback.

    假设图中“地块”中的地块列表,这里是一个带有复选框的示例,可以触发线的可见性:

    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)
    

    这是一个可以清除所有复选框的按钮:

    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)
    

    这是一个检查所有复选框的按钮:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-05
      • 1970-01-01
      • 2018-12-16
      • 2021-06-20
      相关资源
      最近更新 更多