-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
262 lines (207 loc) · 8.43 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
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
#import dash_table
import pandas as pd
import plotly.graph_objects as go
import plotly.express as px
import warnings
import sys
#import re
#import numpy as np
#import urllib
#from urllib.request import urlopen
import json
from geopy.geocoders import Nominatim
import os
#import base64
#import io
#import ast
geolocator = Nominatim(user_agent="my_user_agent")
#########################################################################################
################################# CONFIG APP ############################################
#########################################################################################
warnings.filterwarnings('ignore')
px.set_mapbox_access_token('pk.eyJ1Ijoia2xvY2V5IiwiYSI6ImNrYm9uaWhoYjI0ZDcycW56ZWExODRmYzcifQ.Mb27BYst186G4r5fjju6Pw')
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.config.suppress_callback_exceptions = True
#########################################################################################
################################# LOAD DATA #############################################
#########################################################################################
ls = []
with open('./data/geojson/Boundaries - Community Areas (current).geojson') as f:
com_area_geo = json.load(f)
for feature in com_area_geo["features"]:
com_area = feature['properties']['community']
feature['id'] = com_area
ls.append(com_area)
js = com_area_geo
df = pd.DataFrame(columns=['community'])
df['community'] = ls
clrs = [0] * len(ls)
for i, val in enumerate(ls):
if val == 'AUSTIN':
clrs[i] = 10
df['num'] = clrs
#########################################################################################
################# DASH APP CONTROL FUNCTIONS ############################################
#########################################################################################
def generate_control_card1():
"""
:return: A Div containing controls for graphs.
"""
return html.Div(
id="control-card1",
children=[
html.B("Enter an address and press Return", style={'textAlign': 'left', 'color': '#3385ff'}),
html.Br(),
dcc.Input(
id="address1",
type="text",
debounce = True,
value='545 N Pine Ave, Chicago, IL 60644',
placeholder="Street address",
style={
'textAlign': 'left',
'width': '100%',
},
),
],
)
def description_card1():
"""
:return: A Div containing dashboard title & descriptions.
"""
return html.Div(
id="description-card1",
children=[
html.H3("Location finder",
style={'textAlign': 'left', 'color': '#3385ff'}),
html.P("This tool allows the user to type in a street address and to know which" +
" Chicago community area (e.g., Austin) the address is located within.",
style={
'textAlign': 'left',
}),
],
)
#########################################################################################
################################# DASH APP LAYOUT #######################################
#########################################################################################
app.layout = html.Div([
dcc.Tabs([
dcc.Tab(label='Find a location', children=[
html.Div(
id='df1',
style={'display': 'none'}
),
html.Div(
style={'background-color': '#f9f9f9'},
id="banner12",
className="banner",
children=[html.Img(src=app.get_asset_url("RUSH_full_color.jpg"),
style={'textAlign': 'left'}),
],
),
html.Div(
id="left-column1",
className="five columns",
children=[description_card1(), generate_control_card1()],
style={'width': '24%', 'display': 'inline-block',
'border-radius': '15px',
'box-shadow': '1px 1px 1px grey',
'background-color': '#f0f0f0',
'padding': '10px',
'margin-bottom': '10px',
},
),
html.Div(
id="right-column1",
className="eight columns",
children=[
html.Div(
id="map1",
children=[
dcc.Graph(id="map_plot1"),
],
style={'width': '100%', 'display': 'inline-block',
'border-radius': '15px',
'box-shadow': '1px 1px 1px grey',
'background-color': '#f0f0f0',
'padding': '10px',
'margin-bottom': '20px',
},
),
]),
]),
]),
])
#########################################################################################
############################ Call backs ############################################
#########################################################################################
############################ Update Tab1 ###########################################
@app.callback( # Update available sub_categories
Output("map_plot1", "figure"),
[
Input("address1", "value"),
],
)
def update_output1(v1):
if v1 == None:
lat = 41.8902467
lon = -87.76294496956751
try:
loc = geolocator.geocode(v1)
lat = loc.latitude
lon = loc.longitude
except:
lat = 41.8902467
lon = -87.76294496956751
v1 = None
figure = px.choropleth_mapbox(df,
geojson = js,
locations = 'community',
color = 'num',
color_continuous_scale = "Blues",
mapbox_style="carto-positron",
zoom=11.7, center = {"lat": lat, "lon": lon},
opacity=0.6,
#hover_data=[lab, 'No. of times greater than average'],
)
figure.update_layout(
autosize=True,
coloraxis_showscale=False,
showlegend=False,
hovermode='closest',
mapbox_style="light",
height=500,
margin={"r":0,"t":0,"l":0,"b":0},
)
if v1 == None or v1 == '':
pass
else:
tdf = pd.DataFrame(columns=['Latitude', 'Longitude'])
tdf['Latitude'] = [lat]
tdf['Longitude'] = [lon]
figure.add_trace(go.Scattermapbox(
lon = tdf['Longitude'],
lat = tdf['Latitude'],
text = v1,
marker = dict(
size = 20,
color = '#ff0066',
opacity = 0.9,
#line=dict(
# width=2,
# color='DarkSlateGrey')
),
),
)
return figure
#########################################################################################
############################# Run the server ############################################
#########################################################################################
if __name__ == "__main__":
app.run_server(host='127.0.0.1', port=8050, debug=True)