Loss of a session state variable after hiding a text input

Hello everyone,

I have the following problem. I would like to have the input bar hidden but to keep the information related to it. Unfortunately when I hide it and reactivate it, I lose all the information. I can’t figure out why.

This is my code:

import streamlit as st


if 'text' not in st.session_state:
    st.session_state['text'] = 'hello'


st.write(st.session_state)
st.radio(label= 'hide?', options= [True,False], key = 'hide_button' )

if not st.session_state['hide_button']:
    st.text_input(label = "write something",
            value = st.session_state['text'], key = 'text')

These are the 4 situations:

Phase 1: initialization: Text = ‘Hello’, as expected.
image

Phase 2: Uncover and change the text
image

Phase 3:Hide again: Text = ‘Hello again’, as expected.
image

Phase 4 ( the critical one): Uncover and text = ‘hello’ instead of 'hello again’
image

I cannot understand how this is possible.
Why during the hide rerun it lost the session state variable ‘text’? I was expecting session_state[‘text’] to remain ‘hello again’ …

Thank you all very much for your time and help :slight_smile:

I have the same problem actually. Searching the forum I found the following workaround: Using st.session_state and key together

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.