Multiselect affect another multiselect

I try to connect two multiselect, so when I choose the first one the other will active and have value based on the first one.

For example, I have two dataframe

df_states = pd.DataFrame({‘code’: [11, 12, 13],
‘name’: [‘arizona’, ‘california’, ‘texas’] })

df_cities = pd.DataFrame({‘code’: [1101, 1201, 1202, 1301, 1302],
‘name’: [‘phoenix’, ‘sacramento’,‘los angeles’, ‘austin’, ‘houston’]})

so when i choose arizona, for states, the cities will only have one value, phoenix, but if i add california, the citie will consist of three value phoenix, sacramento, los angeles…

so I can filter based on just states or with specific cities

If you only want it one way (state affects cities), that’s pretty straightforward. I’ll assume cities and states are in a single data frame to simplify the code and express the Streamlit part specifically.

import pandas as pd
df = pd.DataFrame({'city':['phoenix','sacremento','los angeles','austin','houston'],

states = st.multiselect('States', df['state'].unique())
if states != []:
    cities = st.multiselect('Cities', df[df['state'].isin(states) ]['city'].unique())

If you want some bidirectional effect between widgets, that gets a little more complicated and requires session state and callbacks.