Trigger a full-script rerun from inside a fragment
Streamlit lets you turn functions into fragments, which can rerun independently from the full script. When a user interacts with a widget inside a fragment, only the fragment reruns. Sometimes, you may want to trigger a full-script rerun from inside a fragment. To do this, call st.rerun inside the fragment.
Applied concepts
- Use a fragment to rerun part or all of your app, depending on user input.
Prerequisites
-
This tutorial requires the following version of Streamlit:
Text -
You should have a clean working directory called
your-repository. -
You should have a basic understanding of fragments and
st.rerun.
Summary
In this example, you'll build an app to display sales data. The app has two sets of elements that depend on a date selection. One set of elements displays information for the selected day. The other set of elements displays information for the associated month. If the user changes days within a month, Streamlit only needs to update the first set of elements. If the user selects a day in a different month, Streamlit needs to update all the elements.
You'll collect the day-specific elements into a fragment to avoid rerunning the full app when a user changes days within the same month. If you want to jump ahead to the fragment function definition, see Build a function to show daily sales data.

Here's a look at what you'll build:

Click here to see the example live on Community Cloud.
Build the example
Initialize your app
-
In
your_repository, create a file namedapp.py. -
In a terminal, change directories to
your_repository, and start your app:TerminalYour app will be blank because you still need to add code.
-
In
app.py, write the following:PythonYou'll be using these libraries as follows:
- You'll work with sales data in a
pandas.DataFrame. - You'll generate random sales numbers with
numpy. - The data will have
datetime.dateindex values. - The products sold will be "Widget A" through "Widget Z," so you'll use
stringfor easy access to an alphabetical string. - Optional: To help add emphasis at the end, you'll use
time.sleep()to slow things down and see the fragment working.
- You'll work with sales data in a
-
Save your
app.pyfile, and view your running app. -
In your app, select "Always rerun", or press the "A" key.
Your preview will be blank but will automatically update as you save changes to
app.py. -
Return to your code.
Build a function to create random sales data
To begin with, you'll define a function to randomly generate some sales data. It's okay to skip this section if you just want to copy the function.
-
Use an
@st.cache_datadecorator and start your function definition.PythonYou don't need to keep re-randomizing the data, so the caching decorator will randomly generate the data once and save it in Streamlit's cache. As your app reruns, it will use the cached value instead of recomputing new data.
-
Define the list of product names and assign an average daily sales value to each.
Python -
For each product, use its average daily sales to randomly generate daily sales values for an entire year.
PythonIn the last line,
data.index.datestrips away the timestamp, so the index will show clean dates. -
Return the random sales data.
Python -
Optional: Test out your function by calling it and displaying the data.
PythonSave your
app.pyfile to see the preview. Delete these two lines or keep them at the end of your app to be updated as you continue.
Build a function to show daily sales data
Since the daily sales data updates with every new date selection, you'll turn this function into a fragment. As a fragment, it can rerun independently from the rest of your app. You'll include an st.date_input widget inside this fragment and watch for a date selection that changes the month. When the fragment detects a change in the selected month, it will trigger a full app rerun so everything can update.
-
Use an
@st.fragmentdecorator and start your function definition.PythonSince your data will not change during a fragment rerun, you can pass the data into the fragment as an argument.
-
Optional: Add
time.sleep(1)to slow down the function and show off how the fragment works.Python -
Add an
st.date_inputwidget.PythonYour random data is for 2023, so set the minimun and maximum dates to match. Use a key for the widget because elements outside the fragment will need this date value. When working with a fragment, it's best to use Session State to pass information in and out of the fragment.
-
Initialize
"previous_date"in Session State to compare each date selection.Python -
Save the previous date selection into a new variable and update
"previous_date"in Session State.Python -
Call
st.rerun()if the month changed.Python -
Show the best sellers from the selected date.
Python -
Show the worst sellers from the selected date.
Python -
Optional: Test out your function by calling it and displaying the data.
PythonSave your
app.pyfile to see the preview. Delete these two lines or keep them at the end of your app to be updated as you continue.
Build a function to show monthly sales data
Finally, let's build a function to display monthly sales data. It will be similar to your show_daily_sales function but doesn't need to be fragment. You only need to rerun this function when the whole app is rerunning.
-
Start your function definition.
Python -
Optional: Add
time.sleep(1)to slow down the function and show off how the fragment works.Python -
Get the selected date from Session State and compute the first days of this and next month.
Python -
Show the daily sales values for all products within the selected month.
Python -
Show the total sales of each product within the selected month.
Python -
Optional: Test out your function by calling it and displaying the data.
PythonSave your
app.pyfile to see the preview. Delete these three lines when finished.
Put the functions together together to create an app
Let's show these elements side-by-side. You'll display the daily data on the left and the monthly data on the right.
-
If you added optional lines at the end of your code to test your functions, clear them out now.
-
Give your app a wide layout.
Python -
Get your data.
Python -
Add a title and description for your app.
Python -
Create columns and call the functions to display data.
Python
Make it pretty
Now, you have a functioning app that uses a fragment to prevent unnecessarily redrawing the monthly data. However, things aren't aligned on the page, so you can insert a few containers to make it pretty. Add three containers into each of the display functions.
-
Add three containers to fix the height of elements in the
show_daily_salesfunction.Python -
Add three containers to fix the height of elements in the
show_monthly_salesfunction.PythonThe first container creates space to coordinate with the input widget in the
show_daily_salesfunction.
Next steps
Continue beautifying the example. Try using st.plotly_chart or st.altair_chart to add labels to your charts and adjust their height.
Still have questions?
Our forums are full of helpful information and Streamlit experts.
