Hello everyone, I am Hao Ge! Today I want to introduce you to a very useful Python library – Streamlit. Are you still struggling with how to display your data analysis results? Or do you want to quickly build a beautiful web application but don’t know front-end development? Don’t worry, with Streamlit, these are no longer problems!
1. What is Streamlit?
Streamlit is an open-source Python library that allows us to create beautiful data applications using only Python. It is like a magical toolbox that makes complex data visualization and interactive features super simple.
2. Installation and Quick Start
First, we need to install Streamlit:
python run copy
pip install streamlit
Let’s write the simplest Streamlit application:
python run copy
import streamlit as st
st.title('My First Streamlit App')
st.write('Hello, World!')
Tip: Save it as app.py, then enter streamlit run app.py
in the command line to see the effect!
3. Core Components of Streamlit
3.1 Text Display
python run copy
import streamlit as st
st.title('This is a Title')
st.header('This is a Level 1 Header')
st.subheader('This is a Level 2 Header')
st.text('This is normal text')
st.markdown('**This is Markdown text**')
3.2 Data Display
python run copy
import streamlit as st
import pandas as pd
import numpy as np
# Create sample data
df = pd.DataFrame({
'Name': ['Xiao Ming', 'Xiao Hong', 'Xiao Hua'],
'Age': [18, 19, 20],
'Score': [90, 85, 95]
})
st.write('This is a table:')
st.dataframe(df)
3.3 Interactive Components
python run copy
import streamlit as st
# Input box
name = st.text_input('Please enter your name')
# Slider
age = st.slider('Please select your age', 0, 100, 25)
# Button
if st.button('Click Me'):
st.write(f'Hello, {name}! You are {age} years old.')
4. Drawing Charts
python run copy
import streamlit as st
import pandas as pd
import plotly.express as px
# Create sample data
data = pd.DataFrame({
'Month': ['January', 'February', 'March', 'April'],
'Sales': [100, 120, 80, 150]
})
# Draw a chart
fig = px.bar(data, x='Month', y='Sales', title='Monthly Sales Statistics')
st.plotly_chart(fig)
5. Practical Tips
Layout Tips: Use columns to achieve side-by-side display
python run copy
import streamlit as st
col1, col2 = st.columns(2)
with col1:
st.header('Left Side Content')
st.write('This is the content on the left.')
with col2:
st.header('Right Side Content')
st.write('This is the content on the right.')
Tips:
-
Remember to frequently use st.help()
to check the component usage instructions -
After saving the file, Streamlit will automatically detect changes and refresh the page -
Using st.cache
decorator can cache data to improve application performance
Conclusion
Today we learned the basic usage of Streamlit, it really is a very friendly tool! With just a few lines of Python code, we can create professional data applications. We learned:
-
The basic concepts of Streamlit -
The usage of core components -
Data display and chart drawing -
Practical layout tips
Friends, today’s Python learning journey ends here! Remember to code yourself, and feel free to ask me in the comments if you have any questions. I wish you all happy learning and continuous improvement in Python!