Session state with conditional widgets

It’s my first day with streamlit – apologies if the question is basic.

I am having an issue of keeping widget state for a multipage app where widgets are displayed conditionally.

Below is a minum example: as long as I stay on page 1 the choices is preserved as I modify the selection. If I move to page 2 and then go back to page 1, it get lost from the state for some reason.

Is this a bug?

Many thanks in advance

import streamlit as st

if 'choice' in st.session_state:
    st.write(f'choice is in the session state and the value is {st.session_state.choice}')
else:
    st.write(f'choice is not yet in the session state ')

navigation = st.sidebar.radio('Navigation', [1, 2])

if navigation == 1:
    st.multiselect('Choice', [1, 2, 3], key='choice')

Answering my own question if it may help anyone: this seems to be an intended (but unintuitive) feature,
it is being tracked over here together with a workaround:

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