-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
400 lines (382 loc) · 16.8 KB
/
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import pandas as pd
from dash import Dash, dcc, html, Input, Output, State
import plotly.express as px
import dash_bootstrap_components as dbc
from dash_bootstrap_templates import load_figure_template
import dash_daq as daq
import random
data = pd.read_csv("assets/data/van_houses.csv")
logo = html.Img(
src="assets/img/logo.png",
style={"width": "100%", "align": "center", "paddingBottom": "20px"},
)
title = html.H1(
"Vancouver Housing App",
style={"textAlign": "center", "fontWeight": "bold", "color": "white"},
)
# Create the app
app = Dash(__name__, external_stylesheets=[dbc.themes.LUX])
server = app.server
load_figure_template("darkly")
# Define the layout
app.layout = html.Div(
[
dbc.Row(
[
dbc.Col(
[
logo,
dbc.Row(
[
html.Label("Select Communities:"),
dcc.Dropdown(
id="geo-dropdown",
options=[{"label": "All", "value": "all"}]
+ [
{"label": x, "value": x}
for x in sorted(
list(set(data["Geo Local Area"]))
)
],
value=["Downtown", "Kerrisdale",
"Marpole", "Kitsilano"],
multi=True,
placeholder="Select a community",
),
daq.BooleanSwitch(
id='boolean-switch',
on=True,
label="Show fractioned data",
labelPosition="left"
),
html.Div(id='output'),
dbc.Modal(
[
dbc.ModalHeader("Warning"),
dbc.ModalBody("The map on default shows 10% of the full data points. Turning the switch off will display all data and may take a long time to load."),
],
id="warning-modal",
centered=True,
is_open=False,
)
],
style={"paddingBottom": "20px"},
),
dbc.Row(
[
html.Label("Select Property Type:"),
dcc.Dropdown(
id="zoning-dropdown",
options=[{"label": "All", "value": "all"}]
+ [
{"label": x, "value": x}
for x in sorted(
list(
set(data["zoning_classification"]))
)
],
value='all',
multi=True,
placeholder="Select a property type",
),
],
style={"paddingBottom": "20px"},
),
dbc.Row(
[
html.Label("Select Built Year:"),
dcc.RangeSlider(
min(data["year_built"]),
max(data["year_built"]),
1,
id="yearbuilt-slider",
value=[
min(data["year_built"]),
max(data["year_built"]),
],
marks=None,
tooltip={
"placement": "bottom",
"always_visible": True,
},
),
],
style={"paddingBottom": "20px"},
),
dbc.Row(
[
html.Label("Select Improvement Year:"),
dcc.RangeSlider(
min(data["big_improvement_year"]),
max(data["big_improvement_year"]),
1,
id="yearimprov-slider",
value=[
min(data["big_improvement_year"]),
max(data["big_improvement_year"]),
],
marks=None,
tooltip={
"placement": "bottom",
"always_visible": True,
},
),
],
style={"paddingBottom": "20px"},
),
],
# vertical=True,
# pills=True,
style={
"background-color": "rgba(248, 249, 250, 0.9)",
"border-right": "1px solid #dee2e6",
"padding": "20px",
"height": "95vh",
"width": "250px",
"margin": "0",
"opacity": "0.9",
},
md=2,
sm=2,
),
dbc.Col(
[
# Side bar
# Title
dbc.Row(
title,
align="center",
justify="center",
style={"height": "5%"},
),
# Graphs
dbc.Row(
dcc.Loading(
id="card-loading",
children=dbc.Card(
[
dbc.CardHeader(
html.H4(
"Property maps and trends"),
style={'height': '10px'}
),
dbc.CardBody(
[
dcc.Graph(
id="map",
style={
"display": "inline-block",
"padding": "0",
"margin": "0",
"width": "50%",
"height": "325px",
"opacity": 0.9,
},
),
dcc.Graph(
id="trends",
style={
"display": "inline-block",
"padding": "0",
"margin": "0",
"width": "50%",
"height": "325px",
"opacity": 0.9,
},
),
]
),
],
color="rgba(0,0,0, 0.7)",
inverse=True,
style={'width': '100%',
'margin': '0 auto', 'height': '330px'},
),
type="circle",
)
),
dbc.Row(
dcc.Loading(
id="card-loading2",
children=dbc.Card(
[
dbc.CardHeader(
html.H4(
"Property Types in Selected Region"),
style={'height': '10px'}
),
dbc.CardBody(
[
dcc.Graph(
id="histogram",
style={
"display": "inline-block",
"padding": "0",
"margin": "0",
"width": "65%",
"height": "325px",
"opacity": 0.9,
},
),
dcc.Graph(
id="piechart",
style={
"display": "inline-block",
"padding": "0",
"margin": "0",
"width": "33%",
"height": "325px",
},
),
]
),
],
color="rgba(0,0,0, 0.7)",
inverse=True,
style={'width': '100%',
'margin': '0 auto', 'height': '330px'},
),
type="circle",
)
),
],
style={
"padding": "10px 10px",
},
md=9,
sm=9,
),
]
),
html.Footer(
html.Div(
[
html.P("Copyright © 2023 HanChen Wang, UBC Master of Data Science"),
],
),
),
],
style={
"padding": "10px",
"background-image": 'url("assets/img/IMG_5724.png")',
"background-size": "cover",
"height": "100vh"
},
)
# Define callbacks
@app.callback(Output("warning-modal", "is_open"),
[Input('boolean-switch', 'on')])
def toggle_warning_modal(on):
if not on:
return True
return False
@ app.callback(
[
Output("map", "figure"),
Output("trends", "figure"),
Output("histogram", "figure"),
Output("piechart", "figure"),
],
[Input("boolean-switch", "on"),
Input("geo-dropdown", "value"),
Input("zoning-dropdown", "value"),
Input("yearbuilt-slider", "value"),
Input("yearimprov-slider", "value")],
)
def update_graph(on, geo_values, zoning_values, yearbuilt_value, yearimprov_value):
min_yearbuilt, max_yearbuilt = yearbuilt_value[0], yearbuilt_value[1]
min_yearimprov, max_yearimprov = yearimprov_value[0], yearimprov_value[1]
filtered_data = data.query(
"@min_yearbuilt <= year_built & year_built <= @max_yearbuilt & @min_yearimprov <= year_built & year_built <= @max_yearimprov"
)
if not "all" in geo_values:
filtered_data = filtered_data.loc[
(filtered_data["Geo Local Area"].isin(geo_values))
]
if not "all" in zoning_values:
filtered_data = filtered_data.loc[
(filtered_data["zoning_classification"].isin(zoning_values))
]
# Do random sampling on the filtered_data to plot on map
# Load the data and only randomly pick 10% of the data
random.seed(532)
if not on:
map_data = filtered_data.copy().sample(frac=0.1)
else:
map_data = filtered_data.copy()
fig1 = px.scatter_mapbox(
map_data,
lat="latitude",
lon="longitude",
color="current_land_value",
hover_name="full_address",
hover_data=["current_land_value"],
center={"lat": 49.2527, "lon": -123.120},
color_continuous_scale="Agsunset",
range_color=[0, 5000000],
zoom=10,
labels={"current_land_value": "Value ($)"},
)
fig1.update_layout(
mapbox_style="carto-positron",
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
)
# Update trendline
trend_data = pd.DataFrame(filtered_data.groupby(["Geo Local Area", "tax_assessment_year"]).mean(
numeric_only=True)['current_land_value']).reset_index()
trend_data['tax_assessment_year'] = trend_data['tax_assessment_year'].astype(
int)
fig2 = px.line(trend_data, x="tax_assessment_year",
y="current_land_value", color="Geo Local Area")
# title="Average Current Land Value by Geo Local Area and Tax Assessment Year")
fig2.update_xaxes(title="Year")
fig2.update_yaxes(title="Value ($)")
fig2.update_layout(xaxis={'tickmode': 'array', 'tickvals': [
'2020', '2021', '2022', '2023']},
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)")
# Setting color dictionary for fig3 and 4.
color_dict = {
"Comprehensive Development": "#1f77b4",
"Single Detached House": "#ff7f0e",
"Duplex": "#2ca02c",
"One-Family Dwelling": "#d62728",
"Two-Family Dwelling": "#9467bd",
"Multiple Dwelling": "#8c564b",
"Commercial": "#e377c2",
"Industrial": "#7f7f7f",
"Historical Area": "#bcbd22",
"Limited Agriculture": "#17becf",
"Other": "#ff0000"
}
# Update histogram
fig3 = px.histogram(
filtered_data.query("current_land_value <=5000000"),
x="current_land_value",
color="zoning_classification",
range_x=[0, 5000000],
nbins=20,
color_discrete_map=color_dict,
barmode='overlay',
)
fig3.update_yaxes(title="Number of properties")
fig3.update_xaxes(title="Value ($)")
fig3.update_layout(
paper_bgcolor="rgba(0,0,0,0)", plot_bgcolor="rgba(255,255,255,0.8)",
legend=dict(title="Property Types", bgcolor="rgba(0,0,0,0)")
)
# Update pie chart
df = filtered_data["zoning_classification"].value_counts()
fig4 = px.pie(df, values=df.values, names=df.index, hole=0.3,
color=df.index, color_discrete_map=color_dict)
fig4.update_traces(
textfont_size=15, marker=dict(line=dict(color="#000000", width=1.5))
)
fig4.update_layout(paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
showlegend=False)
return fig1, fig2, fig3, fig4
# Run the app
if __name__ == "__main__":
app.run_server(debug=True)