st.spinner() in Streamlit
The spinner method is a context manager in Streamlit used to show a loading indicator while your app is performing a long-running task such as loading data, training models, or reading large files. It displays a temporary spinner with a custom message until the code inside it finishes running.
Syntax
with st.spinner(text="Loading..."):
# long running process here When the code inside this block completes, the spinner disappears automatically.
The function has the following parameter:
- text: Message displayed with the spinner (string)
Example: Basic usage.
Python
# Importing the Streamlit library
import streamlit as st
import time
st.write("Starting process...")
# Show spinner while waiting
with st.spinner("⏳ Please wait... processing data..."):
time.sleep(3) # Simulate a long-running process
st.success("✅ Process completed!") Explanation
- Spinner appears for 3 seconds.
- After the block ends, it disappears automatically.
- A success message is displayed after completion.