Interactive Python Dashboard Application Using Streamlit

Jul 20, 2024

Interactive Python Dashboard Application Using Streamlit

Introduction

  • Speaker: Excited about the session on creating interactive Python dashboards using Streamlit.
  • Goal: To design an interactive dashboard application from scratch using Python, Plotly, and Streamlit.
  • Common Question: Can Streamlit create dashboards similar to Power BI and Tableau? Yes.

Sample Data

  • Dataset: Superstore data.
  • Features:
    • Row ID
    • Order ID
    • Order Date
    • Ship Date
    • Ship Mode
    • Customer ID
    • Customer Name
    • Segment
    • Country
    • City
    • State
    • Postal Code
    • Region
    • Product Information (Product ID, Category, Sub-Category, Product Name, Sales, Quantity, Discount, Profit)

Dashboard Design and Functionality

  • User Interface:
    • Upload dataset using a file uploader.
    • Select specific time periods using a date picker.
    • Filter data based on regions, states, and cities.

Visualization Components

  1. Category-wise Sales: Bar Chart
  2. Region-wise Sales: Pie Chart
  3. Time Series Analysis: Line Chart
  4. Hierarchical View: Treemap
  5. Segment by Sales: Pie Chart
  6. Category by Sales: Pie Chart
  7. Scatter Plot: Relationship between Sales and Profit

Additional Features

  • View Data: Expander sections showing the data backing charts.
  • Download Data: Button to download filtered data as CSV.

Step-by-step Code Implementation

Initial Setup

  • Libraries: Import Streamlit, Plotly, Pandas, and OS.
import streamlit as st
import plotly.express as px
import pandas as pd
import os
  • Streamlit Configuration: Set page title, icon, and layout.
st.set_page_config(page_title='Superstore EDA', page_icon='📊', layout='wide')

Loading and Displaying Data

uploaded_file = st.file_uploader('Upload a file', type=['csv', 'xlsx'])
if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
else:
    df = pd.read_csv('sample_superstore.csv')

Date Picker for Data Filtering

  • Create columns for start and end date.
col1, col2 = st.columns(2)
start_date = col1.date_input('Start Date', df['Order Date'].min())
end_date = col2.date_input('End Date', df['Order Date'].max())
df = df[(df['Order Date'] >= start_date) & (df['Order Date'] <= end_date)]

Sidebar Filters

  • Region: Multi-select filter
  • State: Multi-select filter
  • City: Multi-select filter
regions = st.sidebar.multiselect('Select Region', df['Region'].unique())
if regions:
    df = df[df['Region'].isin(regions)]
states = st.sidebar.multiselect('Select State', df['State'].unique())
if states:
    df = df[df['State'].isin(states)]
cities = st.sidebar.multiselect('Select City', df['City'].unique())
if cities:
    df = df[df['City'].isin(cities)]

Visualization Components

  1. Category-Wise Sales Bar Chart
category_sales = df.groupby('Category')['Sales'].sum().reset_index()
fig1 = px.bar(category_sales, x='Category', y='Sales')
st.plotly_chart(fig1, use_container_width=True)
  1. Region-Wise Sales Pie Chart
region_sales = df.groupby('Region')['Sales'].sum().reset_index()
fig2 = px.pie(region_sales, names='Region', values='Sales', hole=0.5)
st.plotly_chart(fig2, use_container_width=True)
  1. Time Series Analysis
df['Year-Month'] = df['Order Date'].dt.to_period('M') 
time_series_sales = df.groupby('Year-Month')['Sales'].sum().reset_index()
fig3 = px.line(time_series_sales, x='Year-Month', y='Sales')
st.plotly_chart(fig3, use_container_width=True)
  1. Treemap
treemap_fig = px.treemap(df, path=['Region', 'Category', 'Sub-Category'], values='Sales')
st.plotly_chart(treemap_fig, use_container_width=True)

Adding Data Viewing and Download Capability

if st.expander('View Data'):
    st.write(category_sales.style.background_gradient(cmap='Blues'))
    csv = category_sales.to_csv(index=False).encode('utf-8')
    st.download_button('Download CSV', csv, 'category_sales.csv', 'text/csv')

Conclusion

  • Application: A comprehensive interactive dashboard application using Streamlit, Plotly, and Pandas.
  • Features: Data browsing, filtering, visualization, and downloading.
  • Future Add-ons: Emailing the dashboard or other interactive functionalities.