Py.Cafe

huong-li-nguyen/

figure-friday-week-28

Superstore Sales and Profit Analysis

DocsPricing
  • app.py
  • charts.py
  • logo.svg
  • requirements.txt
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
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation

import pandas as pd
import vizro.models as vm
from charts import COLUMN_DEFS, scatter_with_quadrants
from vizro import Vizro
from vizro.actions import export_data, filter_interaction
from vizro.tables import dash_ag_grid


# TIDY DATA
df = pd.read_excel("https://raw.githubusercontent.com/plotly/Figure-Friday/main/2024/week-28/Sample%20-%20Superstore.xls")
df["Category / Sub-Category"] = df["Category"] + " / " + df["Sub-Category"]
df = df.groupby(["Category / Sub-Category", "Product Name"]).agg({"Sales": "sum", "Profit": "sum"}).reset_index()
df["Profit Margin"] = df["Profit"] / df["Sales"]
df["Profit Absolute"] = abs(df["Profit"])  # For size in px.scatter (cannot be negative)


# DEFINE PAGE AND DASHBOARD
page = vm.Page(
    title="Week 28 - Sales vs. Profit 📈",
    id="page-id",
    layout=vm.Layout(grid=[[0, 1]] * 6 + [[2, -1]]),
    components=[
        vm.Graph(
            id="scatter",
            figure=scatter_with_quadrants(data_frame=df, x="Sales", y="Profit", custom_data=["Product Name", "Profit Margin"]),
            actions=[vm.Action(function=filter_interaction(targets=["table"]))],
        ),
        vm.AgGrid(id="table", figure=dash_ag_grid(df, columnDefs=COLUMN_DEFS)),
        vm.Button(
            text="Export data",
            actions=[
                vm.Action(function=export_data()),
            ],
        ),
    ],
    controls=[
        vm.Filter(column="Category / Sub-Category", selector=vm.Dropdown(multi=False, value="Technology / Phones")),
        vm.Parameter(
            targets=["scatter.x_ref_quantile"],
            selector=vm.Slider(min=0, max=1, step=0.2, value=0.8, title="X-reference line (quantile)"),
        ),
        vm.Parameter(
            targets=["scatter.y_ref_quantile"],
            selector=vm.Slider(min=0, max=1, step=0.2, value=0.2, title="Y-reference line (quantile)"),
        ),
    ],
)

dashboard = vm.Dashboard(pages=[page], title="Figure Friday", theme="vizro_light")
Vizro(assets_folder=".").build(dashboard).run()