ipywidgets SelectMultiple 在多个下拉菜单上

ipywidgets SelectMultiple on multiple Dropdown menu

我有 2 个下拉菜单:1) Num Clusters 和 2) Cluster

Num Clusters 是 (1,2,3,4,5) 之一,Cluster 取决于select编成 Num Clusters

我想在集群下拉菜单中 select 多个集群。问题是“Cluster”下拉菜单没有用“Num Cluster”下拉菜单更新 selection.

如有任何帮助,我们将不胜感激。提前致谢

import pandas as pd
import numpy as np
import ipywidgets as widgets

dict_cls = {1:np.array([1,1,1,1,1]), 2:np.array([1,2,1,1,1]), 3:np.array([1,2,3,1,1]), 4:np.array([1,2,3,4,1]), 5:np.array([1,2,3,4,5])}

dd_numcls = widgets.Dropdown(
    options=sorted(dict_cls.keys()),
    value=min(dict_cls.keys()),
    description='Num Cluster:')

dd_cls = widgets.SelectMultiple(
    options=sorted(np.unique(dict_cls[dd_numcls.value])),
    value=(min(sorted(np.unique(dict_cls[dd_numcls.value]))),),
    description='Cluster:')

b_update_cls = widgets.Button(
    description='Select Cluster')

def change_numcls(b):
    tmp_lst = np.unique(dict_cls[dd_numcls.value])
    dd_cls.options = sorted(tmp_lst)
    dd_cls.value = min(tmp_lst)

dd_numcls.observe(change_numcls)

output_filter = widgets.VBox(
    [ widgets.HBox([dd_numcls, dd_cls, b_update_cls]), ] ,
    layout=widgets.Layout(width='2100px'))

output_filter

但是,如果我使用 Dropdown 而不是 SelectMultiple,则“集群”更新没有问题:

dd_cls = widgets.Dropdown(
    options=sorted(np.unique(dict_cls[dd_numcls.value])),
    value=(min(sorted(np.unique(dict_cls[dd_numcls.value]))),),
    description='Cluster:')

如果您能显示收到的错误消息(而不是“它不起作用”),那通常会有帮助。

这里的错误信息很有指导意义。

TraitError: The 'value' trait of a SelectMultiple instance expected a tuple, not the int64 1.

如果您将 dd_cls.value 的赋值从整数更改为元组,我认为这就是您想要的行为。

import numpy as np
import ipywidgets as widgets

dict_cls = {1:np.array([1,1,1,1,1]), 2:np.array([1,2,1,1,1]), 3:np.array([1,2,3,1,1]), 4:np.array([1,2,3,4,1]), 5:np.array([1,2,3,4,5])}

dd_numcls = widgets.Dropdown(
    options=sorted(dict_cls.keys()),
    value=min(dict_cls.keys()),
    description='Num Cluster:')

dd_cls = widgets.SelectMultiple(
    options=sorted(np.unique(dict_cls[dd_numcls.value])),
    value=(min(sorted(np.unique(dict_cls[dd_numcls.value]))),),     # now a tuple
    description='Cluster:')

b_update_cls = widgets.Button(
    description='Select Cluster')

def change_numcls(b):
    tmp_lst = np.unique(dict_cls[dd_numcls.value])
    dd_cls.options = sorted(tmp_lst)
    dd_cls.value = (min(tmp_lst),)  # now a tuple

dd_numcls.observe(change_numcls)

output_filter = widgets.VBox(
    [ widgets.HBox([dd_numcls, dd_cls, b_update_cls]), ] ,
    layout=widgets.Layout(width='2100px'))

output_filter

问题是“值”和“dd_cls_value”不是元组。下面的代码工作得很好。感谢@ac24

import pandas as pd
import numpy as np
import ipywidgets as widgets

dict_cls = {1:np.array([1,1,1,1,1]), 2:np.array([1,2,1,1,1]), 3:np.array([1,2,3,1,1]), 4:np.array([1,2,3,4,1]), 5:np.array([1,2,3,4,5])}

dd_numcls = widgets.Dropdown(
    options=sorted(dict_cls.keys()),
    value=min(dict_cls.keys()),
    description='Num Cluster:')

dd_cls = widgets.SelectMultiple(
    options=sorted(np.unique(dict_cls[dd_numcls.value])),
    value=(min(sorted(np.unique(dict_cls[dd_numcls.value]))),),   #TUPLE
    description='Cluster:')

b_update_cls = widgets.Button(
    description='Select Cluster')

def change_numcls(b):
    tmp_lst = np.unique(dict_cls[dd_numcls.value])
    dd_cls.options = sorted(tmp_lst)
    dd_cls.value = (min(tmp_lst),)    #TUPLE

dd_numcls.observe(change_numcls)

output_filter = widgets.VBox(
    [ widgets.HBox([dd_numcls, dd_cls, b_update_cls]), ] ,
    layout=widgets.Layout(width='2100px'))

output_filter