Streamlit reruns the whole script on every click and loses variables
@devsingh05▲ 13 net votes
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.