Introduction
Streamlit is an open-source Python framework for building and sharing dynamic web applications, primarily designed for data science and machine learning projects.
It lets you transform Python scripts into interactive web apps in minutes, and build dynamic data apps with only a few lines of code.
Snowflake acquired Streamlit in 2022.
Why Streamlit
- Simple and Pythonic: Streamlit allows you to write clean, easy-to-read code in a Pythonic way.
- Fast, interactive prototyping: You can create interactive apps that let users explore your data and provide feedback quickly.
- Live editing: Your app refreshes automatically as you modify your script, giving you real-time updates.
- Free and open-source: Streamlit is completely free, and you can join its vibrant community to contribute and collaborate.
Getting Started
You can create a Streamlit app with just a single python module. For example, the following script displays the text Hello World! on the webpage:
# Filename - test.py
import streamlit as st
st.write("Hello World!")To run this streamlit app, use:
streamlit run test.py
# ALTERNATIVE
# python -m streamlit run test.pyThis starts a local web server on port 8501.
GitHub Gists
You can run a Streamlit app using the code from a GitHub Gist as well:
streamlit run https://raw.githubusercontent.com/streamlit/demo-uber-nyc-pickups/master/streamlit_app.pyPassing arguments to your app
There can be scenarios where you would want to pass some input arguments to your Streamlit app. To achieve this, you can pass your custom arguments after two dashes followed by a space.
NOTE
Make sure you add a space after the dashes, else the argument will be interpreted as an option to streamlit command. Also, if you omit the dashes, the arguments get interpreted as arguments to streamlit itself.
streamlit run test.py -- arg1 arg2Data Flow
Streamlit employs a unique data flow where the entire script is rerun from top to bottom, whenever something must be updated on the screen. This can happen in two situations:
- Whenever you modify your app's source code, and have
Run on saveenabled in Development setting. - Whenever a user interacts with widgets in the app. For example, when dragging a slider, entering text in an input box, or clicking a button.
This is covered in more detail later in this book.
