antonymilne Claude commited on
Commit
3399bdd
·
1 Parent(s): 1e4d086

Swap dictionary keys and values for pages

Browse files

Change dictionary structure to use icons as keys and page objects as values.
Also extract container definitions for regions and products pages to improve readability.

🤖 Generated with Claude Code

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

Files changed (1) hide show
  1. app.py +117 -137
app.py CHANGED
@@ -147,197 +147,177 @@ overview_page = vm.Page(
147
  ],
148
  )
149
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
150
  regions_page = vm.Page(
151
  title="Regions",
152
  components=[
153
  vm.Container(
154
  layout=vm.Grid(grid=[[0, 1]]),
155
- components=[
156
- vm.Container(
157
- components=[
158
- vm.Graph(
159
- id="region_map_chart",
160
- header="💡 Click on a state to filter charts on the right.",
161
- figure=create_map_bubble_new(superstore_df, value_col="Sales", custom_data=["State_Code"]),
162
- actions=va.set_control(control="state_filter", value="State_Code"),
163
- )
164
- ],
165
- variant="filled",
166
- ),
167
- vm.Container(
168
- components=[vm.Graph(id="pg2-chart-1", figure=bar_chart_top_n(superstore_df, x="Sales", y="City"))],
169
- controls=[
170
- vm.Parameter(
171
- targets=["pg2-chart-1.top_n"],
172
- selector=vm.Slider(min=5, max=30, step=5, value=20, title="Choose top N"),
173
- ),
174
- vm.Parameter(
175
- selector=vm.RadioItems(
176
- options=["City", "Customer Name", "Sub-Category"],
177
- title="Choose y-axis",
178
- ),
179
- targets=["pg2-chart-1.y"],
180
- ),
181
- ],
182
- variant="filled",
183
- ),
184
- ],
185
  controls=[
186
- vm.Filter(
187
- id="state_filter",
188
- column="State_Code",
189
- visible=False,
190
- ),
191
  vm.Filter(column="Segment", selector=vm.Checklist(title="Choose segment")),
192
  vm.Filter(column="Category", selector=vm.Checklist(title="Choose category")),
193
  vm.Parameter(
194
- selector=vm.RadioItems(
195
- options=["Sales", "Profit"],
196
- title="Choose metric",
197
- ),
198
- targets=[
199
- "region_map_chart.value_col",
200
- "pg2-chart-1.x",
201
- ],
202
  ),
203
  ],
204
  )
205
  ],
206
  )
207
 
208
- customers_page = vm.Page(
209
- title="Customers",
 
210
  components=[
211
- vm.Container(
212
- layout=vm.Grid(grid=[[0, 1]]),
213
- components=[
214
- vm.AgGrid(
215
- id="table-2",
216
- header="💡 Click on a row to highlight the customer.",
217
- figure=dash_ag_grid(
218
- aggrid_df,
219
- columnDefs=COLUMN_DEFS_CUSTOMERS,
220
- ),
221
- actions=va.set_control(control="pg3_parameter_1", value="Customer Name"),
222
- ),
223
- vm.Graph(id="pg3_pareto_chart", figure=pareto_customers_chart(superstore_df, highlight_customer=None)),
224
- ],
225
- controls=[
226
- vm.Filter(column="Region", selector=vm.Checklist(title="Choose region")),
227
- vm.Filter(column="Segment", selector=vm.Checklist(title="Choose segment")),
228
- vm.Filter(column="Category", selector=vm.Checklist(title="Choose category")),
229
- ],
230
  )
231
  ],
232
  controls=[
233
- vm.Parameter(
234
- id="pg3_parameter_1",
235
- targets=["pg3_pareto_chart.highlight_customer"],
236
- selector=vm.Dropdown(options=customer_name, value="NONE", multi=False),
 
237
  visible=False,
238
  )
239
  ],
 
240
  )
241
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
 
243
  products_page = vm.Page(
244
  title="Products",
245
  components=[
246
  vm.Container(
247
- layout=vm.Grid(grid=[[0, 0, 2, 2], [1, 1, 2, 2], [1, 1, 2, 2]]),
 
 
 
 
 
 
 
 
 
 
248
  components=[
249
- vm.Container(
250
- components=[
251
- vm.Graph(
252
- id="pg4-chart-1",
253
- figure=bar_chart_by_category(superstore_df, custom_data=["Category"]),
254
- actions=va.set_control(control="pg4-filter-1", value="Category"),
255
- )
256
- ],
257
- variant="filled",
258
- ),
259
- vm.Container(
260
- components=[
261
- vm.AgGrid(
262
- id="table",
263
- header="💡 Click on a row to highlight the data point in the matrix on the right.",
264
- figure=dash_ag_grid(superstore_product_df, columnDefs=COLUMN_DEFS_PRODUCT),
265
- actions=va.set_control(control="pg4_parameter_1", value="Sub-Category"),
266
- )
267
- ],
268
- variant="filled",
269
  ),
270
- vm.Container(
271
- components=[
272
- vm.Graph(
273
- id="pg4-chart-3",
274
- figure=scatter_with_quadrants(
275
- data_frame=superstore_product_df,
276
- x="Sales",
277
- y="Profit",
278
- custom_data=["Sub-Category"],
279
- ),
280
- )
281
- ],
282
- variant="filled",
283
  ),
284
  ],
285
  )
286
  ],
287
- controls=[
288
- vm.Filter(
289
- id="pg4-filter-1",
290
- column="Category",
291
- targets=["pg4-chart-1"],
292
- selector=vm.Checklist(title="Product Category"),
293
- visible=False,
294
- ),
295
- # vm.Filter(
296
- # column="Category / Sub-Category",
297
- # targets=["pg4-chart-3"],
298
- # selector=vm.Dropdown(multi=False, value="Technology / Phones"),
299
- # ),
300
- vm.Parameter(
301
- id="pg4_parameter_1",
302
- targets=["pg4-chart-3.highlight_sub_category"],
303
- selector=vm.Dropdown(options=subcategories, value="NONE", multi=False),
304
- visible=False,
305
- ),
306
- ],
307
  )
308
 
309
  orders_page = vm.Page(
310
  title="Orders",
311
  layout=vm.Flex(),
312
  components=[
313
- vm.AgGrid(id="table_id", figure=custom_orders_aggrid(superstore_df)),
314
- vm.Button(
315
- text="Export data", icon="download", actions=va.export_data(targets=["table_id"], file_format="xlsx")
316
- ),
317
  ],
318
  )
319
 
320
 
321
  pages = {
322
- overview_page: "Home",
323
- regions_page: "Globe Asia",
324
- products_page: "Barcode",
325
- customers_page: "Group",
326
- orders_page: "Shopping Cart",
327
  }
328
 
329
  navigation = vm.Navigation(
330
- pages=[page.title for page in pages.keys()],
331
  nav_selector=vm.NavBar(
332
- items=[vm.NavLink(pages=[page.title], label=page.title, icon=icon) for page, icon in pages.items()]
333
  ),
334
  )
335
 
336
  dashboard = vm.Dashboard(
337
- title="Superstore dashboard",
338
- pages=list(pages.keys()),
339
- navigation=navigation,
340
- theme="vizro_light",
341
  )
342
 
343
  app = Vizro().build(dashboard)
 
147
  ],
148
  )
149
 
150
+ region_map_container = vm.Container(
151
+ components=[
152
+ vm.Graph(
153
+ id="region_map_chart",
154
+ header="💡 Click on a state to filter ranked bars on the right.",
155
+ figure=create_map_bubble_new(superstore_df, value_col="Sales", custom_data=["State_Code"]),
156
+ actions=va.set_control(control="state_filter", value="State_Code"),
157
+ )
158
+ ],
159
+ variant="filled",
160
+ )
161
+ top_rank_bars_container = vm.Container(
162
+ components=[vm.Graph(id="top_rank_bars", figure=bar_chart_top_n(superstore_df, x="Sales", y="City"))],
163
+ controls=[
164
+ vm.Parameter(
165
+ targets=["top_rank_bars.top_n"],
166
+ selector=vm.Slider(min=5, max=30, step=5, value=20, title="Choose top N"),
167
+ ),
168
+ vm.Parameter(
169
+ selector=vm.RadioItems(
170
+ options=["City", "Customer Name", "Sub-Category"],
171
+ title="Choose y-axis",
172
+ ),
173
+ targets=["top_rank_bars.y"],
174
+ ),
175
+ ],
176
+ variant="filled",
177
+ )
178
  regions_page = vm.Page(
179
  title="Regions",
180
  components=[
181
  vm.Container(
182
  layout=vm.Grid(grid=[[0, 1]]),
183
+ components=[region_map_container, top_rank_bars_container],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
184
  controls=[
185
+ vm.Filter(id="state_filter", column="State_Code", visible=False),
 
 
 
 
186
  vm.Filter(column="Segment", selector=vm.Checklist(title="Choose segment")),
187
  vm.Filter(column="Category", selector=vm.Checklist(title="Choose category")),
188
  vm.Parameter(
189
+ selector=vm.RadioItems(options=["Sales", "Profit"], title="Choose metric"),
190
+ targets=["region_map_chart.value_col", "top_rank_bars.x"],
 
 
 
 
 
 
191
  ),
192
  ],
193
  )
194
  ],
195
  )
196
 
197
+ # TODO: highlight state instead of filter?
198
+
199
+ product_category_container = vm.Container(
200
  components=[
201
+ vm.Graph(
202
+ id="product_category_bar",
203
+ figure=bar_chart_by_category(superstore_df, custom_data=["Category"]),
204
+ actions=va.set_control(control="product_category_filter", value="Category"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  )
206
  ],
207
  controls=[
208
+ vm.Filter(
209
+ id="product_category_filter",
210
+ column="Category",
211
+ targets=["product_category_bar"],
212
+ selector=vm.Checklist(),
213
  visible=False,
214
  )
215
  ],
216
+ variant="filled",
217
  )
218
 
219
+ product_table_container = vm.Container(
220
+ components=[
221
+ vm.AgGrid(
222
+ header="💡 Click on a row to highlight the data point in the matrix on the right.",
223
+ figure=dash_ag_grid(superstore_product_df, columnDefs=COLUMN_DEFS_PRODUCT),
224
+ actions=va.set_control(control="quadrant_subcategory", value="Sub-Category"),
225
+ )
226
+ ],
227
+ variant="filled",
228
+ )
229
+
230
+ profit_vs_sales_container = vm.Container(
231
+ components=[
232
+ vm.Graph(
233
+ id="profit_vs_sales_quadrant",
234
+ figure=scatter_with_quadrants(
235
+ data_frame=superstore_product_df,
236
+ x="Sales",
237
+ y="Profit",
238
+ custom_data=["Sub-Category"],
239
+ ),
240
+ )
241
+ ],
242
+ controls=[
243
+ vm.Parameter(
244
+ id="quadrant_subcategory",
245
+ targets=["profit_vs_sales_quadrant.highlight_sub_category"],
246
+ selector=vm.Dropdown(options=subcategories, value="NONE", multi=False),
247
+ visible=False,
248
+ ),
249
+ ],
250
+ variant="filled",
251
+ )
252
+
253
+ # TODO: fill vm.Container helper?
254
+ # TODO: bar chart into histogram
255
+ # TODO: fix quadrant highlight
256
 
257
  products_page = vm.Page(
258
  title="Products",
259
  components=[
260
  vm.Container(
261
+ layout=vm.Grid(grid=[[0, 2], [1, 2], [1, 2]]),
262
+ components=[product_category_container, product_table_container, profit_vs_sales_container],
263
+ )
264
+ ],
265
+ )
266
+
267
+ customers_page = vm.Page(
268
+ title="Customers",
269
+ components=[
270
+ vm.Container(
271
+ layout=vm.Grid(grid=[[0, 1]]),
272
  components=[
273
+ vm.AgGrid(
274
+ header="💡 Click on a row to highlight the customer.",
275
+ figure=dash_ag_grid(aggrid_df, columnDefs=COLUMN_DEFS_CUSTOMERS),
276
+ actions=va.set_control(control="customer_parameter", value="Customer Name"),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277
  ),
278
+ vm.Graph(id="pareto_chart", figure=pareto_customers_chart(superstore_df, highlight_customer=None)),
279
+ ],
280
+ controls=[
281
+ vm.Filter(column="Region", selector=vm.Checklist(title="Choose region")),
282
+ vm.Filter(column="Segment", selector=vm.Checklist(title="Choose segment")),
283
+ vm.Filter(column="Category", selector=vm.Checklist(title="Choose category")),
284
+ vm.Parameter(
285
+ id="customer_parameter",
286
+ targets=["pareto_chart.highlight_customer"],
287
+ selector=vm.Dropdown(options=customer_name, value="NONE", multi=False),
288
+ visible=False,
 
 
289
  ),
290
  ],
291
  )
292
  ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
293
  )
294
 
295
  orders_page = vm.Page(
296
  title="Orders",
297
  layout=vm.Flex(),
298
  components=[
299
+ vm.AgGrid(figure=custom_orders_aggrid(superstore_df)),
300
+ vm.Button(text="Export data", icon="download", actions=va.export_data(file_format="xlsx")),
 
 
301
  ],
302
  )
303
 
304
 
305
  pages = {
306
+ "Home": overview_page,
307
+ "Globe Asia": regions_page,
308
+ "Barcode": products_page,
309
+ "Group": customers_page,
310
+ "Shopping Cart": orders_page,
311
  }
312
 
313
  navigation = vm.Navigation(
 
314
  nav_selector=vm.NavBar(
315
+ items=[vm.NavLink(pages=[page.id], label=page.title, icon=icon) for icon, page in pages.items()]
316
  ),
317
  )
318
 
319
  dashboard = vm.Dashboard(
320
+ title="Superstore dashboard", pages=list(pages.values()), navigation=navigation, theme="vizro_light"
 
 
 
321
  )
322
 
323
  app = Vizro().build(dashboard)