forked from austinlasseter/flying-dog-beers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovin-On-UP1.py
402 lines (361 loc) · 13.1 KB
/
Movin-On-UP1.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
401
402
# -*- coding: utf-8 -*-
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
#import dash_table_experiments as dt
import pandas as pd
import numpy as np
import glob
from datetime import datetime
import dash_table as dt
from plotly import graph_objs as go
import plotly.plotly as py
from plotly.graph_objs import *
from uszipcode import Zipcode
from uszipcode import SearchEngine
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="Movin\'OnUp") #"specify_your_app_name_here"
#data from US CENSUS
search = SearchEngine(simple_zipcode=True)
# def update_table(userforecast, minMSP, maxMSP):
# now = datetime.today().strftime('%Y')
# table = userforecast[['zipcode','trend','trend_lower','trend_upper']].loc[userforecast['ds']==now]
# featuresWithinBudget = table[np.logical_and(table['trend'] >=minMSP,table['trend'] < maxMSP)]
# return featuresWithinBudget
#external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__) #__name__, external_stylesheets=external_stylesheets
app.title = 'Movin\'OnUP!'
# API keys and datasets
mapbox_access_token = 'pk.eyJ1IjoiZHluZGwiLCJhIjoiY2p4M2gyYm9wMDBzbDRhbmxzYWMya2tvZCJ9.xWu9JsGNMrFmk6yiydXlqw'
# loading data
path = '/Users/dmlee/Desktop/Insight_DS/data/'
#path = r'C:\DRO\DCL_rawdata_files' # use your path
all_files = glob.glob(path + 'ZILLOW/Z*_MSPAHforecast.csv')
li = []
# for zipcode in LA_zipcodes:
# MSPfile = 'ZILLOW/Z'+zipcode+'_MSPAH.csv' #MSP
# forecaster = 'ZILLOW/Z'+zipcode+'_MSPAH'+'forecast'+'.csv'
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
LA_zipcode = filename.split('_', 2)[1].split('Z')[2]
df['zipcode'] = LA_zipcode
lat_lon = search.by_zipcode(LA_zipcode).values()[7:9]
df['Latitude'] = lat_lon[0]
df['Longitude'] = lat_lon[1]
li.append(df)
userforecast = pd.concat(li, axis=0, ignore_index=True)
map_data = userforecast
#set initial values for min and max
minMSP = 0
maxMSP = 2000000
colors = {
'background': '#111111',
'text': '#7FDBFF',
'light_text': '#D8D8D8'
}
# Boostrap CSS.
app.css.append_css({'external_url': 'https://codepen.io/amyoshino/pen/jzXypZ.css'})
app.config['suppress_callback_exceptions']=True
# Layouts
layout_table = dict(
autosize=True,
height=500,
font=dict(color="#191A1A"),
titlefont=dict(color="#191A1A", size='14'),
margin=dict(
l=35,
r=35,
b=35,
t=45
),
hovermode="closest",
plot_bgcolor='#fffcfc',
paper_bgcolor='#fffcfc',
legend=dict(font=dict(size=10), orientation='h'),
)
layout_table['font-size'] = '12'
layout_table['margin-top'] = '20'
layout_map = dict(
autosize=True,
height=500,
font=dict(color="#191A1A"),
titlefont=dict(color="#191A1A", size='14'),
margin=dict(
l=35,
r=35,
b=35,
t=45
),
hovermode="closest",
plot_bgcolor='#fffcfc',
paper_bgcolor='#fffcfc',
legend=dict(font=dict(size=10), orientation='h'),
title='LA Home Location Desirables by Zipcode',
mapbox=dict(
accesstoken=mapbox_access_token,
style="light",
center=dict(
lon=-118.2437,
lat=34.0522
),
zoom=10,
)
)
# functions
def gen_map(map_data):
# groupby returns a dictionary mapping the values of the first field
# 'classification' onto a list of record dictionaries with that
# classification value.
return {
"data": [{
"type": "scattermapbox",
"lat": list(map_data['Latitude']),
"lon": list(map_data['Longitude']),
"hoverinfo": "text",
"hovertext": [["Name: {} <br>Type: {} <br>Provider: {}".format(i,j,k)]
for i,j,k in zip(map_data['Name'], map_data['Type'],map_data['Provider'])],
"mode": "markers",
"name": list(map_data['Name']),
"marker": {
"size": 6,
"opacity": 0.7
}
}],
"layout": layout_map
}
#__________________________________
#__________________________________
app.layout = html.Div([
#headers and initial user input
#------------------------------
#html.Div([
# html.Div([
# html.H1(children='Movin\'OnUP!',
# style={
# 'textAlign': 'left',
# 'color': colors['text']},
# className = "nine columns"
# ),
# html.Img(
# src="https://assets-global.website-files.com/575a31d2ce5d01dc7a20de45/575a31d2ce5d01dc7a20ded3_insight_logo.png",
# className='three columns',
# style={
# 'height': '14%',
# 'width': '14%',
# 'float': 'right',
# 'position': 'relative',
# 'margin-top': 20,
# 'margin-right': 20
# },
# ),
# html.Div(children='''A home pre-search optimization app''',
# style={
# # 'height': '14%',
# # 'width': '28',
# # 'float': 'left',
# # # 'position': 'fixed',
# 'margin-top': 15,
# 'margin-right': 15,
# 'margin-bottom': 15,
# 'textAlign': 'left',
# 'color': colors['text']
# },
# className = 'nine columns'
# ),
# ], className = "row"
# )
# ]),
#Left side
#---------
# html.Div([
# html.Div([
# dcc.Input(id='minMSPinput', type='number', value=minMSP,
# placeholder='Enter min. price',
# style={
# 'height': '20%',
# 'width': '42%',
# 'float': 'left',
# # 'position': 'fixed',
# 'textAlign': 'left'
# #'color': colors['light_text']
# }),
# dcc.Input(id='maxMSPinput', type='number', value=maxMSP,
# placeholder='Enter max. price',
# style={
# 'height': '20%',
# 'width': '42%',
# 'float': 'left',
# # 'position': 'fixed',
# 'textAlign': 'left'
# #'color': colors['light_text']
# }),
# html.Div(id='userMSPoutput',
# style={
# 'margin-top': 0,
# 'margin-right': 45,
# 'margin-bottom': 35,
# 'textAlign': 'left'
# }),
# html.Div(children='Min. Home Price <---> Max. Home Price',
# style={
# # 'float':'left',
# 'margin-top': 0,
# 'margin-right': 45,
# 'margin-bottom': 10,
# 'textAlign': 'left'
# }),
# # dcc.Graph(
# # id='interactive-choropeth-map',
# # style={
# # 'margin-top': 0,
# # 'margin-right': 45,
# # 'margin-bottom': 0}
# # ),
# #html.Div([
# dcc.Graph(id='map-graph',
# animate=True,
# style={'margin-top': '20'}
# #], className = "five columns"
# ),
# dcc.Slider(
# id='Yearly_outlook',
# min=0,
# max=15,
# value=1,
# marks={
# 0: {'label': 'Now', 'style': {'color': '#77b0b1'}},
# 1: {'label': '1 Year'},
# 2: {'label': ''},
# 3: {'label': ''},
# 4: {'label': ''},
# 5: {'label': '5 Years'},
# 6: {'label': ''},
# 7: {'label': ''},
# 8: {'label': ''},
# 9: {'label': ''},
# 10: {'label': '10 Years'},
# 11: {'label': ''},
# 12: {'label': ''},
# 13: {'label': ''},
# 14: {'label': ''},
# 15: {'label': '15 Years', 'style': {'color': '#f50'}}
# },
# ),
# html.Div(id='Yearly-outlook-output-container',style={'margin-top': 20}
# )
# ], className = "five columns"
# ),
# html.Div([
# dcc.Slider(
# id='Location_Value',
# min=0,
# max=4,
# value=1,
# marks={
# 0: {'label': 'Not Important', 'style': {'color': '#77b0b1'}},
# 1: {'label': 'Less Important'},
# 2: {'label': 'Important'},
# 3: {'label': 'Very Important'},
# 4: {'label': 'Extremely Important', 'style': {'color': '#f50'}}
# }
# ),
# html.Div(id='Location-Value-output-container',style={'margin-top': 20}
# ),
# ],className = "three columns"
# )
# ]),
# Map + table + Histogram
#------------------------
html.Div([
# html.Div([
# dcc.Graph(id='map-graph',
# animate=True,
# style={'margin-top': '20'})
# ], className = "six columns"
# ),
html.Div([
dt.DataTable(
id='datatable',
data=map_data.to_dict('records'),
columns=map_data.columns,
row_selectable='multi',
filtering=True,
sorting=True#,
# pagination_mode='fe',
# #selected_rows=[],
# pagination_settings={
# "current_page": 0,
# "page_size": 10
# },
)
],
style = layout_table,
className="six columns"
),
# html.Div([
# dcc.Graph(
# id='bar-graph'
# )
# ], className= 'twelve columns'
# ),
html.Div([
html.P('Developed by Duane M. Lee, Ph.D - ', style = {'display': 'inline'}
),
html.A('duane.m.lee@gmail.com', href = 'mailto:duane.m.lee@gmail.com'
)
], className = "twelve columns",
style = {'fontSize': 18, 'padding-top': 20,'margin-top':20}
)
], className = "row"
)
],className = 'ten columns offset-by-one'
)
,,
#callback section
#-------------------
# @app.callback(Output('userMSPoutput', 'children'),
# [Input('minMSPinput', 'value'),
# Input('maxMSPinput', 'value')])
# def selected_price_range(minMSP, maxMSP):
# minmax = [minMSP,maxMSP]
# return None #minMSP,maxMSP
# @app.callback(dash.dependencies.Output('Location-Value-output-container', 'children'),
# [dash.dependencies.Input('Location_Value', 'value')])
# def location_value(Location_Value):
# return None #Location_Value
# @app.callback(dash.dependencies.Output('Yearly-outlook-output-container', 'children'),
# [dash.dependencies.Input('Yearly_outlook', 'value')])
# def show_yearly_outlook(Yearly_outlook):
# return None #Yearly_outlook #u'{} Year Outlook'.format(Yearly_outlook)
# @app.callback(
# Output('map-graph', 'figure'),
# [Input('datatable', 'data'),
# Input('datatable', 'selected_rows')])
# def map_selection(rows, selected_rows):
# aux = pd.DataFrame(rows)
# temp_df = aux.ix[selected_rows, :]
# if len(selected_rows) == 0:
# return gen_map(aux)
# return gen_map(temp_df)
# @app.callback(
# Output('datatable', 'data'),
# [Input('minMSPinput', 'value'),
# Input('maxMSPinput', 'value')])
# def update_selected_row_indices(minMSP, maxMSP):
# map_aux = map_data.copy()
# now = datetime.today().strftime('%Y')
# table = map_aux[['zipcode','trend','trend_lower','trend_upper']].loc[map_aux['ds'] >= now]
# #print(table)
# featuresWithinBudget = table[np.logical_and(table['trend'] >= minMSP,table['trend'] < maxMSP)]
# #print(featuresWithinBudget)
# map_aux = featuresWithinBudget
# # # Type filter
# # map_aux = map_aux[map_aux['Type'].isin(type)]
# # # Boroughs filter
# # map_aux = map_aux[map_aux["Borough"].isin(borough)]
# data = map_aux.to_dict('records')
# print(data)
# return data
if __name__ == "__main__":
app.run_server(debug=True)