Nabeel Zewail

August 12, 2021

Streamlit is Amazing

A few months ago a friend of mine told me about this tool called Streamlit and at the time I didn't really grasp what he was saying about it nor did I give the tool much attention. A few months later we are figuring out a new BI tool at work, and I have been having some questions about how we are doing it so kicked the tires on this tool that my friend had mentioned to me. And boy do I wish I had been using Streamlit basically my entire career. It's 1 AM and I'm so jazzed about the possibilities with it.

I so often have ideas about some data project I want to do but don't have a good way of sharing or refreshing the results. I think to develop a web app but then start on the road of building a rails/Flask app and get quickly bored with the idea and have little to show beyond some random boiler plate code.

With Streamlit, it is so easy to start building a web data app and those random scripts can actually turn into something useful.Just like Jupyter made it really easy to script to analyze data on the fly with nicely rendered tables and inline charts. Streamlit does something similar but for basic data web apps. The addition of configurable user inputs also makes Streamlit such a powerful tool.

Unlike other just data viz libraries, Streamlit also allows the developer to have inputs like sliders, boxes, text, file uploads, etc which increases the possibilities tremendously. And there is hardly any boilerplate needed and the code is super intuitive.

To experiment with Streamlit, I wrote a small project to track F1 standings across the year (I guess I truly have become obsessed with F1).

The Streamlit documentation is obviously far more comprehensive and includes some great tutorials as well as examples of other projects people have built using Streamlit. I just wanted to highlight a few things I found to be wonderful about Streamlit.

#1: Really easy to setup user inputs

import streamlit as st

slider = st.slider("Season", min_value=0, max_value=10, step=1, value=1)

Just like that I have a slider that my users can select from and the variable season_dropdown is now accessible in my code. No clunky Form objects or having to figure out some JS nonsense. Really easy python code that does what you expect.

#2: Dynamic Pages

Another neat thing I found was that you can make the pages dynamic with error messages, or status updates

def get_standings(season: int, race_round: int, championship: str) -> List[dict]:
 |     standings = []
 |     latest_iteration = st.empty()
 |     progress_bar = st.progress(0)
 | 

 |     for race in range(1, race_round + 1, 1):
 |         latest_iteration.text(f"Fetching data for round {race}")
 |         progress_bar.progress(race / (race_round + 1))
 |         parsed_response = hit_standings_api(season, race, championship)
 |         standings.extend(parsed_response)
 |     latest_iteration.empty()
 |     progress_bar.empty()
 |     return standings

This is some code I wrote to pull down F1 standings for a season up to a given point (I know it sucks that I have to hit the API n times for n rounds but that is the best free API I was able to find). Because this API is a little slow, I was able to include a progress bar so my users aren't just waiting aimlessly for data to refresh but are seeing progress towards completion.

Streamlit is a great tool and I could easily seeing this becoming the new Jupyter/dbt/airflow in the data world!