Building Streamlit apps with state

Apr 24, 2025 by Cylab Researcher | 62 views

Streamlit

https://cylab.be/blog/414/building-streamlit-apps-with-state

This blog post is the second in our two-part series about Streamlit. If you wish to revisit the basics of Streamlit head over to our first article on how to Kickstart ML Apps with Streamlit in 30 Minutes

If you’ve been working with Streamlit, you might have noticed that your variables sometimes behave unexpectedly - disappearing or resetting when you interact with your app. Your incredible counter that increments by a value set in a slider just never seems to keep adding numbers beyond its initial value.

streamlit4.png

In this guide, we’ll explore five fundamental concepts that will help you master Streamlit’s state management and build more sophisticated applications.

We’ve created an interactive Streamlit app right here where you can follow each of the exercises.

Here’s a sneak preview of what you can expect from this tutorial:

  1. Script Reruns : Understanding how Streamlit reruns your script and why state management is necessary,
  2. Basic Session State: Learning to use st.session_state for persistent storage across reruns,
  3. Widget-State Connection: Mastering the automatic connection between widgets and state using keys,
  4. Callbacks: Implementing complex state updates with callback functions,
  5. Complex State: Managing sophisticated state structures like lists and dictionaries

Happy tinkering!

This blog post is licensed under CC BY-SA 4.0

This website uses cookies. More information about the use of cookies is available in the cookies policy.
Accept