antonymilne Claude commited on
Commit
e6217b4
·
1 Parent(s): d00c79f

Refactor overview page to simplify navigation and reduce code repetition

Browse files

- Replace vm.Action navigation functions with simple href-based navigation
- Create reusable helper function for chart containers with optional navigation buttons
- Improve naming: rename pages (page_1 -> overview_page) and controls (pg1_parameter_1 -> metric)
- Remove unused imports and helper functions
- Update CSS selectors to match new container IDs

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (2) hide show
  1. app.py +46 -117
  2. assets/css/custom.css +1 -1
app.py CHANGED
@@ -1,14 +1,10 @@
1
  """Dev app to try things out."""
2
 
3
- import json
4
- import base64
5
-
6
  import vizro.plotly.express as px
7
  from vizro import Vizro
8
  import vizro.models as vm
9
  import vizro.actions as va
10
  from vizro.figures import kpi_card, kpi_card_reference
11
- from vizro.models.types import capture
12
  from vizro.tables import dash_ag_grid
13
 
14
  from data import superstore_df, create_superstore_product, pareto_customers_table, create_kpi_data
@@ -49,38 +45,28 @@ order_kpi_df = create_kpi_data(superstore_df, value_col="Order ID")
49
  customer_kpi_df = create_kpi_data(superstore_df, value_col="Customer ID")
50
 
51
 
52
- def _encode_to_base64(value):
53
- json_bytes = json.dumps(value, separators=(",", ":")).encode("utf-8")
54
- b64_bytes = base64.urlsafe_b64encode(json_bytes)
55
- return f"b64_{b64_bytes.decode('utf-8').rstrip('=')}"
56
-
57
-
58
- @capture("action")
59
- def nav_region(parameter_value):
60
- url_query_params = f"?pg2-parameter-2={_encode_to_base64(parameter_value)}"
61
- return "/regions", url_query_params
62
-
63
-
64
- @capture("action")
65
- def nav_product():
66
- return "/products"
67
 
 
 
 
 
 
 
 
 
 
68
 
69
- @capture("action")
70
- def nav_customer():
71
- return "/customers"
72
 
73
 
74
- @capture("action")
75
- def nav_orders():
76
- return "/orders"
77
-
78
-
79
- page_1 = vm.Page(
80
  title="Overview",
81
  components=[
82
  vm.Container(
83
- id="pg1-container-1",
84
  title="💡 Click on a KPI card to update the charts below.",
85
  components=[
86
  vm.Figure(
@@ -93,7 +79,7 @@ page_1 = vm.Page(
93
  value_format="${value:,.0f}",
94
  icon="bar_chart",
95
  ),
96
- actions=va.set_control(control="pg1_parameter_1", value="Sales"),
97
  ),
98
  vm.Figure(
99
  figure=kpi_card_reference(
@@ -105,7 +91,7 @@ page_1 = vm.Page(
105
  value_format="${value:,.0f}",
106
  icon="money_bag",
107
  ),
108
- actions=va.set_control(control="pg1_parameter_1", value="Profit"),
109
  ),
110
  vm.Figure(
111
  figure=kpi_card_reference(
@@ -118,7 +104,7 @@ page_1 = vm.Page(
118
  icon="orders",
119
  # agg_func="nunique",
120
  ),
121
- actions=va.set_control(control="pg1_parameter_1", value="Order ID"),
122
  ),
123
  vm.Figure(
124
  figure=kpi_card_reference(
@@ -131,96 +117,39 @@ page_1 = vm.Page(
131
  icon="group",
132
  # agg_func="nunique",
133
  ),
134
- actions=va.set_control(control="pg1_parameter_1", value="Customer ID"),
135
  ),
136
  ],
137
  layout=vm.Grid(grid=[[0, 1, 2, 3]]),
138
  ),
139
- vm.Container(
140
- title="",
141
- components=[
142
- vm.Graph(
143
- id="line_chart_by_month",
144
- figure=create_line_chart_per_month(
145
- superstore_df,
146
- value_col="Sales",
147
- ),
148
- ),
149
- ],
150
- variant="filled",
151
  ),
152
- vm.Container(
153
- title="",
154
- components=[
155
- vm.Graph(id="order_status_chart", figure=pie_chart_by_order_status(superstore_df, value_col="Sales")),
156
- vm.Button(
157
- id="orders-nav-btn",
158
- text="View Deep Dive",
159
- icon="jump_to_element",
160
- variant="outlined",
161
- actions=vm.Action(function=nav_orders(), outputs=["vizro_url.pathname"]),
162
- ),
163
- ],
164
- layout=vm.Grid(grid=[[0], [0], [0], [0], [0], [1]], row_gap="8px"),
165
- variant="filled",
166
  ),
167
- vm.Container(
168
- components=[
169
- vm.Graph(
170
- id="region_bar_chart", figure=create_lollipop_chart_by_region(superstore_df, value_col="Sales")
171
- ),
172
- vm.Button(
173
- id="region-nav-btn",
174
- text="View Deep Dive",
175
- icon="jump_to_element",
176
- variant="outlined",
177
- actions=vm.Action(
178
- function=nav_region("pg1_parameter_1"), outputs=["vizro_url.pathname", "vizro_url.search"]
179
- ),
180
- ),
181
- ],
182
- layout=vm.Grid(grid=[[0], [0], [0], [0], [0], [1]], row_gap="8px"),
183
- variant="filled",
184
  ),
185
- vm.Container(
186
- components=[
187
- vm.Graph(
188
- id="bar_chart_by_segment",
189
- figure=create_bar_current_vs_previous_segment(superstore_df, value_col="Sales"),
190
- ),
191
- vm.Button(
192
- id="segment-nav-btn",
193
- text="View Deep Dive",
194
- icon="jump_to_element",
195
- variant="outlined",
196
- actions=vm.Action(function=nav_customer(), outputs=["vizro_url.pathname"]),
197
- ),
198
- ],
199
- variant="filled",
200
- layout=vm.Grid(grid=[[0], [0], [0], [0], [0], [1]], row_gap="8px"),
201
  ),
202
- vm.Container(
203
- title="",
204
- layout=vm.Grid(grid=[[0], [0], [0], [0], [0], [1]], row_gap="8px"),
205
- components=[
206
- vm.Graph(
207
- id="category_bar_chart",
208
- figure=create_bar_current_vs_previous_category(superstore_df, value_col="Sales"),
209
- ),
210
- vm.Button(
211
- id="customer-nav-btn",
212
- text="View Deep Dive",
213
- icon="jump_to_element",
214
- variant="outlined",
215
- actions=vm.Action(function=nav_product(), outputs=["vizro_url.pathname"]),
216
- ),
217
- ],
218
- variant="filled",
219
  ),
220
  ],
221
  controls=[
222
  vm.Parameter(
223
- id="pg1_parameter_1",
224
  selector=vm.RadioItems(
225
  options=[
226
  {"value": "Sales", "label": "Sales"},
@@ -234,8 +163,8 @@ page_1 = vm.Page(
234
  "region_bar_chart.value_col",
235
  "category_bar_chart.value_col",
236
  "order_status_chart.value_col",
237
- "line_chart_by_month.value_col",
238
- "bar_chart_by_segment.value_col",
239
  ],
240
  show_in_url=True,
241
  visible=False,
@@ -260,7 +189,7 @@ page_1 = vm.Page(
260
  ),
261
  )
262
 
263
- page_2 = vm.Page(
264
  title="Regions",
265
  components=[
266
  vm.Container(
@@ -334,7 +263,7 @@ page_2 = vm.Page(
334
  ],
335
  )
336
 
337
- page_3 = vm.Page(
338
  title="Customers",
339
  components=[
340
  vm.Container(
@@ -369,7 +298,7 @@ page_3 = vm.Page(
369
  )
370
 
371
 
372
- page_4 = vm.Page(
373
  title="Products",
374
  components=[
375
  vm.Container(
@@ -447,7 +376,7 @@ page_4 = vm.Page(
447
  ],
448
  )
449
 
450
- page_5 = vm.Page(
451
  title="Orders",
452
  layout=vm.Flex(),
453
  components=[
@@ -496,7 +425,7 @@ navigation = vm.Navigation(
496
 
497
  dashboard = vm.Dashboard(
498
  title="Superstore dashboard",
499
- pages=[page_1, page_2, page_3, page_4, page_5],
500
  navigation=navigation,
501
  theme="vizro_light",
502
  )
 
1
  """Dev app to try things out."""
2
 
 
 
 
3
  import vizro.plotly.express as px
4
  from vizro import Vizro
5
  import vizro.models as vm
6
  import vizro.actions as va
7
  from vizro.figures import kpi_card, kpi_card_reference
 
8
  from vizro.tables import dash_ag_grid
9
 
10
  from data import superstore_df, create_superstore_product, pareto_customers_table, create_kpi_data
 
45
  customer_kpi_df = create_kpi_data(superstore_df, value_col="Customer ID")
46
 
47
 
48
+ def create_chart_container_with_nav(id, figure, nav_href=None):
49
+ """Create a container with a chart and optional navigation button."""
50
+ components = [vm.Graph(id=f"{id}_chart", figure=figure)]
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
+ if nav_href:
53
+ components.append(
54
+ vm.Button(
55
+ id=f"{id}_nav_btn", text="View Deep Dive", icon="jump_to_element", variant="outlined", href=nav_href
56
+ )
57
+ )
58
+ layout = vm.Grid(grid=[[0]] * 5 + [[1]], row_gap="8px")
59
+ else:
60
+ layout = None
61
 
62
+ return vm.Container(id=id, components=components, layout=layout, variant="filled")
 
 
63
 
64
 
65
+ overview_page = vm.Page(
 
 
 
 
 
66
  title="Overview",
67
  components=[
68
  vm.Container(
69
+ id="kpi-cards",
70
  title="💡 Click on a KPI card to update the charts below.",
71
  components=[
72
  vm.Figure(
 
79
  value_format="${value:,.0f}",
80
  icon="bar_chart",
81
  ),
82
+ actions=va.set_control(control="metric", value="Sales"),
83
  ),
84
  vm.Figure(
85
  figure=kpi_card_reference(
 
91
  value_format="${value:,.0f}",
92
  icon="money_bag",
93
  ),
94
+ actions=va.set_control(control="metric", value="Profit"),
95
  ),
96
  vm.Figure(
97
  figure=kpi_card_reference(
 
104
  icon="orders",
105
  # agg_func="nunique",
106
  ),
107
+ actions=va.set_control(control="metric", value="Order ID"),
108
  ),
109
  vm.Figure(
110
  figure=kpi_card_reference(
 
117
  icon="group",
118
  # agg_func="nunique",
119
  ),
120
+ actions=va.set_control(control="metric", value="Customer ID"),
121
  ),
122
  ],
123
  layout=vm.Grid(grid=[[0, 1, 2, 3]]),
124
  ),
125
+ create_chart_container_with_nav(
126
+ id="line_month",
127
+ figure=create_line_chart_per_month(superstore_df, value_col="Sales"),
 
 
 
 
 
 
 
 
 
128
  ),
129
+ create_chart_container_with_nav(
130
+ id="order_status",
131
+ figure=pie_chart_by_order_status(superstore_df, value_col="Sales"),
132
+ nav_href="/orders",
 
 
 
 
 
 
 
 
 
 
133
  ),
134
+ create_chart_container_with_nav(
135
+ id="region_bar",
136
+ figure=create_lollipop_chart_by_region(superstore_df, value_col="Sales"),
137
+ nav_href="/regions",
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  ),
139
+ create_chart_container_with_nav(
140
+ id="segment_bar",
141
+ figure=create_bar_current_vs_previous_segment(superstore_df, value_col="Sales"),
142
+ nav_href="/customers",
 
 
 
 
 
 
 
 
 
 
 
 
143
  ),
144
+ create_chart_container_with_nav(
145
+ id="category_bar",
146
+ figure=create_bar_current_vs_previous_category(superstore_df, value_col="Sales"),
147
+ nav_href="/products",
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  ),
149
  ],
150
  controls=[
151
  vm.Parameter(
152
+ id="metric",
153
  selector=vm.RadioItems(
154
  options=[
155
  {"value": "Sales", "label": "Sales"},
 
163
  "region_bar_chart.value_col",
164
  "category_bar_chart.value_col",
165
  "order_status_chart.value_col",
166
+ "line_month_chart.value_col",
167
+ "segment_bar_chart.value_col",
168
  ],
169
  show_in_url=True,
170
  visible=False,
 
189
  ),
190
  )
191
 
192
+ regions_page = vm.Page(
193
  title="Regions",
194
  components=[
195
  vm.Container(
 
263
  ],
264
  )
265
 
266
+ customers_page = vm.Page(
267
  title="Customers",
268
  components=[
269
  vm.Container(
 
298
  )
299
 
300
 
301
+ products_page = vm.Page(
302
  title="Products",
303
  components=[
304
  vm.Container(
 
376
  ],
377
  )
378
 
379
+ orders_page = vm.Page(
380
  title="Orders",
381
  layout=vm.Flex(),
382
  components=[
 
425
 
426
  dashboard = vm.Dashboard(
427
  title="Superstore dashboard",
428
+ pages=[overview_page, regions_page, customers_page, products_page, orders_page],
429
  navigation=navigation,
430
  theme="vizro_light",
431
  )
assets/css/custom.css CHANGED
@@ -34,7 +34,7 @@
34
  font-size: 14px;
35
  }
36
 
37
- #pg1-container-1.container-fluid {
38
  gap: 0;
39
  }
40
 
 
34
  font-size: 14px;
35
  }
36
 
37
+ #kpi-cards.container-fluid {
38
  gap: 0;
39
  }
40