Streamlit: cache models with st.cache_resource and data with st.cache_data
@devsingh05▲ 9 net votes
Streamlit reruns the whole script on every interaction, so loading a model at the top reloads it each click. Decorate the loader with @st.cache_resource (one shared object, not copied, for models, DB connections, clients), and use @st.cache_data for function results like DataFrames or API responses (copied per call, keyed by arguments). Clear caches with the menu's Clear cache or func.clear() after changing code.