maxschulz-COL commited on
Commit
d138a11
·
verified ·
1 Parent(s): a881520

Add KPI dashboard

Browse files
Files changed (1) hide show
  1. app.py +248 -0
app.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Example to show dashboard configuration."""
2
+
3
+ import pandas as pd
4
+ import vizro.models as vm
5
+ from utils._charts import COLUMN_DEFS, KPI, bar, choropleth, line, pie
6
+ from utils._helper import clean_data_and_add_columns
7
+ from vizro import Vizro
8
+ from vizro.actions import filter_interaction
9
+ from vizro.tables import dash_ag_grid
10
+
11
+ # DATA --------------------------------------------------------------------------------------------
12
+ df_complaints = pd.read_csv("https://query.data.world/s/glbdstahsuw3hjgunz3zssggk7dsfu?dws=00000")
13
+ df_complaints = clean_data_and_add_columns(df_complaints)
14
+ vm.Container.add_type("components", KPI)
15
+
16
+ # SUB-SECTIONS ------------------------------------------------------------------------------------
17
+ kpi_banner = vm.Container(
18
+ id="kpi-banner",
19
+ title="",
20
+ components=[
21
+ # Note: For some KPIs the icon/sign go in opposite directions as an increase e.g. in complaints is negative
22
+ KPI(
23
+ title="Total Complaints",
24
+ value="75.513",
25
+ icon="arrow_circle_up",
26
+ sign="delta-neg",
27
+ ref_value="6.8% vs. LY",
28
+ ),
29
+ KPI(
30
+ title="Closed Complaints",
31
+ value="99.6%",
32
+ icon="arrow_circle_up",
33
+ sign="delta-pos",
34
+ ref_value="+0.2% vs. LY",
35
+ ),
36
+ KPI(
37
+ title="Open Complaints",
38
+ value="0.4%",
39
+ icon="arrow_circle_down",
40
+ sign="delta-pos",
41
+ ref_value="-0.2% vs. LY",
42
+ ),
43
+ KPI(
44
+ title="Timely Response",
45
+ value="98.1%",
46
+ icon="arrow_circle_up",
47
+ sign="delta-pos",
48
+ ref_value="+10.5% vs. LY",
49
+ ),
50
+ KPI(
51
+ title="Closed w/o cost",
52
+ value="84.5%",
53
+ icon="arrow_circle_down",
54
+ sign="delta-neg",
55
+ ref_value="-8.5% vs. LY",
56
+ ),
57
+ KPI(
58
+ title="Consumer disputed",
59
+ value="9.5%",
60
+ icon="arrow_circle_up",
61
+ sign="delta-neg",
62
+ ref_value="+2.3% vs. LY",
63
+ ),
64
+ ],
65
+ )
66
+
67
+ bar_charts_tabbed = vm.Tabs(
68
+ tabs=[
69
+ vm.Container(
70
+ title="By Issue",
71
+ components=[
72
+ vm.Graph(
73
+ figure=bar(
74
+ data_frame=df_complaints,
75
+ y="Issue",
76
+ x="Complaint ID",
77
+ ),
78
+ )
79
+ ],
80
+ ),
81
+ vm.Container(
82
+ title="By Product",
83
+ components=[
84
+ vm.Graph(
85
+ figure=bar(
86
+ data_frame=df_complaints,
87
+ y="Product",
88
+ x="Complaint ID",
89
+ ),
90
+ )
91
+ ],
92
+ ),
93
+ vm.Container(
94
+ title="By Channel",
95
+ components=[
96
+ vm.Graph(
97
+ figure=bar(
98
+ data_frame=df_complaints,
99
+ y="Channel",
100
+ x="Complaint ID",
101
+ ),
102
+ )
103
+ ],
104
+ ),
105
+ vm.Container(
106
+ title="By Region",
107
+ components=[
108
+ vm.Graph(
109
+ figure=bar(
110
+ data_frame=df_complaints,
111
+ y="Region",
112
+ x="Complaint ID",
113
+ ),
114
+ )
115
+ ],
116
+ ),
117
+ ],
118
+ )
119
+
120
+ # PAGES --------------------------------------------------------------------------------------
121
+ page_exec = vm.Page(
122
+ title="Executive View",
123
+ layout=vm.Layout(
124
+ grid=[
125
+ [0, 0],
126
+ [1, 2],
127
+ [1, 2],
128
+ [1, 3],
129
+ [1, 3],
130
+ ],
131
+ ),
132
+ components=[
133
+ kpi_banner,
134
+ bar_charts_tabbed,
135
+ vm.Graph(figure=line(data_frame=df_complaints, y="Complaint ID", x="Year-Month Received")),
136
+ vm.Graph(
137
+ figure=pie(
138
+ data_frame=df_complaints[df_complaints["Company response - Closed"] != "Not closed"],
139
+ custom_order=[
140
+ "Closed with explanation",
141
+ "Closed without relief",
142
+ "Closed with non-monetary relief",
143
+ "Closed with relief",
144
+ "Closed with monetary relief",
145
+ ],
146
+ values="Complaint ID",
147
+ names="Company response - Closed",
148
+ title="Closed company responses",
149
+ )
150
+ ),
151
+ ],
152
+ )
153
+
154
+ page_region = vm.Page(
155
+ title="Regional View",
156
+ layout=vm.Layout(grid=[[0, 0]] + [[1, 2]] * 4),
157
+ components=[
158
+ vm.Card(
159
+ text="""
160
+ ##### Click on a state inside the map to filter the bar charts on the right.
161
+
162
+ - Which state has the most complaints?
163
+ - What are the three biggest issues in California?
164
+ - What is the product with the most complaints in Texas?
165
+ """
166
+ ),
167
+ vm.Graph(
168
+ figure=choropleth(
169
+ data_frame=df_complaints,
170
+ locations="State",
171
+ color="Complaint ID",
172
+ title="Complaints by State",
173
+ custom_data=["State"],
174
+ ),
175
+ actions=[
176
+ vm.Action(
177
+ function=filter_interaction(targets=["regional-issue", "regional-product"]),
178
+ )
179
+ ],
180
+ ),
181
+ vm.Tabs(
182
+ tabs=[
183
+ vm.Container(
184
+ title="By Issue",
185
+ components=[
186
+ vm.Graph(
187
+ id="regional-issue",
188
+ figure=bar(
189
+ data_frame=df_complaints,
190
+ y="Issue",
191
+ x="Complaint ID",
192
+ ),
193
+ )
194
+ ],
195
+ ),
196
+ vm.Container(
197
+ title="By Product",
198
+ components=[
199
+ vm.Graph(
200
+ id="regional-product",
201
+ figure=bar(
202
+ data_frame=df_complaints,
203
+ y="Product",
204
+ x="Complaint ID",
205
+ ),
206
+ )
207
+ ],
208
+ ),
209
+ ],
210
+ ),
211
+ ],
212
+ controls=[
213
+ vm.Filter(column="Region", selector=vm.Checklist()),
214
+ vm.Filter(column="State"),
215
+ vm.Filter(column="Product"),
216
+ vm.Filter(column="Issue"),
217
+ ],
218
+ )
219
+
220
+ page_table = vm.Page(
221
+ title="List of complaints",
222
+ components=[
223
+ vm.AgGrid(
224
+ figure=dash_ag_grid(
225
+ data_frame=df_complaints,
226
+ columnDefs=COLUMN_DEFS,
227
+ dashGridOptions={"pagination": True},
228
+ )
229
+ )
230
+ ],
231
+ )
232
+
233
+ dashboard = vm.Dashboard(
234
+ pages=[page_exec, page_region, page_table],
235
+ title="Cumulus Financial Corporation",
236
+ navigation=vm.Navigation(
237
+ nav_selector=vm.NavBar(
238
+ items=[
239
+ vm.NavLink(label="Executive View", icon="Leaderboard", pages=["Executive View"]),
240
+ vm.NavLink(label="Regional View", icon="South America", pages=["Regional View"]),
241
+ vm.NavLink(label="Table View", icon="Table View", pages=["List of complaints"]),
242
+ ]
243
+ )
244
+ ),
245
+ )
246
+
247
+ if __name__ == "__main__":
248
+ Vizro().build(dashboard).run()