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:
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”.
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)
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.
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.
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.