Streamlit is the first application development framework specifically for machine learning and data science teams. It’s the fastest way to develop custom machine learning tools, and you could argue that it aims to replace Flask in machine learning projects and help machine learning engineers develop user interaction tools quickly.

1, Hello world

Streamlit applications are Python scripts with no implicit state that you can refactor using function calls. As long as you can write Python scripts, you can develop Streamlit applications. For example, the following code prints Hello,world! :

import streamlit as st
st.write('Hello, world! ')
Copy the code

The results are as follows:

2. Use UI components

Streamlit treats components as variables, there are no callbacks in Streamlit, and every interaction is simply returned to ensure clean code:

import streamlit as st
x = st.slider('x')
st.write(x, 'squared is', x * x)
Copy the code

The results are as follows:

3. Data reuse and computation

What if you want to download a lot of data or run complex calculations? The key is to reuse data safely. Streamlit introduces caching primitives that allow Steamlit to apply safe and easy reuse of information. For example, the following code only needs to download data from Udacity’s self-driving car project once to get a simple, fast application:

import streamlit as st
import pandas as pd
# Reuse this data across runs!
read_and_cache_csv = st.cache(pd.read_csv)
BUCKET = "https://streamlit-self-driving.s3-us-west-2.amazonaws.com/"
data = read_and_cache_csv(BUCKET + "labels.csv.gz", nrows=1000)
desired_label = st.selectbox('Filter to:'['car'.'truck'])
st.write(data[data.label == desired_label])
Copy the code

The results are as follows:

In a nutshell, Streamlit works as follows:

  • The entire script is executed from beginning to end for each user interaction
  • Streamlit assigns values to variables based on the state of the UI component
  • Caching allows Streamlit to avoid double requests for data or double calculations

Or look at the following image:

If that doesn’t make sense, you can try Streamlit!

$ pip install –upgrade streamlit

$ streamlit hello

You can now view your Streamlit app in your browser.

Local URL: http://localhost:8501

The Network URL: http://10.0.1.29:8501

This automatically opens the local Web browser and accesses the Streamlit application:

4. Example: Automated driving dataset tool

The Streamlit app below allows you to perform semantic searches, visualize manual tagging, and run a YOLO target detector in real time across Udacity’s autonomous vehicle photo dataset:

The entire application is just 300 lines of Python code, mostly machine learning code. There were actually only 23 Streamlit calls. You can try running it yourself:

$ pip install --upgrade streamlit opencv-python
$ streamlit run https://raw.githubusercontent.com/streamlit/demo-self-driving/master/app.pyCopy the code