<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ana</title>
    <description>The latest articles on DEV Community by Ana (@ana_p).</description>
    <link>https://dev.to/ana_p</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4046432%2F442c5690-d8c5-4e45-bf00-2eecc0f57818.png</url>
      <title>DEV Community: Ana</title>
      <link>https://dev.to/ana_p</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ana_p"/>
    <language>en</language>
    <item>
      <title>How to Build an Interactive Sales Analytics Dashboard in Python using Streamlit</title>
      <dc:creator>Ana</dc:creator>
      <pubDate>Sat, 25 Jul 2026 06:18:50 +0000</pubDate>
      <link>https://dev.to/ana_p/how-to-build-an-interactive-sales-analytics-dashboard-in-python-using-streamlit-272</link>
      <guid>https://dev.to/ana_p/how-to-build-an-interactive-sales-analytics-dashboard-in-python-using-streamlit-272</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;Streamlit makes it remarkably fast to transform raw Python scripts into interactive, web-based data applications without needing any frontend knowledge in HTML, CSS, or JavaScript.

In this tutorial, we will build a full-featured &lt;span class="gs"&gt;**Sales Analytics Dashboard**&lt;/span&gt; complete with real-time sidebar filtering, custom KPI metric cards, dynamic line/bar charts, and expandable data preview tables.
&lt;span class="p"&gt;
---
&lt;/span&gt;
&lt;span class="gu"&gt;## Prerequisites&lt;/span&gt;

To follow along, make sure you have Python 3.9+ installed along with the required libraries:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
pip install streamlit pandas numpy&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Step 1: Setting Up the Page &amp;amp; Mock Data with Caching

First, we import the necessary libraries, set up the layout, and create a function to generate mock sales records.

We use Streamlit’s `@st.cache_data` decorator so the data is only generated once per session, keeping the app snappy during user interactions.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
python&lt;br&gt;
import streamlit as st&lt;br&gt;
import pandas as pd&lt;br&gt;
import numpy as np&lt;/p&gt;
&lt;h1&gt;
  
  
  Set layout configuration
&lt;/h1&gt;

&lt;p&gt;st.set_page_config(page_title="Sales Dashboard", layout="wide")&lt;/p&gt;
&lt;h1&gt;
  
  
  Cache data loading for performance optimization
&lt;/h1&gt;

&lt;p&gt;&lt;a class="mentioned-user" href="https://dev.to/st"&gt;@st&lt;/a&gt;.cache_data&lt;br&gt;
def load_data():&lt;br&gt;
    dates = pd.date_range("2025-01-01", periods=180)&lt;br&gt;
    regions = ["North", "South", "East", "West"]&lt;br&gt;
    df = pd.DataFrame({&lt;br&gt;
        "date": np.random.choice(dates, 500),&lt;br&gt;
        "region": np.random.choice(regions, 500),&lt;br&gt;
        "product": np.random.choice(["A", "B", "C"], 500),&lt;br&gt;
        "sales": np.random.randint(100, 5000, 500),&lt;br&gt;
        "units": np.random.randint(1, 50, 500),&lt;br&gt;
    })&lt;br&gt;
    return df.sort_values("date")&lt;/p&gt;

&lt;p&gt;df = load_data()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Step 2: Adding Interactive Sidebar Filters

Next, we add controls inside the sidebar to let users filter the dataset by region, product type, and date range. A boolean mask applies those selections dynamically.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
python&lt;/p&gt;
&lt;h1&gt;
  
  
  --- Sidebar filters ---
&lt;/h1&gt;

&lt;p&gt;st.sidebar.header("Filters")&lt;br&gt;
region_filter = st.sidebar.multiselect("Region", df["region"].unique(), default=df["region"].unique())&lt;br&gt;
product_filter = st.sidebar.multiselect("Product", df["product"].unique(), default=df["product"].unique())&lt;br&gt;
date_range = st.sidebar.date_input("Date range", [df["date"].min(), df["date"].max()])&lt;/p&gt;
&lt;h1&gt;
  
  
  Filter dataframe based on selections
&lt;/h1&gt;

&lt;p&gt;mask = (&lt;br&gt;
    df["region"].isin(region_filter)&lt;br&gt;
    &amp;amp; df["product"].isin(product_filter)&lt;br&gt;
    &amp;amp; (df["date"] &amp;gt;= pd.to_datetime(date_range[0]))&lt;br&gt;
    &amp;amp; (df["date"] &amp;lt;= pd.to_datetime(date_range[1]))&lt;br&gt;
)&lt;br&gt;
filtered = df[mask]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Step 3: Displaying High-Level KPI Metrics

To display top-level executive metrics at a glance, we split the main body into 4 equal columns using `st.columns()` and populate them with `st.metric()`.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
python&lt;/p&gt;
&lt;h1&gt;
  
  
  --- Title &amp;amp; Subtitle ---
&lt;/h1&gt;

&lt;p&gt;st.title("📈 Sales Dashboard")&lt;br&gt;
st.caption(f"Showing {len(filtered):,} records")&lt;/p&gt;
&lt;h1&gt;
  
  
  --- KPI row ---
&lt;/h1&gt;

&lt;p&gt;c1, c2, c3, c4 = st.columns(4)&lt;br&gt;
c1.metric("Total Sales", f"${filtered['sales'].sum():,.0f}")&lt;br&gt;
c2.metric("Total Units", f"{filtered['units'].sum():,}")&lt;br&gt;
c3.metric("Avg Order", f"${filtered['sales'].mean():,.0f}" if len(filtered) else "$0")&lt;br&gt;
c4.metric("Orders", f"{len(filtered):,}")&lt;/p&gt;

&lt;p&gt;st.divider()&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Step 4: Adding Charts &amp;amp; Raw Data Views

Finally, we group the filtered data and visualize trends using Streamlit's built-in `line_chart` and `bar_chart` components. We also wrap the raw DataFrame inside an expandable container (`st.expander`) to keep the interface clean.

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
python&lt;/p&gt;
&lt;h1&gt;
  
  
  --- Visualizations ---
&lt;/h1&gt;

&lt;p&gt;col1, col2 = st.columns(2)&lt;/p&gt;

&lt;p&gt;with col1:&lt;br&gt;
    st.subheader("Sales Over Time")&lt;br&gt;
    daily = filtered.groupby("date")["sales"].sum()&lt;br&gt;
    st.line_chart(daily)&lt;/p&gt;

&lt;p&gt;with col2:&lt;br&gt;
    st.subheader("Sales by Region")&lt;br&gt;
    by_region = filtered.groupby("region")["sales"].sum()&lt;br&gt;
    st.bar_chart(by_region)&lt;/p&gt;
&lt;h1&gt;
  
  
  --- Product Performance ---
&lt;/h1&gt;

&lt;p&gt;st.subheader("Sales by Product")&lt;br&gt;
by_product = filtered.groupby("product")["sales"].sum()&lt;br&gt;
st.bar_chart(by_product)&lt;/p&gt;
&lt;h1&gt;
  
  
  --- Raw Data Section ---
&lt;/h1&gt;

&lt;p&gt;with st.expander("View raw data"):&lt;br&gt;
    st.dataframe(filtered, use_container_width=True)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
---

## Running the Application

Save your Python code in a file named `app.py` and run the following command in your terminal:

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
bash&lt;br&gt;
streamlit run app.py&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;


Your browser will automatically open a tab at `http://localhost:8501` showing your live interactive sales dashboard.

---

## Conclusion

With less than 80 lines of clean Python code, we built a responsive dashboard that updates instantaneously as users interact with filters. Streamlit handles the state management, caching, and layout automatically, allowing developers and data engineers to focus purely on the logic and insights.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>python</category>
      <category>streamlit</category>
      <category>datascience</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
