Notebook Review: Streamlit
Good for visually clean data apps for clients who don’t want to go playing with your code
So first off, Streamlit isn’t exactly a notebook platform. It does, however, inherit key ideas from literate programming just like a computational notebook platform: you write Markdown to narrate what’s going on in a program, and you can easily render data tables and plots in an interactive document. For its intended use case, Streamlit is absolutely fantastic, and a refreshingly different take in an otherwise crowded space of today’s literate programming tools that tend to stick close to mimicking Jupyter notebooks.
Back in 2017 I ran a study where we interviewed regular (professional) users of Jupyter Notebooks to better characterize the use cases and practices people have for notebooks. We ended up with 2 major classes of use cases:
- You are using a notebook as a scratchpad.
- You are using a notebook to share and inform others about a data analysis.
Streamlit really hones in on 2. The welcome message of its docs reads :
“Streamlit is an open-source Python library that makes it easy to create and share beautiful, custom web apps for machine learning and data science.”
The keyword here is “web app”. When I started making my first project with Streamlit, an analytics app for my own research data, I was a little bit shocked at first over the lack of displayed code — it felt like someone ripping away my security blanket, I really like to see code! My app looks like this:
To write an app with Streamlit is to just write a normal python file in your editor of choice, here with my beloved Visual Studio Code (I’m a front-end web developer at heart). Just like a nice web app development setup, I place my code editor side-by-side with a web browser that continuously renders my app. Streamlit will automatically detect when your code has changed and offer on the web browser side to re-run the app to update it. So (thank goodness 🎉) no mucking around in the terminal to run gross build processes. Once you start Streamlit, here with
streamlit run demo.py, it just handles everything in the background to let you focus on code.
So why would you want to use Streamlit? Let’s return to our use case research.
In our study, the use case “You are using a notebook to share and inform others about a data analysis” actually broke down further into some distinctly different flavors of computational notebook documents that professionals were creating for sharing. I’ll summarize these:
- You’re a data scientist sharing an analysis — in its gory details — with your team. This kind of notebook has Markdown, plots, tables, etc. but the code itself is front and center too so that you and your colleagues can dissect the details of your approach.
- You’re an instructor creating a guided example of an analysis. This kind of notebook will have more Markdown describing what’s going on, more images, and often more interactive widgets that allow your audience to “play” with parameters of the analysis for maximal learning from the notebook. How much code there is displayed in the notebook really depends on how code-savy your audience is and how much you think the details of the code will help their learning. I’ve often seen instructors “hide” much of their code by putting the bulk of their code in a seperate python file that they then call as a library within the notebook itself.
- You’re creating an interactive tool to let others run your analysis on their own data. Seriously, people are using Jupyter Notebooks as full-on web apps. As a web developer I was surprised to find how common this one is, but it’s actually a stellar use for notebooks if your audience is not code-literate and a highly-polished app isn’t needed. I spoke with a data scientist from a local Parks and Recreation government agency (I’m making up the exact agency to protect her anonymity) who created an analysis in a notebook to help Parks and Rec understand climate patterns at their parks. Her audience doesn’t know how to code, they just want to pop in their data and see the charts. Her agency also didn’t have the budget to create a formal web app. The solution? Give Parks and Rec a good old Jupyter notebook and tell them to replace the first code cell’s input text with their own data, then run the notebook. Elegant? Nope, but it works. I saw a similar case with a scientist, who created a notebook to allow their clients to play around interactively with the analysis without editing code. For these use cases, showing code may not be desired at all. Some users said that showing code actually distracted or intimidated their clients or gave their clients the (wrong) impression that the notebook was in-progress, rather than finished, work.
So basically I love to see code, you love to see code, but maybe your audience doesn’t want to look at (all) your source code.
Streamlit is really excellent for case 2 & 3. You can show selected code in your rendered app with Streamlit if you really want to, but the default state is hiding code. A data scientist can write super basic python to get an easy data web app, without having to fuss with full web app development and without having code on display as in a standard notebook. Of course, lots of notebook platforms do have code hiding options, but for a polished professional look, Streamlit does a great job in its render by not looking like a notebook “try to be” a web app.
What would you not use Streamlit for? Remember use case 1 at the very beginning of this article? If I need to do significant hacking to figure out the right pandas + plotting syntax to get a chart I want, I prefer to go back to my Jupyter Notebook for fast messy iteration. The “scratchpad” use case is still very much core to my notebook usage. Once I worked the right syntax out, I copy the finished code snippets to my Streamlit project to render a polished data app.