📘 Introduction

Streamlit makes it super easy to build interactive apps in Python. But as your app grows — loading datasets, running expensive computations, or calling APIs — you might notice it slows down.

The good news: Streamlit has a built-in caching system that stores results of expensive operations and reuses them instead of recalculating every time. With just a few decorators, you can make your app much faster. In this tutorial, you’ll learn how to use Streamlit’s caching functions to optimize your app.

💡 Why Use Caching?

Without caching, Streamlit re-runs your script from top to bottom every time a user interacts with a widget. That means:

  • Re-downloading or reprocessing data
  • Re-running slow computations
  • Reloading models or re-calling APIs

This makes the app unnecessarily slow.

💡
Caching solves this problem by storing the result of expensive operations and reusing them when the same inputs are requested again.

👉 Use caching when you:

📊 Load large datasets
⚙️ Run heavy computations (e.g., aggregations, simulations)
🌍 Make API calls (avoid hitting rate limits or waiting for responses)
🤖 Load machine learning models (don’t reload on every interaction)
🗄️ Connect to databases or external services

💡
With caching, you’ll notice your app feels much snappier and more user-friendly.

✅ Prerequisites

Before starting, make sure you have the following:

🐍☑️ Python installed
📦☑️ Pip installed
🌐☑️ A virtual environment (venv) created and activated

📦1️⃣ Install Libraries

Install the following Python packages using pip:

pip install streamlit pandas numpy

📥2️⃣ Import Libraries

Start by importing the required Python modules:

import streamlit as st
import pandas as pd
import numpy as np
import time

✍️3️⃣ Create a Basic App (Without Caching)

First, let’s simulate an expensive computation.

Create a file called app.py and add this code:

st.title("Streamlit App")
st.write("This app demonstrates how to use caching in Streamlit.")

# Simulate a slow computation
def load_data(n_rows):
    time.sleep(3)  # Simulate delay
    data = pd.DataFrame({
        "x": np.arange(n_rows),
        "y": np.random.randn(n_rows).cumsum()
    })
    return data

rows = st.slider("Number of rows", 100, 2000, 500)
df = load_data(rows)

st.line_chart(df.set_index("x"))

Now run your app from the terminal:

streamlit run app.py

Notice how every time you move the slider, the app waits 3 seconds. 🐢

⚡4️⃣ Speed It Up with Caching

We can fix this using st.cache_data.

You can view this post with the tier: Academy Membership

Join academy now to read the post and get access to the full library of premium posts for academy members only.

Join Academy Already have an account? Sign In