I using session_state to store values of a slider and a callback function assigned to a button change it’s value later, just like the snippet from this article:
def plus_one():
if st.session_state["slider"] < 10:
st.session_state.slider += 1
else:
pass
return
add_one = st.button("Add one to the slider", on_click=plus_one, key="add_one")
slide_val = st.slider("Pick a number", 0, 10, key="slider")
My problem is that i need to be able to call plus_one after, without the button.
When I simply call the function inside my code, I got an error: **StreamlitAPIException** : st.session_state.slidercannot be modified after the widget with keyslider is instantiated.
Is there a way I update the slider value without getting this error?
Because streamlit code executes from top to bottom, being able to set a widget value after it is initialised wouldn’t make a lot of sense. The widget’s function return occurs when it is called, so being able to set its value later would be confusing.
One solution is to call your function before the widget is initialised.
Otherwise, if what you want is for the slider to be set to a value for the next run of the script, you must still set it before the widget is initialised. One way to do that is with a new variable in st.session_state which lets the next run know it needs to change the value of the widget. For example:
#initialise slider in st.session_state
if 'slider' not in st.session_state:
st.session_state['slider'] = 0
if 'add_one' in st.session_state:
if st.session_state['add_one']:
if st.session_state['slider'] < 10:
st.session_state.slider += 1
st.session_state['add_one'] = False #so this code does not execute next run
slide_val = st.slider("Pick a number", 0, 10, key="slider")
if st.button("Add one to the slider"):
st.session_state['add_one'] = True
What I’m trying to achieve it’s like a “two way binding” between the widget value and another function down in the code. My script is processing videos and I`m using the slider to set the actual frame, but, if a play button is pressed, the script enters in a while True block that iterates over the video. And this “while” block should be able to increment the slider value with:
st.session_state['slider'] += 1. This code returns error. I tried to call the “plus_one” function instead, but with the same error.
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.