Streamlit: Visualize Data Easily and Build Your First Web App in 30 Minutes!

Streamlit: Visualize Data Easily and Build Your First Web App in 30 Minutes!

Today, I want to introduce a particularly useful Python library – Streamlit. Are you still worried about presenting your data analysis results? Are you still feeling overwhelmed by building web applications? With Streamlit, you’ll find that everything becomes super easy! I will guide you step by step to create a beautiful data visualization application with minimal code.

Why Choose Streamlit?

As an experienced Python user, I have tried many data visualization tools. However, Streamlit is the simplest and easiest to use that I have ever seen! It has these amazing features:

  • You only need to know Python, no front-end knowledge required
  • You can create beautiful interfaces with just a few lines of code
  • The page automatically refreshes after code modifications, what you see is what you get
  • Supports various data science libraries, such as Pandas and Matplotlib, which can be perfectly used

Getting Started Quickly

First, we need to install Streamlit:

pip install streamlit

After installation, let’s create our first application. Create a new app.py file:

import streamlit as st
import pandas as pd
import numpy as np

# Set the page title
st.title('My First Streamlit App')

# Add a line of text
st.write('Hello, welcome to the world of Python data visualization!')

# Create a simple data table
df = pd.DataFrame({
    'Name': ['Xiao Ming', 'Xiao Hong', 'Xiao Hua'],
    'Age': [25, 22, 28],
    'City': ['Beijing', 'Shanghai', 'Guangzhou']
})
st.write('This is a simple table:')
st.write(df)

Running this application is super easy, just enter the following command in the terminal:

streamlit run app.py

Tip: The first run may take a few seconds, and the browser will automatically open the application page.

Adding Interactive Elements

Static displays are not enough; let’s add some interactive effects:

# Add a slider
age = st.slider('Select an age', 0, 100, 25)
st.write(f'You selected the age: {age} years')

# Add a select box
city = st.selectbox(
    'Select a city',
    ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen']
)
st.write(f'You selected the city: {city}')

# Add a file upload feature
uploaded_file = st.file_uploader("Upload a CSV file")
if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    st.write(df)

Data Visualization

Let’s create some cooler charts:

# Generate random data
chart_data = pd.DataFrame(
    np.random.randn(20, 3),
    columns=['Stock A', 'Stock B', 'Stock C'])

# Line chart
st.line_chart(chart_data)

# Bar chart
st.bar_chart(chart_data)

Tip: Streamlit supports various chart types and can directly use charts from libraries like Matplotlib and Plotly!

Useful Tips

  1. Use st.sidebar to create a sidebar:
with st.sidebar:
    st.title('Sidebar Options')
    option = st.radio(
        'Select content to display',
        ['Data Table', 'Chart', 'Analysis']
    )
  1. Use st.cache to cache data and improve efficiency:
@st.cache_data
def load_data():
    return pd.read_csv('large_file.csv')

Hands-On Practice

Try adding the following features to your application:

  1. Add a multi-select box to let users choose which data columns to display
  2. Add a button that generates random data when clicked
  3. Try using st.columns() to create a multi-column layout

Friends, today’s Python learning journey ends here! Remember to code, and feel free to ask questions in the comments. Streamlit is truly a fantastic tool, and I believe you will soon be able to create your own data visualization applications! Wishing everyone a happy learning experience, and may your Python skills improve steadily!

Leave a Comment