Improved Caching Produces a 5000x Performance Boost on Streamlit Dashboards | by Marcin Kozak | Jan, 2024

Editor
2 Min Read


PYTHON PROGRAMMING

Learn how to use native Python caching to implement blazing-fast dashboards.

Photo by Myriam Jessier on Unsplash

I started working with it a few months ago, and I must say have really enjoyed it. I’ve worked with R Shiny and Python dash dashboards, but Streamlit beats the both of them in terms of coding simplicity. I find it surprisingly easy to use. While I have never particularly liked implementing dashboards (I’m not sure why), Streamlit has actually changed that; it can be fun!

However, there are some drawbacks to Streamlit. One is that Streamlit code can often look like poor Python code, as dashboards are usually implemented without functions, just as a list of lines of code. Some functionalities can be moved to functions or classes, but sometimes it’s better not to do that.

Another disadvantage of Streamlit is that its dashboards can be slow, for several reasons. First of all, every time the user interacts with the dashboard, Streamlit re-runs the entire dashboard script, and this can mean rerunning, for instance, calculations, data reads, graphs and charts.

In this article, we will discuss how to make Streamlit data faster using caching. I am not going to tell you what’s already known about caching. Instead, I am going to show you a caching method that is much more performant than the native Streamlit approach.

Before reading on, it’s good to know how caching works in Python. You can learn about caching and memoization from my recent Towards Data Science article:

Today, I will show you that the native Streamlit data cacher, the st.cache_data decorator, offers performance improvements, but then I’ll show you that these improvements can be improved — and by improved I mean improved a lot. To achieve this, however, you need to know how to do it, and the main aim of this article is to explain this.

Share this Article
Please enter CoinGecko Free Api Key to get this plugin works.