Is there anyway for me to force delete / reinitialize the keys tied to button/text input/check box in the session_state?
The reason behind is I have too many keys tied to buttons/text input/check box and the on_click method is not working for me.
I want to make all the keys tied to buttons/text input/check box reinitialized so the buttons/text input/check box displays the default value instead of the key value after experimental_rerun.
Here’s is an example that I believe should illustrate what you desire:
import streamlit as st
MY_WIDGET_KEYS = {
'key1':'default value 1',
'key2':'default value 2',
'key3':'default value 3'
}
def initialize_all():
for key, value in MY_WIDGET_KEYS.items():
st.session_state[key] = value
def make_it_meow():
for key in MY_WIDGET_KEYS.keys():
st.session_state[key] = 'Meow!'
def clear_all():
for key in MY_WIDGET_KEYS.keys():
st.session_state[key] = ''
if 'key1' not in st.session_state:
initialize_all()
st.text_input('Text 1', key='key1')
st.text_input('Text 2', key='key2')
st.text_input('Text 3', key='key3')
st.write(f'Widget 1: {st.session_state.key1}')
st.write(f'Widget 2: {st.session_state.key2}')
st.write(f'Widget 3: {st.session_state.key3}')
cols=st.columns(3)
cols[0].button('Meow', on_click=make_it_meow)
cols[1].button('Clear', on_click=clear_all)
cols[2].button('Reset', on_click=initialize_all)
You can define a function to assign default values to all your widget keys. (Make sure to not simultaneously use the default value optional parameter in your widgets; just use the values assigned to the keys in session state directly to control the default to avoid a warning that can occur if the default value and key don’t agree.)
Put the initializing function inside a check to see if things are initialized so it’s forced to run on first load. Thereafter you can manually trigger it to re-initialize everything (or overwrite it with other values) when desired.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.