Streamlit Fragment
When a widget changes, Streamlit usually runs your whole script again. Fragments break this pattern by letting you run parts of your app again, which makes it run faster and more responsively.
The decorator @st.fragment() just runs the fragment, not the whole app.
A fragment is a function decorated with @st.fragment that reruns independently from the whole app.
User interactions inside the fragment only rerun that fragment, not the full app.
You can also set run_every to rerun the fragment automatically at fixed intervals.
To rerun the whole app, use st.rerun(). To rerun only the fragment, use st.rerun(scope=”fragment”).
Use Session State to share data between fragments and the wider app.
Widgets inside a fragment are redrawn on each rerun. But if you place elements in external containers, they accumulate until the full app reruns.
st.sidebar is not supported directly—wrap the fragment in with st.sidebar: if needed.
Fragments can interact with Session State and other app elements, but be careful with repeated side effects.
Need a Streamlit developer? Click Here
Syntax:
st.fragment(func=None, *, run_every=None)
Example
Here, only the say_hello_fragment() reruns when its button is clicked, not the entire app.
import streamlit as st
@st.fragment
def say_hello_fragment():
if st.button("Hello"):
st.write("Hello inside fragment")
say_hello_fragment()

Automatic Reruns(run_every)
This is perfect for streaming data or status indicators without user input.
Here the random number keeps changing every 10 seconds
import streamlit as st
import numpy as np
@st.fragment(run_every="5s")
def live_counter():
random_number = np.random.rand(1)
st.write(f"This random number: {random_number.item()} fragment updates every 10 seconds!")
live_counter()

Handling Multiple Containers
Fragments shouldn’t write to external containers or elements may accumulate across reruns.
we use st.empty() to prevent that.
Here, when we click on the “Rerun Fragment”, only the fragment reruns.
But, when we click on the “Rerun App”, only the app reruns
import streamlit as st
if "app_runs" not in st.session_state:
st.session_state["app_runs"] = 0
if "frag_runs" not in st.session_state:
st.session_state["frag_runs"] = 0
@st.fragment
def frag():
st.session_state["frag_runs"] += 1
if st.button("Rerun Fragment"):
pass
st.write("Fragment ran:", st.session_state["frag_runs"])
st.session_state["app_runs"] += 1
frag()
if st.button("Rerun App"):
st.rerun()
st.write("App ran:", st.session_state["app_runs"])

Interactive DataFrame Manager
We can use fragments for distinct user tasks like
viewing, adding, editing, deleting rows with session state persistence.
Summary
Fragments let you rerun just parts of your app (@st.fragment())—ideal for efficient dashboards and modular design
Use run_every for automatic updates
Use st.empty() to manage containers and avoid duplication
Avoid fragments with input parameters and sidebar use directly inside fragments
Learn more about Streamlit: Click Here
Watch Videos on Streamlit: