st.button("Button with callback", on_click=st_callback)
where st_callback is a user-defined function?
In both cases, clicking on the button causes a re-run of the app, at least in this simple example:
import streamlit as st
st.balloons()
if 'cnt' not in st.session_state:
st.session_state.cnt = 0
def bt_callback():
st.session_state.cnt += 1
if st.button("Button with if"):
bt_callback()
st.button("Button with callback", on_click=bt_callback)
st.write(f"{st.session_state.cnt = }")
Here, balloons are released after every click on either of the buttons.
And if there is difference, can you provide an example illustrating it? I have not found anything in the documentation.
Thanks for the question. In essence, Streamlit reruns the app upon widget interaction (e.g. triggered by on_click or on_change). To preserve variables from losing their values during the rerun, one can use Session state for that. In your example, your preserving the variable values through the use of session state. So in spite of the app reruns (as noticed from the balloons going off in your app), the count value was added to session state and thus is preserved.
Then every time a user interacts with a widget, Streamlit simply reruns your script from top to bottom, assigning the current state of the widget to your variable in the process.
Thanks, but it does not answer the main question: is there any functional difference between using on_click= and putting the button to an if-block?
Or, to put it differently, why does button have on_click at all?
As also mentioned earlier, the app reruns upon widget interaction. In the Docs, you can see that st.button (is also a widget) will have a on_click, while other widgets such as st.slider, st.text_input, st.selectbox, etc. will have on_change.
Thanks again - and sorry, I should have read the whole thread, not only the quoteā¦
To me, the crucial piece of information is this:
The key is that the callback happens before the page loads.
In other words, the counter will have the increased values for the whole re-run of the script if used with on_click, and only after the button if we use a button with if-block - good to know!
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.