As requested, I will reorganize this blog post about Streamlit into a clear and easy-to-understand Python tutorial:
In data analysis work, we often need to present analysis results in an intuitive way. The traditional method is to save the results as charts or reports, but wouldn’t it be better to create an interactive web page? The Streamlit library that I will introduce today allows us to quickly create beautiful data visualization web applications with simple Python code.
1. Introduction to Streamlit and Installation
Streamlit is an open-source Python library specifically designed for creating web applications for data analysis and machine learning. Its main feature is its ease of use; you can develop a professional data display interface as long as you know Python.
The installation method is very simple, just one command:
pip install streamlit
2. Creating Your First Streamlit Application

Let’s start with the simplest example:
import streamlit as st
st.title('My First Streamlit App')
user_input = st.text_input("Please enter some text")
st.write('The content you entered is:', user_input)
Save the above code as<span>app.py</span>
, then run it in the terminal:
streamlit run app.py
Tip: Streamlit will automatically open your default browser and display the application interface. If it doesn’t open automatically, you can manually visit the address displayed in the terminal (usually http://localhost:8501).
3. Adding Interactive Components
Streamlit provides a rich set of interactive components to make our applications more dynamic:
import streamlit as st
import pandas as pd
import numpy as np
# Create sample data
df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'])
# Add a slider
slider_val = st.slider('Select a threshold', -2.0, 2.0, 0.0)
# Filter data based on slider value
filtered_df = df[df['A'] > slider_val]
# Display data table
st.write('Filtered data:')
st.write(filtered_df)

Note: The Streamlit page will automatically refresh when the data changes, so you don’t need to manually handle the update logic.
4. Data Visualization Display
Streamlit supports various data visualization libraries, such as using matplotlib to draw charts:
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np
# Create a chart
fig, ax = plt.subplots()
x = np.linspace(0, 10, 100)
ax.plot(x, np.sin(x))
# Display the chart in Streamlit
st.pyplot(fig)
5. Practical Tips
Cache Computation Results

For time-consuming data processing operations, you can use<span>@st.cache_data</span>
decorator to cache results:
@st.cache_data
def load_data():
# Assume this is a time-consuming data loading operation
df = pd.read_csv("large_file.csv")
return df
Sidebar Layout
Use<span>st.sidebar</span>
to create a control panel on the left side of the page:
# Add controls in the sidebar
selected_option = st.sidebar.selectbox(
'Select display item',
['Chart', 'Data Table', 'Statistics']
)
Conclusion
Streamlit greatly simplifies the development process of data applications, and its advantages include:
-
No front-end development experience is needed; pure Python code is sufficient -
Built-in rich components and layout options -
Supports mainstream data analysis and visualization libraries -
Automatically handles interface refresh and data updates
Readers are encouraged to start with simple data displays and gradually add interactive features to familiarize themselves with the various components of Streamlit. Hands-on practice is the best way to learn!
Previous Reviews
1.
2.
3.