DEV Community

algieba
algieba

Posted on

[Python Tips] Streamlit: A Rapid Prototyping Tool for Python

This article aims to quickly get you started with the Python streamlit library, including installation, input data, chart drawing, basic controls, progress bars, and free deployment.

Streamlit, as the official website claims, helps you build and share data applications faster! It is well-suited for data processing, data presentation, machine learning prototypes, AI prototype demonstrations, and even simple web applications. With low difficulty in getting started, simple deployment, and free public access to Streamlit Sharing provided by the official platform.

Quick Introduction to the Features of Streamlit

  • High Integration with Data Science Tools. Libraries such as Matplotlib, Pandas, Plotly, Altair, etc., are well-adapted in streamlit, allowing direct display of charts and data generated by these libraries in applications, and Streamlit's presence can be seen in many related academic papers.
  • Easy Deployment. You can complete the deployment of applications with a simple command, running anywhere, locally, on cloud servers, or directly hosted on the official Streamlit Sharing.
  • Real-time Updates. Streamlit applications update in real-time, automatically refreshing whenever the data changes.

0x00 Installation

  • Ensure your Python version is above 3.8
  • Install directly with pip pip install streamlit OK, the installation is complete. You can use the command streamlit hello

streamlit hello

Whoosh~ The built-in HelloWorld Demo of streamlit pops up, you can scroll through the demo on the left.

0x01 Before Coding

  • Before starting to code, let's talk about an important point -> how to start a streamlit app script

Unlike general Python scripts, which can be run with python xxx.py, streamlit requires python -m streamlit run xxx.py to run. (The script following streamlit run can even be a GitHub repository link, for example: streamlit run https://github.com/Algieba-dean/ZhuGeHorary/blob/master/ZhuGeHorary_APP.py)

  • Streamlit will monitor changes in real-time, and code changes will be synchronized to the deployed App upon saving.

0x02 Displaying Data

st.write()

  • st.write(), if you don't know what to use, just use it. Even with st.table(), st.dataframe() there are no special requirements.
import streamlit as st
import pandas as pd
st.write("Display table:")
st.write(pd.DataFrame({
    'Column A': [1, 2, 3, 4],
    'Column B': [101, 202, 303, 404]
}))
Enter fullscreen mode Exit fullscreen mode

OK, now we've learned the simplest data presentation.

dataframe_show

0x03 Input Control

st.text_input

  • The simplest input control, for text input.
text = st.text_input("Enter some text")
st.write(text)
Enter fullscreen mode Exit fullscreen mode

text_input_example

OK, with these two, you can complete a large part of the tasks.

0x04 Advanced Controls

  • st.number_input for numerical input
number = st.number_input("Enter a number between 0-100", min_value=0, max_value=100, step=1)
Enter fullscreen mode Exit fullscreen mode

number_input_example

  • st.slider for slider
slider_number = st.slider(f"Try sliding to the number: {number}", min_value=0, max_value=10000, value=[0, 500])
Enter fullscreen mode Exit fullscreen mode

slider_example

  • st.file_uploader for file uplaoding
uploaded_file = st.file_uploader("upload a data file", type=['csv', 'txt'])
Enter fullscreen mode Exit fullscreen mode

upload_file_example

0x05 Progress Bar

  • st.progress
import  time
st.write("Calculation simulation in progress...")

latest_iteration = st.empty()

bar = st.progress(0)

for i in range(100):
    latest_iteration.text(f"now it's {i+1}")
    bar.progress(i + 1)
    time.sleep(0.1)

st.write("Simulation calculation complete!")
Enter fullscreen mode Exit fullscreen mode

progress_bar_example

0x06 Application Deployment

  • Upload your code to Github and set it as a public repository
  • Log in to streamlit share with your GitHub account
  • Click on Create app at the top right
  • Click on Deploy a public app from GitHub and then Deploy now
  • Fill in the repository information correctly, script filename, configure a usable URL
  • Click Deploy
  • After waiting, your app will be online

deploy app

End

Welcome to leave your app link here

Top comments (0)