-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
399 lines (312 loc) · 11.5 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
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_table
from dash.dependencies import Input, Output, State
from dash_table.Format import Format, Scheme, Sign, Symbol
import dash_table.FormatTemplate as FormatTemplate
import pandas as pd
import plotly.graph_objs as go
import re
import json
import urllib
import urllib.parse
from functools import reduce
mapbox_access_token = 'pk.eyJ1IjoiY2FscmVtbWVsIiwiYSI6ImNqc25scWtiMzBkcGI0M3BtNDRrbnFvNGoifQ.qmi7OtQn6vIJbHbbTZs2MQ'
# Define format for table columns, including currency formatting.
TABLE_COLS = ["Parcel ID", "Address", "Property Value", "Listed Owner", "Principals"]
FORMAT_COLS = [{"name": i, "id": i} for i in TABLE_COLS]
FORMAT_COLS[2]['type'] = 'numeric'
FORMAT_COLS[2]['format'] = FormatTemplate.money(0)
# Load data.
geocodes = pd.read_csv('data/csvs/geocoded_properties.csv')
principals = pd.read_csv('data/csvs/props_to_principals.csv')
# Define color list for city wards.
colors = ["rgba(251,180,174,0.5)", "rgba(179,205,227,0.5)", "rgba(204,235,197,0.5)", "rgba(222,203,228,0.5)", "rgba(254,217,166,0.5)", "rgba(255,255,204,0.5)", "rgba(229,216,189,0.5)", "rgba(253,218,236,0.5)"]
# GeoJSON files for city wards.
files = ['ward1.json', 'ward2.json', 'ward3.json', 'ward4.json', 'ward5.json', 'ward6.json', 'ward7.json', 'ward8.json']
all_wards = ['Ward 1', 'Ward 2', 'Ward 3', 'Ward 4', 'Ward 5', 'Ward 6', 'Ward 7', 'Ward 8']
# load GeoJSON layers for map
layers_one = []
for i, name in enumerate(files):
layers_one.append(
dict(
sourcetype = 'geojson',
source = json.load(open("data/wards/" + name)),
type = 'fill',
color = colors[i],
below="road_major_label"
)
)
app = dash.Dash(__name__)
server = app.server
app.layout = html.Div(children=[
html.H1(children='Who Owns Burlington?'),
html.Div(children=[
html.H3('Enter Last Name(s):'),
html.P('Separate names by commas.'),
dcc.Input(id='my-id', value='handy, bissonette, pomerleau', type='text')]),
html.Div(children=[
html.H3('Filter by Ward:'),
dcc.Dropdown(
id='ward-select',
options=[
{'label': 'Ward 1', 'value': 'Ward 1'},
{'label': 'Ward 2', 'value': 'Ward 2'},
{'label': 'Ward 3', 'value': 'Ward 3'},
{'label': 'Ward 4', 'value': 'Ward 4'},
{'label': 'Ward 5', 'value': 'Ward 5'},
{'label': 'Ward 6', 'value': 'Ward 6'},
{'label': 'Ward 7', 'value': 'Ward 7'},
{'label': 'Ward 8', 'value': 'Ward 8'},
],
value=None,
multi=True
)
]),
html.Div(children=[
dcc.Graph(
id ='map'
)
]),
html.Div(children=[
dcc.Graph(
id ='ward-bars'
)
]),
html.A(
'Download Data',
id='download-link',
download="rawdata.csv",
href="",
target="_blank"
),
html.Div(children=[
dash_table.DataTable(
id='table',
columns = FORMAT_COLS,
sorting=True
)
])
])
@app.callback(
Output('download-link', 'href'),
[Input('my-id', 'n_submit'), Input('ward-select', 'value')],
[State('my-id', 'value')])
def update_download_link(ns, ward_list, name_string):
dff = generate_table(ward_list, name_string)
csv_string = dff.to_csv(index=False, encoding='utf-8')
csv_string = "data:text/csv;charset=utf-8," + urllib.parse.quote(csv_string)
return csv_string
@app.callback(
Output('ward-bars', 'figure'),
[Input('my-id', 'n_submit'), Input('ward-select', 'value')],
[State('my-id', 'value')])
def update_bars(ns, ward_list, name_string):
if ward_list == None:
ward_list = all_wards
if name_string == "":
traces = [go.Bar(x=ward_list, y=[0 for ward in ward_list])]
else:
name_list = name_lister(name_string)
traces = []
for name in name_list:
ward_counts = get_ward_counts(name, ward_list)
trace = go.Bar(
x=ward_list,
y=ward_counts,
name=name.title()
)
traces.append(trace)
return {
'data': traces
}
@app.callback(
Output('map', 'figure'),
[Input('my-id', 'n_submit'), Input('ward-select', 'value')],
[State('my-id', 'value')])
def update_map(ns, ward_list, name_string):
marker_colors = ['rgb(215,25,28)','rgb(253,174,97)','rgb(255,255,191)','rgb(171,217,233)','rgb(44,123,182)']
if name_string == "":
traces = [go.Scattermapbox()]
else:
name_list = name_lister(name_string)
traces = []
for i, name in enumerate(name_list):
lats, lngs, text = get_marker_data(name, ward_list)
trace = go.Scattermapbox(
lat=lats,
lon=lngs,
mode='markers',
marker=dict(
size=8
# color=marker_colors[i]
),
text=text,
name=name.title()
)
traces.append(trace)
return {
'data': traces,
'layout': go.Layout(
height=800,
autosize=True,
hovermode='closest',
mapbox=dict(
accesstoken=mapbox_access_token,
bearing=0,
center=dict(
lat=44.495,
lon=-73.23
),
pitch=0,
zoom=11.45,
layers = layers_one
)
)
}
def generate_table(ward_list, name_string):
if name_string == '':
empty_df = pd.DataFrame()
return empty_df
name_list = []
if "," not in name_string:
name_list.append(name_string)
else:
name_split = name_string.split(",")
name_list = [x.strip() for x in name_split]
df_list = []
for name in name_list:
df=name_search(name)
df_list.append(df)
if len(df_list) == 1:
combined_df = df_list[0]
else:
combined_df = pd.concat(df_list)
final_df = pd.merge(combined_df, geocodes, how='left', left_on='property_id', right_on='Parcel ID')
if ward_list:
ward_arrays = [final_df[x] for x in ward_list]
ward_bool = reduce((lambda x, y: x | y), ward_arrays)
final_df = final_df[ward_bool]
final_df = final_df[["property_id", "Address", "property_real_value", "owner_name", "principals"]]
cols = ["Parcel ID", "Address", "Property Value", "Listed Owner", "Principals"]
final_df.columns = cols
final_df["Listed Owner"] = final_df["Listed Owner"].str.title()
final_df["Principals"] = final_df["Principals"].str.title()
return final_df
@app.callback(
Output('table', 'data'),
[Input('my-id', 'n_submit'), Input('ward-select', 'value')],
[State('my-id', 'value')])
def update_table(ns, ward_list, name_string):
final_df = generate_table(ward_list, name_string)
return final_df.to_dict("rows")
def findWholeWord(word, sentence):
"""Finds a complete word in a sentence.
Args:
word (str): Word to be searched for.
sentence (str): Sentence to be searched.
Returns:
bool: True if found, False otherwise.
"""
result = re.compile(r'\b({0})\b'.format(word), flags=re.IGNORECASE).search(sentence)
if result:
return True
else:
return False
def currency(number):
'''Formats integer or float as currency.'''
return '${:,.2f}'.format(number)
def principal_string(id, df=principals):
"""Returns a string of principal names, separated by commas.
Args:
id (str): Property id to be matched.
df (DataFrame): Dataframe to be searched.
Returns:
str: Comma separated principal names.
"""
df=df
prin_series = df.loc[df['property_id'] == id, 'principals']
prin_list = sorted(list(prin_series.unique()))
prin_string = ", ".join(prin_list)
return prin_string
def name_search(name, df=principals, institutions=False):
"""Finds properties associated with a name.
Args:
name (str): Name to be searched for.
df (DataFrame): DataFrame to be searched.
institutions (bool): Include major gov institutions only if True.
Returns:
data (DataFrame): Properties that match name.
"""
df=df
name=name.upper()
inst_lst = [45152.0, 43409.0, 46484.0, 46891.0]
principal_bool = df.principals.map(lambda x: findWholeWord(name, x))
owner_bool = df.owner_name.map(lambda x: findWholeWord(name, x))
data = df[
principal_bool | owner_bool
].sort_values(
['property_id', 'owner_name', 'principals']
).drop_duplicates(subset=['property_id'])
if institutions == False:
data = data[data['business_id'].isin(inst_lst) == False]
data['principals'] = data['property_id'].map(principal_string)
return data
def get_marker_data(name, ward_list):
"""Returns makers for properties associated with name.
Args:
name (str): Name to be searched for.
ward_list (list): List of city wards to filter by.
Returns:
lats (Series): Series of latitudes.
lngs (Series): Series of longitudes.
text (Series): Series of strings with addresses, owners, values.
"""
df = name_search(name)
data = pd.merge(df, geocodes, how='left', left_on='property_id', right_on='Parcel ID')
if ward_list:
ward_arrays = [data[x] for x in ward_list]
ward_bool = reduce((lambda x, y: x | y), ward_arrays)
data = data[ward_bool]
lats = data.Latitude
lngs = data.Longitude
text1 = data.owner_name
text2 = data['Listed Real Value'].map(currency)
text3 = data.Address
text = text3 + "<br>" + "<b>Listed Owner:</b> " + text1 + "<br>" + "<b>Value:</b> " + text2
return lats, lngs, text
def name_lister(name_string):
"""Creates a formatted list of names for use in other functions.
Args:
name_string (str): String of comma-separated names.
Returns:
name_list (list): List of names.
"""
name_list = []
if "," not in name_string:
name_list.append(name_string)
else:
name_split = name_string.split(",")
name_list = [x.strip() for x in name_split]
return name_list
def get_ward_counts(name, ward_list):
"""Gets ward counts properties associated with name.
Args:
name (str): Last name associated with properties.
ward_list (array): List of city wards for filtering.
Returns:
ward_counts (array): List of integers.
"""
# Load data.
df = name_search(name)
data = pd.merge(df, geocodes, how='left', left_on='property_id', right_on='Parcel ID')
# Filter wards.
ward_arrays = [data[x] for x in ward_list]
ward_bool = reduce((lambda x, y: x | y), ward_arrays)
data = data[ward_bool]
ward_counts = [sum(data[ward]) for ward in ward_list]
return ward_counts
if __name__ == '__main__':
app.run_server(debug=True)