What is a difference between an `if` block and `on_click` for a button?

Hi @mkaut

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.

There’s a similar thread here:

Also, in the Docs, there is a short mention of this here (Main concepts - Streamlit Docs):

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.

Hope this helps!

Best regards,
Chanin