How do you keep Streamlit widgets from getting reset when I collapse/expand a heading

Viewed 234

I'm playing with Streamlit, and there is some behaviour I don't know how to manage.

I'd like three widgets in a collapsible section, but when I collapse/expand the section the values go back to their defaults, making correcting the values a bit of a pain for the user.

Here is some sample code:

import streamlit as st

with st.beta_expander("Describe your baby"):
    age, weight, sex = st.beta_columns((1,1,1))

with age:
    baby_age = st.number_input('Baby age in months', min_value=0.0, max_value=36.0, step=0.25, value=2.0)

with weight:
    baby_weight = st.number_input('Baby weight in KG', min_value=0.0, max_value=16.0, step=0.01, value=6.0)

with sex:
    baby_sex = st.selectbox('Baby sex', ['Male', 'Female'])

st.write(f'Your {baby_age} month old {"boy" if baby_sex=="Male" else "girl"} is {baby_weight}kg.')

What is the intended way to handle this?

1 Answers

It seems to now work for streamlit>=1.8.0:

Parameters' state is now saved

Of course, it also works with the st.expander too. You don't need the beta_ in the new versions.

Related