Streamlit — Quickly Build a Web App Using Python
By: Shashvat G
Tags: Tech, Python, Web App Development, Data Science, Streamlit

Streamlit is a super useful tool to develop an interactive web application in a couple of function calls written in python. It lets you create apps for your machine learning models using fairly simple python code. If you can write scripts in python, you can quickly embrace streamlit by getting acquainted with a couple of streamlit functions. It’s really that simple!

Photo by Shahadat Rahman on Unsplash

One of the major plus points I have witnessed in Streamlit is its hot-reloading feature. You can see changes in the app as you write and save your python file.

Installation

Alright, the pre-requisites for this is you already have python 3.6 or later running on your system. You can simply pip install using below command:

pip install streamlit

I would install streamlit in a separate virtual environment to avoid any version compatibility issues with other applications.

Once you have it up and running, you can try below command:

Conda activate streamlit streamlit hello

If it displays Local and Network URLs and then redirects you to your web browser, you have successfully installed streamlit in your system and you are ready to develop your first application.

Developing your First Streamlit App

Let’s develop a simple web app that reads and visualize a pandas data frame.

Step 1: Open your preferred IDE and start by importing python libraries.

import streamlit as st

import pandas as pd

import numpy as np

st.title(‘Yelp Reviews’)

df = pd.read_csv(“yelp.csv”)

if st.checkbox(‘Show dataframe’):

st.write(df)

Step 2: Set your application title by using St.title (Given you imported streamlit as st)

Step 3: Use pandas to read your CSV file — change name according to your requirement.

Step 4: Now, let’s utilize a checkbox for hiding and showing a specific section as you check/uncheck.

Step 5: St.write is a magic command which can be exploited to display markdowns, texts, or even data frames on the web app.

Now that you have a script, save it as hello_streamlit.py Next step is to run your streamlit app. The command to run an app is streamlit run followed by the python script name.

streamlit run hello_streamlit.py

Here is a screencast for the same:

One of the major highlights of streamlit is it’s caching feature. It lets you cache that makes it fast when you make a small tweak and then try to rerun, you will witness the power of @st.cache.

Word of Advice:

When you have too much to show on the UI, st.sidebar and st. checkbox can be handy for a clean UI look.

It’s always a good idea to have browser windows of the code and the app to be open side by side so that changes can be seen in real-time.

Use st.map for excellent and interactive visualization. (This is amazing!)

Alternatives:

For R users — R shiny is a good option to develop web apps quickly.

While Streamlit works completely in Python, R and Plotly Dash is another popular choice that supports Julia as well as python.

You can always use Django/Flask if you like to work in HTML/CSS.

Conclusion

In this post, We’ve covered mainly interactive data frames and talked about quickly developing a web app using python scripting. Streamlit is bliss for Data scientists as we can build machine learning web applications and demonstrate the models to stakeholders and decision-makers. Streamlit also allows maps, modification of existing elements, and showing progress. You can find excellent documentation on Streamlit here.

I’d love to hear your thoughts about Streamlit, Machine learning, and model deployment in the comments below.

If you found this useful and know anyone you think would benefit from this, please feel free to send it their way.