Update form before submit button was hit

Hello!

I really really need your help. I have a form (st.form), which is an excellent feature of streamlit. I really like the way that I can change things in there and they will be sent to the Streamlit app only when I click a Submit button. I absolutely need this, because otherwise, my app would load again and again.

Okay, now to my issue. I have one Multiselect there, where you can select Continents and I have one Multiselect where you can select Countries. When I choose one continent, I want that you can only select the countries that belong to that continent. I have the function for that and everything would work, but my problem is now, how do I call this function before clicking submit and after calling this function, how can I update the value (I know, with st.session_state, but I don’t think it will update it when just setting st.session_state.countries = available_countries, because it’s in a form)

I would be so grateful if you could help me, because I was looking for a solution for hours now, but haven’t found one. Thank you so much in advance!

Unfortunately, this is one of the inherent tradeoffs of using a form – it won’t update/rerun if you change a widget, which can be a performance benefit, but also means you don’t get the ability to make one widget update another widget, if they’re both in a form. If you had the continent selector outside of the form, it should work, but I don’t think there’s any way to get a widget in a form to affect another widget in the form before you hit submit.

If you really need this functionality, you might try removing the st.form, and caching any slow functions with st.experimental_memo, and see if you are satisfied with the performance.

Okay, thank you! I will try the caching.