Update options in a dropdown based on another dropdown in streamlit forms

268 views Asked by At

I have a registration form in streamlit. I want to change lists of states that users can choose from based on their choice of country from another dropdown. The problem I'm facing is that unless I actually press Submit button, the st.session_state or dropdown variable doesn't change and I can't see the current user choice.

Can anyone help me with this?

I have tried to use on_change and also save the selection in a variable but they were not helpful.

1

There are 1 answers

0
Bhargav Kar On

One constraint with streamlit is when you use st.forms, the selections of any input widget doesn't get registered unless submit button is hit. Only way around I found is you can keep the input widgets which needs dynamic onclick changes outside st.forms blocks

Sample Code

import streamlit as st

st.header("Region")
country = st.selectbox('Enter your Country',['India', 'USA'])

if 'India' in country: 
    state = st.selectbox("Enter your state",['Karnataka','Maharashtra'], key= 'state')
else:
    state = st.selectbox("Enter your state",['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California'], key= 'state')

with st.forms("Registration_Form"):

    ######## Rest of the input fields for your form ##########

Hope it helps...Cheers!