How to nest selectbox

Hello guys:

I’d like to pop up a selectbox depend on the first level selectcox options. How can I do this?

For example, the first level selectbox provide the customer the options of (cars, food, electronics). Then if the customer choose food, then pop up the second level selectbox of food options as (rice, drink, meat). Then if a customer choose rice, list print all rice stocks on the screen such as " long grain, short grain."

I found the second level selectbox doesn’t work, once the button is pressed, all the record is gone even with the session.

Hey Cu_Buffalo, welcome to Streamlit! :smiley:

I’m a Streamlit user just like you but as far as I know nesting selectboxes isn’t a thing yet (I’m curious about your code example though!).
But a way to work around this would be to use 2 selectboxes like this:

import streamlit as st

level_two_options = {
    "Cars" : ["Honda", "Opel", "Tesla"],
    "Food" : ["Egg", "Pizza", "Spinach"],
    "Electronics" : ["Headphones", "Laptop", "Phone"]
}

first_choice = "Cars"
first_choice = st.selectbox("First level options", ["Cars", "Food", "Electronics"])
second_choice = st.selectbox("Second level options", level_two_options[first_choice])

st.write("You chose: ", second_choice)

Hope this helps you out a bit or that someone could chime in with a solution to nested selectboxes!
Happy Streamlitting :sunglasses:

Thank you, Fhireman! I didn’t expect someone could answer my question during the weekend. It is awesome.

I tested your code and it works as the alternative. As long as the second level choice was chosen, the script rerun deleting all the second level choice. The code was:

first_level_choice =  st.sidebar.selectbox('', ('Cars', 'Food', 'Electronics'))
b1 = st.sidebar.button('submit level 1 choice')
if b1: 
      if first_level_choice == 'Cars':
              seccond_level_choice_car = st.sidebar.selectbox('', ('Honda', 'Opel', 'Tesla'))
              b21 = st.sidebar.button('submit car choice')
              if b21: 
                  st.write('your car choice is ' + second_level_choice_car)
       if first_level_choice == 'Food':
              second_level_choice_food = st.sidebar.selectbox('', ("Egg", "Pizza", "Spinach"))
              b22 = st.sidebar.button('submit food choice')
              if b22: 
                  st.write('your food choice is ' + second_level_choice_food)
       if first_level_choice == 'Electronics':
              second_level_choice_elec = st.sidebar.selectbox('', ("Headphones", "Laptop", "Phone"))
              b23 = st.sidebar.button('submit elec choice')
              if b23: 
                  st.write('your electronics choice is ' + second_level_choice_elec)

Hi @Cu_Buffalo, I think I recently I did something similar to what you are trying.
Please, check this app and let me know if it is of help
https://share.streamlit.io/napoles-uach/st_selfies/main/st_selfies.py

(Under the hood, this grows molecules based on some rules)

This is very cool app. Thanks so much!

1 Like