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.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.

Phase 2: Uncover and change the text

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

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

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: