Session State Variables are re-initialized to initial state going through pages

hi all,

I am having issues with my session state variables with the following code.

  1. I instantiate the session state variables

  2. In the sidebar, I can select the pages to which I want to navigate to

  3. In each page, there is a widget that takes a value and saves it in the session state.

Problem: when going through different pages, the values that were given are being removed from the session state.

Here is my code:

# Instantiate the Session State Variables

if 'name' not in st.session_state:

    st.session_state.name = ''

if 'age' not in st.session_state:

    st.session_state.age = ''

if 'gender' not in st.session_state:

    st.session_state.gender = ''

# Sidebar Widgets

sidebar_Title = st.sidebar.markdown('# Streamlit')

sidebar_pages = st.sidebar.radio('Menu', ['Page 1', 'Page 2', 'Page 3'])

# Page 1

def page1():

    name = st.text_input('What is your name?', key = 'name' )

# Page 2

def page2():

    age = st.text_input('What is your age?', key = 'age')

# Page 3

def page3():

    gender = st.text_input('What is your gender?', key = 'gender')

# Navigate through pages

if sidebar_pages == 'Page 1':

    page1()

elif sidebar_pages == 'Page 2':

    page2()

else:

    page3()

st.write(st.session_state)