DEV Community

Robertino
Robertino

Posted on • Originally published at auth0.com

Introduction to Streamlit and Streamlit Components

Streamlit is an open-source app framework for Machine Learning and Data Science teams.

In this article we will show you how to build Streamlit apps and custom Streamlit Components, with the end goal of implementing Auth0 authentication.


TL;DR: Streamlit is an open-source app framework for Machine Learning and Data Science teams. You can create beautiful data apps in hours. All in pure Python. Streamlit was released in October 2019 and was recently acquired by Snowflake. There's huge excitement about it in the Data Science community. It's not just for Data Science, though. With its component extensibility architecture, you can build and integrate most kinds of web frontends into Streamlit apps. My experience with Streamlit can be verified on the official page of Streamlit Creators.

Don't take my word for it that Streamlit is extremely popular and worth taking a serious look at. Here's a chart I found showing the extremely rapid adoption rate of Streamlit compared to other similar tools.

Streamlit Rapid Adoption Rate

Why is that so?

Streamlit focuses on simplicity

"How can we make a machine learning script and convert it into an app as simple as possible, so that it basically feels like a scripting exercise?", Inventor of Streamlit, Adrien Treuille (Ph.D.)

Streamlit is a single Python package that you install through pip, that gives you a set of functions which:

  • Can be interleaved into an existing ML code script
  • Essentially making the ML code parameterizable
  • Does a little bit of layout, and
  • Magically turns your ML code into a beautiful app

Inspiration is drawn from Jupyter, ipywidgets, R-Shiny, Voila, React, etc., but more as a guiding light than a software architecture. There are significant technical differences in the implementation of Streamlit, which is based on a declarative data flow model, not wiring callbacks.

Python frameworks such as scikit-learn, spaCy, Pandas, and various visualization frameworks such as Altair, Plotly and Matplotlib all work seamlessly with Streamlit.

Streamlit supports many uses

  • Every single data analysis team needs to create apps. They're a focal point - like the bonfire of the team. It's where team members get together and communicate.
  • Apps are a crucial part of the ML (data analysis) workflow, especially in a non-trivial project.
  • This applies not only to internal apps. Machine learning researchers and data scientists need to build apps for external consumption too. Other teams need to consume models in various different ways, and it ought to be much easier to build the required but different application layers to do that.

I'm a big fan of Streamlit and use it extensively for serious work and play. In a previous job, I had developed several in-house apps that I needed to share externally with clients and colleagues, so adding security and authentication features was imperative. As you'll see in this article, Streamlit's embedded components, extensibility architecture, and native session state management will help realize this security objective.

Here is the GitHub repository for this article.

Getting Started with Streamlit

The core Streamlit documentation, discussion forum, and examples gallery are very good. This article will by no means replace them but will serve as an alternative place you can start learning about Streamlit with the specific aim of integrating with Auth0. To get a broader appreciation of Streamlit check out these links: API Docs | Gallery | GitHub | Discussion Forum | Discord Server.

Quick installation

To use Streamlit, you'll need Python 3.5 or above. I use the Anaconda Python distribution (conda) and Visual Studio Code IDE with Python extensions, which works well with conda environments.

  • Open a conda console window
  • If necessary, run conda activate for the env in which you want to install package requirements. See managing conda environments.
  • Run one of these commands in the console window:

$ pip install --upgrade streamlit
# and
$ pip install -r requirements.txt

Enter fullscreen mode Exit fullscreen mode

The latter command ensures that all necessary packages for this article are in your Python environment. The requirements file is in the GitHub repository for this article.

Note: Linux and Mac users, please remove the windows-curses package from the requirements file. The curses package comes with the Python standard library. In Linux and Mac, the curses dependencies should already be installed, so there are no extra steps needed. On Windows, you need to install one special Python package, windows-curses, available on PyPI to add curses support.

Read more...

Top comments (0)