Py.Cafe

antonymilne/

vizro-h1-sales-patterns

Analyzing H1 Sales Patterns with Vizro

DocsPricing
  • assets/
  • __init__.py
  • app.py
  • generate_data.py
  • h1_sales.csv
  • 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
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
import pandas as pd
import vizro.actions as va
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.tables import dash_ag_grid

df = pd.read_csv("h1_sales.csv")
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]

daily_revenue_container = vm.Container(
    title="Daily revenue",
    components=[
        vm.Graph(
            figure=px.density_heatmap(
                df,
                x="week",
                y="day_of_week",
                z="revenue",  # sum of revenue per cell,
                nbinsx=df["week"].nunique(),
                marginal_x="histogram",
                category_orders={"day_of_week": days},
                labels={"revenue": "Revenue ($)", "price": "Price ($)", "day_of_week": "Day"},
            ),
            actions=[
                va.set_control(control="week_filter", value="x"),
                va.set_control(control="day_filter", value="y"),
            ],
            header="This heatmap shows the aggregated revenue for each day of the week across H1. Click on a cell to "
            "show all the day's transactions in the table below.",
        ),
    ],
    collapsed=False,
    variant="filled",
)

individual_transactions_container = vm.Container(
    layout=vm.Flex(),
    title="Individual transactions",
    description="**Data source**: h1_sales.csv.\n\n**Last updated**: 2025-09-18.",
    components=[
        vm.Button(
            icon="Download",
            text="Download data",
            actions=va.export_data(targets=["grid"]),
        ),
        vm.AgGrid(
            id="grid",
            figure=dash_ag_grid(
                df,
                columnDefs=[
                    {"field": "product"},
                    {"field": "price", "cellDataType": "dollar"},
                    {"field": "quantity"},
                    {"field": "revenue", "cellDataType": "dollar"},
                ],
            ),
        ),
    ],
    controls=[
        vm.Filter(id="week_filter", column="week", selector=vm.Dropdown(multi=False), show_in_url=True),
        vm.Filter(
            id="day_filter",
            column="day_of_week",
            selector=vm.Dropdown(title="Day", options=days),  # Put days in manually to get ordered correctly.
            show_in_url=True,
        ),
    ],
    variant="outlined",
)

page = vm.Page(
    title="Analysis of H1 sales",
    layout=vm.Flex(),
    components=[daily_revenue_container, individual_transactions_container],
)

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