This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
357 lines (319 loc) · 16.3 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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, Event, State
from datetime import datetime as dt
from google.cloud import bigquery
from flask import Flask, request, Response
import pandas_gbq as pd_gbq
import pandas as pd
import urllib.request, json
import plotly.graph_objs as go
mapbox_access_token = 'pk.eyJ1IjoiZ2lsdHJhcG8iLCJhIjoiY2o4eWJyNzY4MXQ1ZDJ3b2JsZHZxb3N0ciJ9.MROnmydnXtfjqjIBtC-P5g'
client = bigquery.Client()
job_config = bigquery.QueryJobConfig()
job_config.use_legacy_sql = False
app = dash.Dash(__name__)
server = app.server
app.config.supress_callback_exceptions = True
from units import *
app.css.append_css({'external_url': 'https://codepen.io/sptkl/pen/MXQKoQ.css'})
control_dict = {
'borough': [{'label': i, 'value': i} for i in borough],
'cb2010': [{'label': i, 'value': str(i)} for i in cb2010],
'ct2010': [{'label': i, 'value': str(i)} for i in ct2010],
'incident_zip':[{'label': i, 'value': str(i)} for i in incident_zip],
'community_board': [{'label': i, 'value': i} for i in community_board],
'agency':[{'label': i, 'value': i} for i in agency],
'category':[{'label': i, 'value': i} for i in category],
'season':[{'label': i, 'value': i} for i in season],
'day_night':[{'label': i, 'value': i} for i in day_night],
'dayofweek':[{'label': i, 'value': i} for i in dayofweek],
'council':[{'label': i, 'value': i} for i in council],
}
app.layout = html.Div(children=[
html.Div(children=[
html.Div([
html.Img(src='https://rawgit.com/SPTKL/Data_Visualization_Project/master/_flavicon_.jpg', height='60')
], className='one column', style={'margin':'15'}),
html.Ul(children=[
html.Li(children=[
html.A('NYC 311 One-Stop Shop',
href='https://cart-gate-pkkx.squarespace.com/',
style={'font-size': '20px'}),
], style={'float':'left'}, className='nav'),
html.Li(children=[
html.A('Manual',
href='https://cart-gate-pkkx.squarespace.com/new-page-1/',
),
], className='nav'),
html.Li(children=[
html.A('Resources',
href='https://cart-gate-pkkx.squarespace.com/new-page/',
)
], className='nav'),
html.Li(children=[
html.A('About US',
href='https://cart-gate-pkkx.squarespace.com/about-us/',
)
], className='nav'),
html.Li(children=[
html.A('Contact',
href='https://cart-gate-pkkx.squarespace.com/contact-us/',
)
], className='nav'),
], className='eleven columns', style={'margin-left':'0', 'padding':'0'}),
], className='row'),
#controls
html.Div(children=[
html.Div(children = [
html.Div([
html.H2('Data Quick Access',
style={'text-align':'center'}),
html.Div([
dcc.Dropdown(
id='geospatial',
options=[{'label': i, 'value': i} for i in ['borough','community_board',
'incident_zip','ct2010', 'cb2010',
'council']],
placeholder="Filter by Administrative Boundry")
], style={'padding': '20px 20px 0px 20px'}),
html.Div([
dcc.Dropdown(
id='geospatial_child',
multi=True)
], style={'padding': '20px 20px 0px 20px'}),
html.Div([
dcc.Dropdown(
id='categorical',
options=[{'label': i, 'value': i} for i in ['category','agency', 'day_night', 'dayofweek', 'season']],
placeholder="Filter by Categories")
], style={'padding': '20px 20px 0px 20px'}),
html.Div([
dcc.Dropdown(
id='categorical_child',
multi=True)
], style={'padding': '20px 20px 0px 20px'}),
html.Div([
dcc.DatePickerRange(
id='date_picker',
clearable=True,
min_date_allowed=dt(2010, 1, 1),
max_date_allowed=dt(2018, 5, 2),
initial_visible_month=dt(2018, 5, 2),
start_date=dt(2010, 5, 2),
end_date=dt(2018, 5, 2)),
], style={'padding': '20px 20px 0px 20px'}),
html.Div([
dcc.RadioItems(
id='aggregation',
options=[{'label': i, 'value': i} for i in ['Aggregate', 'No Aggregate']],
value='No Aggregate',
labelStyle={'display': 'inline-block'})
], style={'padding': '20px 20px 0px 20px'}),
html.Div(id='aggregation_area', children = [
dcc.Dropdown(
id='aggregation_dropdown',
multi=True,
placeholder="Select Aggregation Units",
options=[{'label': i, 'value': i} for i in ['borough','community_board','incident_zip','ct2010', 'cb2010',
'year','month','season','day_and_night',
'category','agency']]),
], style={'padding': '20px 20px 0px 20px'}),
html.Div(children=[
html.A(id = 'download_link', children=[
html.Button('Download',
id='submit',
type='submit',
className='button-primary',
style={'background-color':'rgba(0,221,85,1)',
'border-color' : 'rgba(0,221,85,1)',
'width': '100%'})])
], style={'padding': '20px 20px 20px 20px'})
])
], className='three columns offset-by-one',
style={'margin-top':'30'}),
html.Div([
dcc.Tabs(
tabs=[{'label': item, 'value': item} for item in ['User Guide', 'Agency Plot', 'Category Plot']],
value='User Guide',
id='tabs'),
html.Div(id='tab_output'),
], className='seven columns', style={'margin-top':'30'}),
], className='row'),
html.Div(children=[
html.Div([
html.P(
dcc.Markdown('''Developed by Baiyue Cao - [[email protected]](mailto:[email protected])''')),
], className='footer', style={'text-align':'left'})
], className='ten columns')
])
@app.callback(
Output('geospatial_child', 'options'),
[Input('geospatial', 'value')])
def get_geospatial_child(spatial_unit):
return control_dict[spatial_unit]
@app.callback(
Output('geospatial_child', 'placeholder'),
[Input('geospatial', 'value')])
def get_geospatial_placeholder(spatial_unit):
if spatial_unit is None:
return 'Leave Dropdown Empty to Select All'
else:
return 'Select {}'.format(spatial_unit)
@app.callback(
Output('categorical_child', 'options'),
[Input('categorical', 'value')])
def get_categorical_child(categorical_unit):
return control_dict[categorical_unit]
@app.callback(
Output('categorical_child', 'placeholder'),
[Input('categorical', 'value')])
def get_categorical_placeholder(categorical_unit):
if categorical_unit is None:
return 'Leave Dropdown Empty to Select All'
else:
return 'Select {}'.format(categorical_unit)
@app.callback(
Output('aggregation_area', 'style'),
[Input('aggregation', 'value')])
def get_aggregation(value):
if value == 'No Aggregate':
return {'display': 'none',
'padding': '20px 20px 0px 20px'}
else:
return {'display': 'block',
'padding': '20px 20px 0px 20px'}
@app.callback(
Output('download_link', 'href'),
[],
[State('geospatial', 'value'),
State('geospatial_child', 'value'),
State('categorical', 'value'),
State('categorical_child', 'value'),
State('date_picker', 'start_date'),
State('date_picker', 'end_date'),
State('aggregation_dropdown', 'value'),
State('aggregation', 'value')],
[Event('submit', 'click')])
def build_query(geo, geo_child, cat, cat_child, start_date, end_date, agg_select, agg):
if geo == None or geo_child == None:
geo_query = ''
else:
geo_query = ' '.join(("AND", geo, "in", str(geo_child).replace('[', '(').replace(']', ')')))
if cat == None or cat_child == None:
cat_query = ''
else:
cat_query = ' '.join(("AND", cat, "in", str(cat_child).replace('[', '(').replace(']', ')')))
filter = ' '.join(("SELECT * FROM `nodal-component-204421.311_complaints.complaints`",
"WHERE created_date BETWEEN '{}' AND '{}'".format(str(start_date), str(end_date)), geo_query, cat_query))
if agg == 'No Aggregate' or agg_select == None:
query = filter
else:
agg_query_select = 'SELECT ' + ', '.join(agg_select) + ', count({})'.format(agg_select[0]) + ' from ({})'.format(filter)
agg_query_groupby = ' GROUP BY ' + ', '.join(agg_select)
query = ' '.join((agg_query_select, agg_query_groupby))
return '/download.csv?value={}'.format(query)
@app.callback(
Output('tab_output', 'children'),
[Input('tabs', 'value')],
[State('geospatial', 'value'),
State('geospatial_child', 'value'),
State('categorical', 'value'),
State('categorical_child', 'value'),
State('date_picker', 'start_date'),
State('date_picker', 'end_date')])
def get_tab_output(value, geo, geo_child, cat, cat_child, start_date, end_date):
if geo == None or geo_child == None:
geo_query = ''
else:
geo_query = ' '.join(("AND", geo, "in", str(geo_child).replace('[', '(').replace(']', ')')))
if cat == None or cat_child == None:
cat_query = ''
else:
cat_query = ' '.join(("AND", cat, "in", str(cat_child).replace('[', '(').replace(']', ')')))
filter = ' '.join(("SELECT * FROM `nodal-component-204421.311_complaints.complaints`",
"WHERE created_date BETWEEN '{}' AND '{}'".format(str(start_date), str(end_date)), geo_query, cat_query))
if value == 'Category Plot':
query = 'SELECT category, date_trunc(Date(created_date), MONTH) AS year_month, count(*) as count FROM ({}) GROUP BY year_month, category ORDER BY 1,2;'.format(filter)
df = pd_gbq.read_gbq(query, dialect='standard')
traces = [{'x':df.year_month.unique().tolist(),
'y':df[df.category == cat]['count'].tolist(),
'type': 'line+markers',
'name': cat} for cat in df.category.unique().tolist()]
return html.Div([
dcc.Graph(
id='category_timeseries',
figure= {
'data': traces,
'layout': {
'title': 'Complaint Counts by Master Categories',
'xaxis': {'title': 'Year-month'},
'yaxis': {'title': 'Complaint Count'},
'height': 700
}
})
])
if value == 'Agency Plot':
query = 'SELECT agency, date_trunc(Date(created_date), MONTH) AS year_month, count(*) as count FROM ({}) GROUP BY year_month, agency ORDER BY 1,2;'.format(filter)
df = pd_gbq.read_gbq(query, dialect='standard')
traces = [{'x':df.year_month.unique().tolist(),
'y':df[df.agency == agc]['count'].tolist(),
'type': 'line+markers',
'name': agc} for agc in df.agency.unique().tolist()]
return html.Div([
dcc.Graph(
id='agency_timeseries',
figure= {
'data': traces,
'layout': {
'title': 'Complaint Counts by Agency',
'xaxis': {'title': 'Year-month'},
'yaxis': {'title': 'Complaint Count'},
'height': 700
}
})
])
if value == 'User Guide':
return html.Div([
dcc.Markdown('''
###### __1. Filter by Administrative Areas__
Interested in a specific area of the city? Select an administrative level in the first drop-down menu (i.e. by borough, community board, incident zip code, census track, census block or council). The dropdown directly below dynamically updates where you can select the specific administrative area(s) you are looking for. The tool will select all by default if you do not select any. Learn more about
[how NYC311 data are geocoded](https://cart-gate-pkkx.squarespace.com/news-notes/2018/5/17/heres-another-manual-entry).
'''.replace(' ','')),
dcc.Markdown('''
###### __2. Filter by Category__
Interested in a specific category of data such as city agencies, master complaint categories or temporal groupings (day vs night, weekday vs weekend or season)? Select the type of category from the dropdown. The dropdown directly below dynamically updates where you can select the specific grouping you are looking for. The tool will select all by default if you do not select any. Learn more about
[how master complaint categories are grouped](https://cart-gate-pkkx.squarespace.com/news-notes/2018/5/17/we-3-open-data).
'''.replace(' ','')),
dcc.Markdown('''
###### __3. Filter by Date__
Interested in a specific time frame? By default, the tool selects all the dates but we encourage you to pick your own desired date range. In fact, selecting a specific year or month range would decrease the files size allowing the tool to run faster.
'''.replace(' ','')),
dcc.Markdown('''
###### __4. Aggregate Counts by Comparative Groups__
The aggregate feature allows you to summarize the data by specific groupings. In effect, this calculates the total number of service requests in comparative groups thus making the data ready for quick analysis. Click the Aggregate radius button for the aggregation dropdown menu to appear then select the feature(s) you want to aggregate on. The tool allows you to select and aggregate multiple administrative areas and categories using the same attributes from above. Start with one or two groups to review the output and data structure then add more attributes as needed.
'''.replace(' ','')),
dcc.Markdown('''
###### __5. Visualizations__
Explore the data based on your filter selections by selecting the menu tab “Agency Plot” or “Category Plot.” Note that the datasets used can be further filtered and downloaded directly from the graph.Have fun playing with it!
'''.replace(' ','')),
], style={'padding': '45px 35px 0px 35px', 'font-size': '14px'})
@server.route('/download.csv')
def download_csv():
query = request.args.get('value')
query = query.replace('%20', ' ').replace('%27', "'")
query_job = client.query(
query,
location='US',
job_config=job_config)
def generate():
i = 0
for row in query_job:
if i == 0:
yield ','.join(str(i) for i in row.keys()) + '\n'
i += 1
yield ','.join(str(x) for x in row.values()) + '\n'
return Response(generate(), mimetype = 'text/csv')
if __name__ == '__main__':
app.run_server(debug=True)