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.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.