How to let options of panel.widgets.MultiSelect depend on other widget inputs

1.5k Views Asked by At

I have a multiple select widget, pn.widgets.MultiSelect, and I am trying to get that when its value is selected it updates the options of another pn.widgets.MultiSelect. This is very similar to How do i automatically update a dropdown selection widget when another selection widget is changed? (Python panel pyviz), however, I have not had much success implementing it.

Using the data from the above example, but as a pandas dataframe, when I try to update the options nothing happens:

outerType = pn.widgets.MultiSelect(name="Outer", 
                                   value=["Africa"], 
                                   options=np.unique(df.cont).tolist())     

innerType = pn.widgets.MultiSelect(name="Inner",
 options=np.unique(df.loc[df['cont'].isin(outerType.value)].country).tolist())     

@pn.depends(outerType, watch=True)
def _update(outerType):
    _values = np.unique(df.loc[df['cont'].isin(outerType.value)].country).tolist()
    innerType.options = _values
    innerType.value = _values
    
pn.Row(outerType, innerType)

enter image description here

The same happens if I use

@pn.depends(outerType.param.value, watch=True)
def _update(outerType):

However, it does execute as I expect if I use the code below; this uses AnyOldDummy as the argument in def. How can I do this properly please?

outerType = pn.widgets.MultiSelect(name="Outer",
                                   value=["Africa"], 
                                   options=np.unique(df.cont).tolist())     

innerType = pn.widgets.MultiSelect(name="Inner", 
  options=np.unique(df.loc[df['cont'].isin(outerType.value)].country).tolist() )     

@pn.depends(outerType, watch=True)
def _update(AnyOldDummy):
    _values = np.unique(df.loc[df['cont'].isin(outerType.value)].country).tolist()
    innerType.options = _values
    innerType.value = _values
    
pn.Row(outerType, innerType)

enter image description here

Data:

import numpy as np
import pandas as pd
import panel as pn
pn.extension()

_countries = {
    'Africa': ['Ghana', 'Togo', 'South Africa'],
    'Asia'  : ['China', 'Thailand', 'Japan'],
    'Europe': ['Austria', 'Bulgaria', 'Greece']
}
df = []
for cont in _countries.keys(): 
    co = _countries.get(cont)
    df.append(np.c_[np.repeat(cont, len(co)), co])   
df = pd.DataFrame(np.vstack(df), columns=['cont', 'country'])  
0

There are 0 best solutions below