Build your first Data Science app with Streamlit in 5 lines of code.

Mike Salem
Analytics Vidhya
Published in
4 min readApr 22, 2021

--

Build this app in 5 lines of code!

As a data scientist, I am constantly evaluating new tools and techniques to enhance my workflow. Having started my career as a software engineer, I am comfortable installing programs and setting up environments. One of my biggest challenges has always been, “How do I show the insight from my work in a way that is fast and easy?”

Jupyter Notebooks are easy to use, but end-users cannot gain any additional insight other than what is provided. Plotly Dash makes beautiful dashboards and apps, but it requires a lot more understanding of callbacks, stylizing, and containers than I really want for prototyping or quickly sharing insight. Recently, I started using Streamlit and it has made it easy to share insight quickly.

Why use Streamlit?

Streamlit was founded by a group of data scientists. Why does that matter? Well, if anyone knows the struggles of a data scientist, it’s a data scientist. Streamlit allows me to focus on getting to the insight and capabilities I want to share without getting bogged down in app/dashboard development.

What is Streamlit?

According to their website, Streamlit is an open-source app framework for Machine Learning and Data Science teams. Streamlit provides an easy way to build apps in a matter of hours that can be run and shared locally, privately hosted, or published through their platform. Streamlit can be installed via pip in python by running the following command:

pip install streamlit

In a few minutes, you will have Streamlit installed and ready to use.

Let’s build an app!

To showcase just how easy it is to build an app, I am going to build a fully-functional Streamlit app in a few lines of code (in minutes). Let’s build it together!

For this project, we are going to build a visual for Tasmania’s “Service Tasmania” locations. The app will consist of points on a map for their locations .This data set is available for free and contains city name, address, latitude and longitude and more. For our purpose, we will focus on latitude and longitude from this data set.

First, let’s make sure we have the packages we need installed

# Standard Imports
import io
import requests
# Third-Party Imports
import pandas as pd
import streamlit as st

If you don’t have pandas installed you can do pip install pandas like you did for Streamlit.

Let’s get into the code. What we want to do is grab the Service Tasmania data set from data.gov.au, and put that into a dataframe. The python module requests will allow you to send HTTP requests so you can grab the csv file from their website.

url="https://data.gov.au/dataset/648d7a00-96bb-40b5-ad8e-5ca51f90f441/resource/b8e618b5-2ec4-46b7-a1ce-e1fafedce05f/download/stoshops.csv"result=requests.get(url).content

This will get us back the contents of the csv stored as bytes. We want to take these contents and put them into a format to create a Pandas DataFrame. We will have to decode the bytes using utf-8, stream in the information and turn it into a dataframe and rename Latitude to lat and Longitude to lon (alternatively, you could just lowercase the column names):

df=pd.read_csv(io.StringIO(result.decode('utf-8'))).rename(columns = {"Latitude" : "lat", "Longitude": "lon"})

Now the magic happens, we want to create the map and put the points on the map. Sounds like a lot of work, but really it’s just one line!

st.map(df)

Putting it all together looks like this:

app.py

Save this file as app.py so we can run the app.

Running your app

To run your app, navigate to the directory where you saved your file and type

streamlit run app.py

Congratulations! You made your first app!

In a few seconds you should see your browser open and your app appear. In your terminal, you should notice there are two links: localhost (only accessible on your computer) and network URL(one accessible to anyone on your network provided your firewall allows it).

That’s it!

Recap and next steps

Streamlit is a powerful framework that allows you to quickly build web apps to share insight and more without having to focus on a lot of web development. We covered some basics to get a project up and running, but there is much more you can do including adding UI elements (buttons, checkboxes, etc.) or using other packages and plotting frameworks (plotly, matplotlib, seaborn, etc.). There is a lot more to learn so have fun and keep exploring!

--

--

Mike Salem
Analytics Vidhya

Assoc. Director of Data Science @gilead | Robotics Instructor @brandeisuniversity | Mathematician interested in multi-agent systems and self-organizing behavior