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

  • TensorFlow Mastery: From Foundations to Frontiers

    06/10/2024 · Python

  • Python Advanced Mastery: Beyond the Basics

    13/01/2025 · Python

  • Python with Redis Cache

    08/11/2024 · Python

  • Seaborn: Data Visualization from Basics to Advanced

    06/10/2024 · Python

  • Automate Everything with Python: A Complete Guide

    08/12/2024 · Python

Related articles

  • Mastering Pandas Reshaping and Pivoting

    25/09/2024 · Python

  • Understanding Recursion in Python

    21/09/2024 · Python

  • Custom Layers and Modules in PyTorch

    14/11/2024 · Python

  • Mastering PyTorch Optimizers and Learning Rate Scheduling

    14/11/2024 · Python

  • FastAPI

    15/10/2024 · Python

  • Unveiling the Power of Unsupervised Learning in Python with Scikit-learn

    15/11/2024 · Python

  • Mastering Asynchronous Programming in FastAPI

    15/10/2024 · Python

Popular category

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