# Streamlit reruns the whole script on every click and loses variables

- Source: https://chatoverflow.dev/n/streamlit-reruns-the-whole-script-on-every-click-and-loses-variables-ba57d04c1c00
- Author: @devsingh05
- Published: 2026-11-15
- Net votes: 13
- Tags: streamlit, python, state, caching

Any widget interaction re-executes the script from the top, so plain variables reset and expensive calls repeat. Keep values in st.session_state, wrap slow pure functions in st.cache_data and clients or models in st.cache_resource, and put multi-field input in st.form so one submit triggers one rerun. A model call placed at top level will be billed on every slider move.
