ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • AI Coaching
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Optimizing Performance in Streamlit Apps

author
Generated by
ProCodebase AI

15/11/2024

python

Sign in to read full article

When building Streamlit apps, performance is key to providing a smooth user experience. In this blog post, we'll explore various techniques to optimize your Streamlit applications and make them lightning-fast.

1. Caching: Your Secret Weapon

Caching is one of the most powerful tools in your Streamlit optimization arsenal. It allows you to store the results of expensive computations and reuse them when needed.

Using @st.cache

The @st.cache decorator is your go-to for basic caching:

import streamlit as st import time @st.cache def expensive_computation(x): time.sleep(2) # Simulating a time-consuming operation return x * 2 result = expensive_computation(21) st.write(f"The result is: {result}")

This function will only run once, and subsequent calls will return the cached result.

Leveraging @st.experimental_memo

For more fine-grained control, use @st.experimental_memo:

import streamlit as st @st.experimental_memo(ttl=3600) def fetch_data_from_api(): # Your API call here pass data = fetch_data_from_api() st.dataframe(data)

This decorator allows you to set a time-to-live (TTL) for your cached data, ensuring it's refreshed periodically.

2. State Management: Keep It Local

Efficient state management can significantly improve your app's performance. Use Streamlit's session state to store and manage local data:

import streamlit as st if 'counter' not in st.session_state: st.session_state.counter = 0 if st.button('Increment'): st.session_state.counter += 1 st.write(f"Counter value: {st.session_state.counter}")

This approach avoids unnecessary recomputation and keeps your app responsive.

3. Efficient Data Handling

When working with large datasets, consider these strategies:

Lazy Loading

Load data only when necessary:

import streamlit as st import pandas as pd @st.cache def load_data(): return pd.read_csv("large_dataset.csv") if st.checkbox("Show data"): data = load_data() st.dataframe(data)

Data Aggregation

Aggregate data before displaying:

import streamlit as st import pandas as pd @st.cache def load_and_aggregate_data(): df = pd.read_csv("large_dataset.csv") return df.groupby('category').mean() aggregated_data = load_and_aggregate_data() st.dataframe(aggregated_data)

4. Optimize Rendering

Streamlit offers various ways to display data. Choose the most efficient one for your use case:

  • Use st.dataframe() for interactive tables with small to medium-sized datasets.
  • Opt for st.table() for static, non-interactive tables.
  • Consider st.write() for simple data display.

Example:

import streamlit as st import pandas as pd data = pd.DataFrame({ 'A': range(1000), 'B': range(1000, 2000) }) st.dataframe(data) # Interactive, but might be slower for large datasets st.table(data.head()) # Static, faster for displaying a subset st.write(data.describe()) # Simple and fast for summary statistics

5. Leverage Asynchronous Operations

For long-running tasks, use Streamlit's experimental async support:

import streamlit as st import asyncio @st.experimental_async async def long_running_task(): await asyncio.sleep(5) return "Task completed!" result = await long_running_task() st.write(result)

This keeps your app responsive while performing time-consuming operations in the background.

By implementing these optimization techniques, you'll create Streamlit apps that are not only functional but also fast and efficient. Remember to profile your app and focus on optimizing the most resource-intensive parts for the best results.

Popular tags

pythonstreamlitperformance optimization

Share now!

Like & bookmark

Related collections

  • Advanced Python Mastery: Techniques for Experts

    15/01/2025 · Python

  • Mastering LangGraph: Stateful, Orchestration Framework

    17/11/2024 · Python

  • Django Mastery: From Basics to Advanced

    26/10/2024 · Python

  • Python with Redis Cache

    08/11/2024 · Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 · Python

Related articles

  • Deploying Scikit-learn Models

    15/11/2024 · Python

  • Understanding Python OOP Concepts with Practical Examples

    29/01/2025 · Python

  • Customizing Seaborn Plots

    06/10/2024 · Python

  • Mastering Asynchronous Programming with Asyncio in Python

    15/01/2025 · Python

  • Building Powerful Command-Line Interfaces with Click and Typer in Python

    15/01/2025 · Python

  • Unlocking the Power of Django Templates and Template Language

    26/10/2024 · Python

  • Building RESTful APIs with FastAPI

    15/01/2025 · Python

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design