Py.Cafe

amward/

dash-superstore-gross-margin

Superstore Gross Margin Analysis

DocsPricing
  • app.py
  • 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Plotly's Figure Friday challenge. See more info here: https://community.plotly.com/t/figure-friday-2024-week-28/84980

import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc, Input, Output
import dash_bootstrap_components as dbc
import dash_ag_grid as dag
import plotly

df = pd.read_excel(
    "https://raw.githubusercontent.com/plotly/Figure-Friday/main/2024/week-28/Sample%20-%20Superstore.xls"
)
df = df[df["Country/Region"] == "United States"]
df["Category"] = pd.Categorical(df["Category"], categories=df["Category"].unique())
df["gm"] = df["Profit"] / df["Sales"] * 100
df["Order Date"] = df["Order Date"].dt.strftime("%Y-%m-%d")
df["Ship Date"] = df["Ship Date"].dt.strftime("%Y-%m-%d")
df["Year"] = df["Ship Date"].str[:4]
df.sort_values("State/Province", inplace=True)
ALL_STATES = df["State/Province"].unique().tolist()

# Range for the colorscales
MIN_GM = -150.0
MAX_GM = 150.0

app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])


def create_bar(dff, first_card=True):
    # Group by category and calculate total sales and total profit
    grouped_df = (
        dff.groupby(["Category"], observed=False)
        .agg({"Sales": "sum", "Profit": "sum"})
        .reset_index()
    )

    # Calculate GM%
    grouped_df["gm%"] = (grouped_df["Profit"] / grouped_df["Sales"]) * 100
    # Format label
    grouped_df["GM%"] = grouped_df["gm%"].apply(
        lambda x: f"{x:.1f}%" if isinstance(x, float) else "N/A"
    )

    # Create a horizontal bar chart
    fig = px.bar(
        grouped_df,
        x="gm%",
        y="Category",
        color="gm%",
        color_continuous_scale="rdbu",
        range_color=[MIN_GM, MAX_GM],
        orientation="h",
        text="GM%",
        template="simple_white",
        hover_data={"gm%": False},
        height=175 if first_card else 100,
    )
    fig.add_vline(
        x=0,
        line_width=3,
    )
    fig.update_layout(xaxis_range=[-75, 75], margin=dict(l=5, r=5, t=0, b=0))
    if not first_card:
        fig.update_layout(coloraxis_showscale=False)
        fig.update_xaxes(visible=False)
        fig.update_yaxes(visible=False)
    return fig


def get_color(gm):
    """
    returns the color for the gm%  based on the same colorscale as the bar charts
    """

    # Clamp the gm value between MIN_GM and MAX_GM
    gm = max(MIN_GM, min(gm, MAX_GM))

    normalized_gm = (gm - MIN_GM) / (MAX_GM - MIN_GM)

    color = plotly.colors.sample_colorscale(
        plotly.colors.sequential.RdBu,
        samplepoints=normalized_gm,
    )
    return color[0]


state_dropdown = html.Div(
    [
        dbc.Label("Select a State", html_for="state_dropdown"),
        dcc.Dropdown(id="state-dropdown", value=[], multi=True),
    ],
    className="my-4",
)

region_dropdown = html.Div(
    [
        dbc.Label("Select a Region", html_for="region_dropdown"),
        dcc.Dropdown(
            id="region-dropdown",
            options=df["Region"].unique(),
            value=["South"],
            multi=True,
        ),
    ],
    className="mb-4",
)

date_checklist = html.Div(
    [
        dbc.Label("Select Dates"),
        dbc.Checklist(
            options=["2024", "2023", "2022", "2021"],
            value=["2024", "2023", "2022", "2021"],
            id="date-checklist",
        ),
    ]
)

control_panel = dbc.Card(
    dbc.CardBody(
        [region_dropdown, state_dropdown, date_checklist],
        className="bg-light",
    )
)


heading = html.H4(
    "Superstore Gross Margin Analysis by Region",
    className="bg-secondary text-white p-2 mb-4",
)


def make_grid():
    df["gm_color"] = df["gm"].apply(lambda x: get_color(x))
    other_grid_columns = [
        "Sub-Category", "Order ID",  "Ship Date", "Ship Mode", "Customer ID", "Segment", "City",
        "Postal Code", "Product ID", "Product Name", "Quantity",  "Discount",
    ]

    grid = dag.AgGrid(
        id="grid",
        rowData=df.to_dict("records"),
        columnDefs=[{"field": f} for f in ["Region", "State/Province", "Category"]]
        + [
            {
                "field": "Sales",
                "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"},
                "type": "rightAligned",
            },
            {
                "field": "Profit",
                "valueFormatter": {"function": "d3.format('$,.2f')(params.value)"},
                "type": "rightAligned",
            },
            {
                "field": "gm",
                "valueFormatter": {"function": "d3.format(',.1f')(params.value) + '%'"},
                "cellStyle": {
                    "function": "params.value && {'backgroundColor': params.data.gm_color}",
                },
                "type": "rightAligned",
            },
        ]
        + [{"field": f} for f in other_grid_columns],
        defaultColDef={"filter": True, "floatingFilter": True},
        columnSize='autoSize',
        dashGridOptions={"suppressColumnVirtualisation": True}
    )
    return grid


app.layout = dbc.Container(
    [
        heading,
        dbc.Row(
            [
                dbc.Col(control_panel, md=3),
                dbc.Col([html.Div(id="panel"), make_grid()], md=9),
            ]
        ),
    ],
    fluid=True,
)


@app.callback(
    Output("state-dropdown", "options"),
    Output("state-dropdown", "value"),
    Input("region-dropdown", "value"),
)
def update_state_options(region):
    if not region:
        return ALL_STATES, []
    dff = df[df["Region"].isin(region)]
    return dff["State/Province"].unique().tolist(), []


@app.callback(
    Output("panel", "children"),
    Output("grid", "dashGridOptions"),
    Input("state-dropdown", "value"),
    Input("region-dropdown", "value"),
    Input("date-checklist", "value"),
)
def update(states, regions, years):
    dff = df[df["Year"].isin(years)]
    if not states and not regions:
        states = ALL_STATES
    if not states and regions:
        dff = dff[dff["Region"].isin(regions)]
        states = dff["State/Province"].unique().tolist()

    # filter grid
    my_filter = f"{states}.includes(params.data['State/Province']) && {years}.includes(params.data['Year'])"
    grid_options = {
        "isExternalFilterPresent": {"function": "true"},
        "doesExternalFilterPass": {"function": my_filter},
    }

    # make summary card
    dff = dff[dff["State/Province"].isin(states)]
    state_gm = 0
    if dff["Sales"].sum() != 0:
        state_gm = dff["Profit"].sum() / dff["Sales"].sum() * 100

    fig = create_bar(dff)
    first_card = dbc.Card(
        [
            dbc.CardHeader(
                f"Total Selected {state_gm:.1f}%",
                style={"backgroundColor": get_color(state_gm)},
            ),
            dcc.Graph(
                figure=fig,
                id="total",
                config={"displayModeBar": False},
                className="p-1",
            ),
        ],
        style={"maxWidth": 800},
        className="mb-4",
    )

    # make panel with cards by state
    state_panel = []
    for i in states:
        dff_state = dff[dff["State/Province"] == i]
        state_gm = 0
        if dff_state["Sales"].sum() != 0:
            state_gm = dff_state["Profit"].sum() / dff_state["Sales"].sum() * 100

        fig = create_bar(dff_state, first_card=False)
        card = dbc.Card(
            [
                dbc.CardHeader(
                    f"{i} {state_gm:.1f}%",
                    style={"backgroundColor": get_color(state_gm)},
                ),
                dcc.Graph(
                    figure=fig, id=i, config={"displayModeBar": False}, className="p-1"
                ),
            ],
            style={"width": 200},
            className="mb-2",
        )
        state_panel.append(dbc.Col(card))

    return [
        dbc.Row(dbc.Col(first_card)),
        dbc.Row(state_panel, className="g-1"),
    ], grid_options


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