Handling errors in Selectbox chain

Hello everybody, I am new with streamlit, I have a question how can I change the error message ValueError: to a custom one?
I have a chain of 5 selections where you select states, cities and elements in the cities, then there are elements that are not in these cities so it generates some errors with certain combinations of selection

data= data_5.loc[data_5['City'] == select_city]
     select_brand = st.selectbox('Select a brand of stores for this city', options=pd.unique(data_5_fil['Brand']))
     data_5_cad = data_5_fil.loc[data_5_fil['Brand'] == select_Brand]
     select_categ = st.selectbox('Select a product category',	options=pd.unique(data_5_cad['Categ']))
     data_5_cat = data_5_cad.loc[data_5_cad['Categ'] == select_categ]
     select_subcateg = st.selectbox('Select a sub-category',	options=pd.unique(data_5_cat['SubCateg']))
     data_5_subcat = data_5_cat.loc[data_5_cat['SubCateg'] == select_subcateg]

Hi @Risharky, there are probably multiple ways to do this, two of which are below:

  1. The simple, lazy way: wrap your code within a try-except, with a standard error message in case of an error.

  2. Use some pandas df filtering. An example of this is provided by @asehmi in the link below:


1 Like