An App-solute Delight: Streamlit

Exploring the uber-cool tool that helps build data apps


5 min read

An App-solute Delight: Streamlit

A user-friendly tool that helps deploy any machine learning model and any Python project with ease by turning data scripts into shareable apps in minutes? Yep, it is true. And it’s here!

Video on Getting Started with Streamlit

Getting started with Streamlit | Build your first Data/ML application by Anuj Syal

Decoding Streamlit

Created by Adrien Treuille, Thiago Teixeira, and Amanda Kelly, Streamlit is an open-source Python library that enables you to effortlessly build beautiful, custom web apps for machine learning and data science without worrying about the front end, for free. Astutely developed keeping the data scientists and ML engineers in mind, this tool allows them to create an interactive environment making it easier to share their model and present it to their colleagues and customers. The values can be altered as per the inputs provided, with the changes and results all happening interactively, in real-time, all thanks to its hot-reloading feature. Predominantly Markdown and Python are used in Streamlit, but HTML and CSS are also supported. Free and easy to install, Streamlit brings your ideas to life on the internet in the form of web apps.

What makes Streamlit stick out?

The main idea behind the tool is to make data app creation as easy as writing a Python script. And that’s exactly what the creators have managed to achieve.

  1. It is interactive and aesthetically pleasing particularly to your clients
  2. If you know how to write Python scripts, you are all set for creating cool apps

    Instant deployment from your GitHub repository

How to Streamlit?

One just needs to type:

pip install streamlit

In your terminal/command prompt and it is ready to use (provided you have pip installed first). Once Streamlit is successfully installed, run the python code given below and if you don’t get an error, it automatically means Streamlit is successfully installed. Open command prompt or Anaconda shell and type:

streamlit run

Note: To avoid any version compatibility issues with other applications, install Streamlit in a separate/independent virtual environment.

Picture 2.jpg

Max Duzij on Unsplash

Time to Test Streamlit: My Demo Web App

While it’s one thing to call this tool fairly easy and convenient, it’s another to put it to test. Let us develop a simple web app that reads and visualizes a pandas data frame.

Step 1: Import Python libraries add the title+body of the app

import streamlit as st
import pandas as pd
import numpy as np
st.title(‘App by Anuj’)
st.write('This app walkthroughs on creating your first data app with streamlit. It demonstrates how easy it is now for data scientists to develop and deploy quick prototypes.')

Step 2: Read data into dataframes

# simly read a csv in folder in pandas dataframe
df = pd.read_csv('game_of_thrones_battles.csv')

Step 3: Include interactive elements

#Use sidebar
option = st.sidebar.selectbox(
    'Which number do you like best?',
     df['first column'])

'You selected:', option

Step 4: Create your first chart

chart_data = pd.DataFrame(
     np.random.randn(20, 3),
     columns=['a', 'b', 'c'])


Step 5: Create a map with plots

map_data = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [37.76, -122.4],
    columns=['lat', 'lon'])

Step 6: Add a progress bar

# Add a placeholder
latest_iteration = st.empty()
bar = st.progress(0)

for i in range(100):
  # Update the progress bar with each iteration.
  latest_iteration.text(f'Iteration {i+1}')
  bar.progress(i + 1)

Step 7: Save and run

Now that we are done with a simple script, it’s time to save it. I am going to save it as Run from terminal (Make sure the pip requirements are installed)

streamlit run

(Command streamlit run followed by your Python script name)

And there you have it! Your first demo web application. Feel free to explore other commands, add sidebars, progress bars, columns and other iterations especially when you have too much to show on the UI but want a cleaner look.

Streamlit’s caching feature is one of its major highlights. The ability to cache makes it relatively faster especially when you are making small tweaks and then trying to rerun.

Note: I would suggest my fellow data scientists have their browser windows of the code and the app opened side by side for efficient and faster results as the changes can be seen in real-time.

Looking at streamlit competitors

Whilst Streamlit works entirely in Python, R and Plotly Dash is another popular choice that supports Python as well as Julia. For R users especially, R shiny is a great option to develop web apps quickly.

My Thoughts: Is Streamlit Really Lit?

Streamlit is a blessing for data scientists. There’s no two ways about it. It not only helps them to build ML web applications, but also conveniently share and demonstrate their models to stakeholders, customers and colleagues especially if they are non-technical and/or not well-versed with programs or scripts. It has become a de-facto choice of ML engineers today, enabling them to quickly build and share proofs-of-concept, while giving them an option for quick tweaks here and there. Streamlit is aesthetically pleasing and user-friendly that can be learned with zero to minimal effort by anyone who is already familiar with Python scripting. Streamlit is feature-rich and the ease with which it enables one to deploy models is beyond commendable. It is simple. And it is outstanding!