# Streamlit: Data Visualization Made Easy!
Hello everyone, I am Python! Today I am bringing you a particularly fun Python library - Streamlit. Are you still worried that data visualization is too complicated? With Streamlit, you can build beautiful visualization web pages with just a few lines of code, making your data presentation work much more efficient!
## What is Streamlit?
Streamlit is a Python library specifically designed for data science that allows us to easily create interactive data visualization web applications. You can think of it as a magical tool that can turn your Python scripts into a beautiful web application with one click!
First, let's install Streamlit:
```python
pip install streamlit
Getting Started: Create Your First Streamlit App
Let’s see how to create a simple Streamlit app:
import streamlit as st
import pandas as pd
# Set the page title
st.title('My First Streamlit App')
# Add a piece of text
st.write('Hello, Streamlit!')
# Create a simple DataFrame
df = pd.DataFrame({
'Name': ['Xiao Ming', 'Xiao Hong', 'Xiao Zhang'],
'Age': [25, 28, 22],
'City': ['Beijing', 'Shanghai', 'Guangzhou']
})
# Display the DataFrame
st.dataframe(df)
Save the above code as app.py
, and then run it in the command line:
streamlit run app.py
Something magical happens! Your browser will automatically open a web page displaying the app we just created.
Add Interactive Components
The best part about Streamlit is that it provides a wealth of interactive components. Let’s look at some commonly used ones:
import streamlit as st
# Add a sidebar
st.sidebar.title('Settings')
# Slider
age = st.sidebar.slider('Select Age', 0, 100, 25)
st.write(f'You selected age: {age} years old')
# Dropdown
option = st.selectbox(
'What is your favorite programming language?',
['Python', 'Java', 'JavaScript', 'C++']
)
st.write(f'You selected: {option}')
# Checkbox
if st.checkbox('Show more information'):
st.write('Here is more information...')
Data Visualization Effects
Streamlit has great support for various data visualization libraries, such as matplotlib and plotly:
import streamlit as st
import numpy as np
import plotly.express as px
# Generate sample data
np.random.seed(42)
data = np.random.randn(100)
# Create a chart with plotly
fig = px.histogram(data, title='Data Distribution Histogram')
st.plotly_chart(fig)
# Display a line chart
chart_data = pd.DataFrame(
np.random.randn(20, 3),
columns=['A', 'B', 'C']
)
st.line_chart(chart_data)
Tip: Streamlit automatically handles the responsive layout of charts, so you don’t have to worry about adaptation issues!
Useful Tips
-
Cache Data: For computationally intensive operations, you can use the @st.cache
decorator to cache results:
@st.cache
def load_data():
# This is a time-consuming data processing operation
return data
-
Page Layout: Use st.columns
to create side-by-side columns:
col1, col2 = st.columns(2)
with col1:
st.write('This is the first column')
with col2:
st.write('This is the second column')
-
Progress Display: Add a progress bar for a better user experience:
progress_bar = st.progress(0)
for i in range(100):
progress_bar.progress(i + 1)
Small Exercises
Try to complete these small tasks:
-
Create a simple BMI calculator -
Make a simple to-do list -
Display a real-time updating data chart
Remember, the philosophy of Streamlit is: Everything is a function call. You just need to call the corresponding functions in the order you want to build the desired interface.
Friends, today’s Python learning journey ends here! Remember to get your hands on the code, and feel free to ask questions in the comments. Streamlit is truly a very friendly tool, and I believe you will quickly get the hang of it. Happy learning, and may your Python skills improve steadily!