Py.Cafe

ann_sajee/

vizro-superstore-sales-dashboard

Vizro Data Visualizations with Superstore Dataset

DocsPricing
  • app.py
  • requirements.txt
  • superstore.csv
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import vizro
from vizro import Vizro
import pandas as pd

import vizro.models as vm
import vizro.plotly.express as vpx
import vizro.tables as vt

# Load the Superstore dataset
# You can download it from: https://community.tableau.com/s/question/0D54T00000CWeX8SAL/sample-superstore-sales-excelxls
df = pd.read_csv("./superstore.csv")  # Update path as needed

# Ensure dates parsed for the line chart
df["Order Date"] = pd.to_datetime(df["Order Date"], errors="coerce")

# Pre-aggregations
sales_by_cat  = df.groupby("Category", as_index=False)["Sales"].sum()
sales_by_date = df.groupby("Order Date", as_index=False)["Sales"].sum()

page1 = vm.Page(
    title="Sales Overview",
    components=[
        vm.Graph(
            figure=vpx.bar(
                data_frame=sales_by_cat.sort_values("Sales", ascending=True),
                x="Sales",
                y="Category",
                orientation="h",
                title="Sales by Category",
                labels={"Sales": "Total Sales ($)", "Category": "Product Category"}
            )
        ),
        vm.Graph(
            figure=vpx.line(
                data_frame=sales_by_date,
                x="Order Date",
                y="Sales",
                title="Sales Trend Over Time",
                labels={"Sales": "Total Sales ($)", "Order Date": "Order Date"}
            )
        ),
        vm.Graph(
            id="scatter",
            figure=vpx.scatter(
                data_frame=df,
                x="Sales",
                y="Profit",
                color="Category",
                facet_col="Region",
                title="Sales vs Profit by Category (Faceted by Region)",
                labels={"Sales": "Sales ($)", "Profit": "Profit ($)", "Category": "Product Category"}
            )
        ),
    ],
    controls=[
        vm.Filter(column="Region"),
        vm.Filter(column="Category"),
    ],
)

# Page 2 - Regional Analysis
# Prepare data
df["Order Date"] = pd.to_datetime(df["Order Date"], errors="coerce")
region_summary = df.groupby(["Region"], as_index=False).agg(
    {"Sales": "sum", "Profit": "sum", "Quantity": "sum"}
)
region_category = df.groupby(["Region", "Category"], as_index=False)["Sales"].sum()
df_temp = df.copy()
df_temp["Year-Month"] = df_temp["Order Date"].dt.to_period("M").astype(str)
region_month = df_temp.groupby(["Region", "Year-Month"], as_index=False)["Sales"].sum()

page_regional = vm.Page(
    title="Regional Analysis",
    components=[
        # 1. Regional Sales Summary
        vm.Graph(
            figure=vpx.bar(
            data_frame=region_summary.sort_values("Sales", ascending=True),
            x="Sales",
            y="Region",
            orientation="h",
            # color="Region",
            title="Total Sales by Region",
            labels={"Sales": "Total Sales ($)", "Region": "Region"}
            )
        ),
        # 2. Regional Monthly Trend
        vm.Graph(
            figure=vpx.line(
            data_frame=region_month,
            x="Year-Month",
            y="Sales",
            color="Region",
            title="Monthly Sales Trend by Region"
            )
        ),
        # 3. Category Mix by Region
        vm.Graph(
            figure=vpx.bar(
            data_frame=region_category,
            x="Sales",
            y="Region",
            color="Category",
            color_discrete_sequence=["#FF6B6B", "#4ECDC4", "#45B7D1"],
            orientation="h",
            title="Sales Composition by Category across Regions"
            )
        ),
    ],
    controls=[
        vm.Filter(column="Region"),
        vm.Filter(column="Category"),
    ],
)


# Create the dashboard
dashboard = vm.Dashboard(
    pages=[page1, page_regional],
    title="Superstore Dashboard"
)
# Build and run the app
app = Vizro().build(dashboard)

if __name__ == "__main__":
    app.run(debug=True)