I have an application that has multiple ‘pages’ similar to the code below. I’d like to be able to prepopulate input widgets with values set by the user previously. The code I have written below does that, but there is weird behavior when trying to modify the text value. You have to enter the input twice in order to set a new value. The first input just gets erased.
import streamlit as st #1.6.0
def load_previous_input(label):
"""If it exists, populate input with previous user entry, otherwise None"""
return st.session_state[label] if label in st.session_state else None
def save_input(label, new_value):
"""Overwrite previous input with new input"""
st.session_state[label] = new_value
with st.sidebar:
selected_page = st.radio('Select Page', ['Page 1', 'Page 2'])
if selected_page == 'Page 1':
label = 'Enter Some Text'
value = load_previous_input(label)
user_text = st.text_input(label, value) if value is not None else st.text_input(label)
save_input(label, user_text)
elif selected_page == 'Page 2':
st.write("""Page 2: User does stuff on this page, when clicking back to Page 1, I want the field to
prepopulate with what they entered before (if it exists). It seemingly works, (prepopulates with previous input)
but look what happens when you try to change the text...
""")
Is this a bug with Streamlit? Or is there a better way to accomplish this?
Thanks!