Create Stunning Data Dashboards with Streamlit

Hello everyone! Today I want to introduce you to a particularly useful Python library – Streamlit. As a data visualization enthusiast, I find this library to be a magic tool for creating data dashboards! You don’t need to understand front-end development or know JavaScript; as long as you know basic Python, you can easily create professional-level data presentation pages.

What is Streamlit?

Streamlit is an open-source Python library designed for quickly building web applications for data science and machine learning. Its slogan is “The fastest way to turn data scripts into shareable web apps.” Honestly, when I first used it, I was shocked by its simplicity and directness!

Installation and First App

First, we need to install Streamlit with just one command:

pip install streamlit

Let’s create our first Streamlit app. Create a new file app.py and enter the following code:

import streamlit as st

st.title('My First Streamlit App')
st.write('Hello, World!')

After saving, run it in the command line:

streamlit run app.py

Immediately, a webpage will open in your browser! Isn’t it super simple?

Add Various Cool Components

Streamlit provides many out-of-the-box components, let me showcase a few of the most commonly used:

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

# Add title and text
st.title('Data Display Panel')
st.header('This is a Level 1 Title')
st.subheader('This is a Level 2 Title')

# Display data table
df = pd.DataFrame({
    'Name': ['Zhang San', 'Li Si', 'Wang Wu'],
    'Age': [25, 30, 35],
    'City': ['Beijing', 'Shanghai', 'Guangzhou']
})
st.dataframe(df)

# Add chart
chart_data = pd.DataFrame(
    np.random.randn(20, 3),
    columns=['a', 'b', 'c'])
st.line_chart(chart_data)

# Add interactive component
option = st.selectbox(
    'Which city do you like?',
    ['Beijing', 'Shanghai', 'Guangzhou'])
st.write('You selected:', option)

Tips

  1. Streamlit automatically detects file changes and refreshes the page, no need to restart manually.
  2. Using st.cache decorator can cache data and improve application performance.
  3. Streamlit supports Markdown syntax, making it easy to add formatted text.

Showcase Practical Features

Let’s see how to add some more practical features:

import streamlit as st

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

# Sidebar
st.sidebar.header('Sidebar Options')
user_input = st.sidebar.text_input('Enter your name')
st.sidebar.write(f'Hello, {user_input}!')

# Display map data
map_data = pd.DataFrame(
    np.random.randn(1000, 2) / [50, 50] + [39.9, 116.4],
    columns=['lat', 'lon'])
st.map(map_data)

Deploying the App

Streamlit apps can be easily deployed to the cloud. I recommend using Streamlit Cloud, which offers free hosting services. You just need to push your code to GitHub and then link the repository on Streamlit Cloud.

Summary

Streamlit is truly an addictive tool! It allows us to:

  • Quickly build data visualization applications
  • Focus on data analysis rather than front-end development
  • Share and deploy applications easily

I recommend starting with a simple project, such as creating a data exploration panel or a visualization report. Once you get started, you’ll find that making data dashboards is so easy!

Practice Tasks

  1. Try making a panel that displays real-time weather data
  2. Create a simple stock data analysis tool
  3. Make a visualization app for personal expense statistics

Remember, the most important thing about learning programming is to practice hands-on. Come and try using Streamlit to create your first data dashboard!

If you encounter any issues while learning, feel free to leave a comment for discussion. In the next issue, we will explore how to create more advanced data analysis applications with Streamlit, stay tuned!

Create Stunning Data Dashboards with Streamlit

Leave a Comment