Depdendant Selectbox

Dear Community,

Recently I trying to build an app with a dataframe which has more than 15000 rows and 75 columns.
Looks like this:

From this dataset, I want to let user select Manufacturer first, then the model Number to further store and display related values for next steps. Something like this:

Please help!!

Hi,

You can modify the choices of the first select box to display “Select” by default and only render second select box if the first value is not “Select”.

Let me know you need any further help

Thanks,
Akshansh

Dear Akshan,

What I need is a dependent dropdown where the logic will be like this:

Can you please give a few lines of code to implement this using streamlit please?

1 Like

Hi

Try this:

import streamlit as st

dummy_data = {
    'Man1':['1','111','1111'],
    'Man2':['2','222','22222'],
    'Man3':['3','333','33333'],
}

if __name__ == "__main__":
    # adding "select" as the first and default choice
    manufacturer = st.selectbox('Select Manufacturer', options=['select']+list(dummy_data.keys()))
    # display selectbox 2 if manufacturer is not "select"
    if manufacturer != 'select':
        model_number = st.selectbox('Select Model Number', options=dummy_data[manufacturer])
    if st.button('Submit'):
        st.write('You selected ' + manufacturer + ' ' + model_number)

Animation2

Thanks,
Akshansh

Dear @akshanshkmr ,

Thanks for the code. but the problem is I need streamlit to read the below dataset and based on “Manufacturer” and “Model Number” selection, the code will give the rest of the values as output to store in a dict or another DF.

maybe you can get the answer at:thttps://discuss.streamlit.io/t/how-to-automatically-change-value-of-dependent-select-box-inside-st-form/28556/2?u=la2yyu

dependent selectbox can work in st.form

you can use masks in dataframe.

simple example:

st.sidebar.header("Choose your filter: ")
# Create a filter for the manufacturer
col = "Manufacturer"
unique_mnfr = sorted(df['Manufacturer'].unique.tolist())
filter_1 = st.sidebar.multiselect(f"Select {col}", unique_mnfr)
mask_1 = df['Manufacturer'].isin(filter_1 or unique_mnfr)

# Create a filter for the Model Number
col = "Model Number"
unique_models = sorted(df[mask_1][col].unique().tolist())
filter_2 = st.sidebar.multiselect(f"Select {col}", unique_models)
mask_2 = df[mask_1][col].isin(filter_2 or unique_models)

final_mask = mask1 & mask2
filtered_df = df[final_mask].copy()
st.dataframe(filtered_df, hide_index=True)

Note: I know it is quite an old thread, but I felt to contribute a little as it might help someone in future. :slight_smile:

Hi @akshanshkmr,
Could you attempt this using st.form? I have numerous requirements similar to this one with st.form, but I’m uncertain why this feature doesn’t seem to support it in the form.