import streamlit as st
import random
if "var" not in st.session_state:
st.session_state["var"] = 0
col3,col4 = st.columns(2)
def set_var(value: int):
st.session_state["var"] = value
def setVal(value:int):
set_var(value)
# st.rerun()
with col4:
st.write("Var value:")
st.write(st.session_state["var"])
with col3:
IB = st.button("A2", key="a2_v2") #, on_click=setVal, args=( random.randint(0, 10),))
if IB:
toto=random.randint(0, 10)
st.write(toto)
setVal(toto)
when I click the IB btn logically the toto value should be equal to st.session_state[“var”] but I found a delay between them. I want to resolve it by the st.rerun() but it freeze my application…infinity loop. do you have please any idea how to resolve this issue without using st.rerun()
I don’t think you need the st.rerun here. Since the order of operations with a button is 1. run the callback 2. rerun the entire page, if you use the on_click method you have commented out and put the random int in the setVal function, does that work?
So like this:
import streamlit as st
import random
if "var" not in st.session_state:
st.session_state["var"] = 0
col3,col4 = st.columns(2)
def set_var(value: int):
st.session_state["var"] = value
def setVal(value: int):
set_var(random.randint(0, 10))
with col4:
st.write("Var value:")
st.write(st.session_state["var"])
with col3:
IB = st.button("A2", key="a2_v2", on_click=setVal)
thanks for your time and efforts, in fact I dont want to pass setVal like this on_click=setVal, i kew it works…but in my real usecase I need to use functions directly I dont have btns…this is the reason why…I tried st.rerun() but it did infinity loop. do you have any idea please ?
Any chance you can share the real use case/full code? I think I’m confused - an infinite loop would usually be a bug, either in your code or potentially in the streamlit rerun
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.