I finally have how to keep the state of a widget like for example selectbox if I change section with a sidebar for example
what surprises me is that the only valid method is to go through a callback and especially why the key of the widget here ‘value’ disappears when I change section
The disappearance of the ‘value’ key is an official feature of streamlit ?
wouldn’t it be wiser to keep this key as long as there is no clear cache and thus avoid switching from one key to another via a callback?
Attached is the sample code
import streamlit as st options = ['a','b'] if 'index' not in st.session_state: st.session_state['index'] = 0 def func_(): st.session_state['index'] = options.index(st.session_state['value']) section = st.sidebar.radio('',['S1','S2']) if section =='S1': st.write(st.session_state) print(st.session_state) index = st.session_state['index'] side = st.selectbox("test" ,options = options,index = index, key = 'value', on_change = func_) st.write(st.session_state) print(st.session_state) if section =='S2': st.write(st.session_state) print(st.session_state)
1 change selectbox to ‘b’
2 pass in section ‘S2’
3 return to section ‘S1’
we find the state of the widget on ‘b’ but the key value has disappeared
thank you in advance