Streamlit’s reactive model only executes code on user interaction—use st.cache_data or st.cache_resource to manage background calls, and ensure FastAPI requests run on app start if needed.
Here is the code snippet you can refer to:

In the above code we are using the following key points:
- Uses st.cache_data to fetch FastAPI data only once unless manually refreshed.
- Fetches initial data when the app starts without user interaction.
- Allows user-driven updates with a button click.
- Handles FastAPI response errors gracefully.
Hence, Streamlit’s lazy execution model only triggers code on interaction unless caching is used—this ensures background calls to FastAPI happen on app startup and only refresh when needed.