-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
383 lines (311 loc) · 12.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
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_daq as daq
import pandas as pd
import plotly.express as px
from dash.dependencies import Output, Input
import urllib.request as request
import json
from datetime import date
def get_json():
link = "https://raw.githubusercontent.com/mbalcerzak/warsaw_flats_api/raspberry-updates/json_dir/flats.json"
with request.urlopen(link) as url:
data = json.loads(url.read().decode())
return data
def get_list(label: str) -> dict:
data = get_json()
sorted_list = sorted(data[label].keys())
options = []
for key in sorted_list:
options.append({'label': key, 'value': key})
return options
def today_str():
return date.today().strftime("%Y-%m-%d")
today = today_str()
def get_dates():
data = get_json()
dff = data["dates"]
min_date = dff['min_date']
max_date = dff['max_date']
return min_date, max_date
date_first, date_last = get_dates()
districts = get_list("flats_per_location")
flat_sizes = get_list("flats_per_area_cat")
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
# --- initialize the app ---
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
# --- layout the dashboard ---
app.layout = html.Div([
html.Div([
html.Div([
html.H1('Price tracker for apartments in Warsaw, Poland',
style={'textAlign': 'center',
'color': '#FFFFFF',
'fontSize': '36px',
'padding-top': '0px'},
),
html.P('By MAB', style={'textAlign': 'center',
'color': '#FFFFFF',
'fontSize': '24px'},
),
html.P('''An interactive dashboard displaying apartment prices in the districts of Warsaw in real-time ''',
style={'textAlign': 'center',
'color': '#FFFFFF',
'fontSize': '16px'},
),
],
style={'backgroundColor': '#1f3b4d',
'height': '200px',
'display': 'flex',
'flexDirection': 'column',
'justifyContent': 'center'},
),
html.Div([
html.H1(f'From {date_first} to {date_last}',
style={'textAlign': 'center',
'color': '#1f3b4d',
'fontSize': '30px',
'padding-top': '15px'},
),
# two pie charts in a row
html.Div([
html.Div([
html.H4('Number of flats in each district', style={'textAlign': 'center'}),
dcc.Graph(id='pie_flats_per_location',
style={'padding': '25px'}),
], className="six columns"),
html.Div([
html.H4('Size of flats (m2)', style={'textAlign': 'center'}),
dcc.Graph(id='pie_flats_per_area_cat',
style={'padding': '25px'}),
], className="six columns"),
], className="row"),
html.Div(
dcc.Graph(id='scraped-per-day',
style={'padding': '25px'}),
),
html.Div(
dcc.Graph(id='posted-per-day',
style={'padding': '25px'}),
),
html.Div(
dcc.Graph(id='price-changes-per-day',
style={'padding': '25px'}),
),
html.Div(
dcc.Graph(id='all-districts-prices',
style={'padding': '25px'}),
),
]),
html.Div([
html.H1('Select district and flat area to see average prices',
style={'textAlign': 'center',
'color': '#1f3b4d',
'fontSize': '30px',
'padding-top': '15px'},
),
html.Div([
html.Div([
html.Label('Select district'),
dcc.Dropdown(
id='district-dropdown',
options=districts,
value='Mokotów',
multi=False,
clearable=True,
searchable=True,
placeholder='Choose a district...',
),
], className="five columns",
style={'width': '25%',
'display': 'inline-block',
'padding-left': '150px',
'padding-top': '20px'}
),
html.Div([
html.Label('Select a flat size (square metres)'),
dcc.Dropdown(
id='area-dropdown',
options=flat_sizes,
value='40_50',
multi=False,
clearable=True,
searchable=True,
placeholder='Choose a flat size...',
),
], className="five columns",
style={'width': '25%',
'display': 'inline-block',
'padding-left': '50px',
'padding-top': '20px'}
),
html.Div([
html.Label('Fix price axis (easier to compare)'),
daq.ToggleSwitch(
id='price-axis-toggle',
value=False
),
html.Div(id='toggle-switch-output')
], className="one column",
style={'width': '25%',
'display': 'inline-block',
'padding-left': '30px',
'padding-top': '30px'}),
], className="row"),
html.Div(
dcc.Graph(id='district-area-price',
style={'padding': '25px'}),
),
]),
# html.Div([
# html.Label('Useful links:',
# style={'padding': '10px'}
# ),
# html.Label(' - JSON data',
# style={'padding-left': '25px'}),
# html.A('https://raw.githubusercontent.com/mbalcerzak/warsaw_flats_api/main/json_dir/flats.json',
# style={'padding-left': '25px'}),
# html.Label('- code for this thing',
# style={'padding-left': '25px'}),
# html.A('https://github.com/mbalcerzak/warsaw_flats_dashboard',
# style={'padding-left': '25px'}),
# ],
# ),
],
),
],
)
@app.callback(
Output('district-area-price', 'figure'),
[Input('district-dropdown', "value"),
Input('area-dropdown', "value"),
Input('price-axis-toggle', "value")])
def update_figure(location, area, toggle):
data = get_json()
dff = data["price_m_loc_area_cat"]
dff = pd.DataFrame.from_dict(dff)
prices = [x for x in dff['avg_price_per_m'].to_list() if x < 50000]
max_price = max(prices)
min_price = min(prices)
dff = dff[dff['location'] == location]
dff = dff[dff['area_category'] == area]
num_flats = sum(dff['num_flats'])
dff = dff.sort_values(by=['month_num'])
fig = px.line(dff, x='month', y='avg_price_per_m')
if toggle:
fig.update_yaxes(range=[min_price, max_price])
fig.update_layout(template='xgridoff',
yaxis={'title': 'Price per m2 (PLN)'},
xaxis={'title': 'Month'},
title={'text': f'Prices in {location} for flats of size {area} ({num_flats} flats)',
'font': {'size': 24}, 'x': 0.5, 'xanchor': 'center'},
)
fig.update_traces(mode="markers+lines")
return fig
@app.callback(
Output('all-districts-prices', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(area):
data = get_json()
dff = data["price_m_location"]
dff = pd.DataFrame(dff)
dff = dff.sort_values(by=['month_num'])
fig = px.line(dff, x='month', y='avg_price_per_m', color='location')
fig.update_layout(template='xgridoff',
yaxis={'title': 'Average price per m2 (PLN)'},
xaxis={'title': 'Month'},
title={'text': f'Average prices per m2 for each district (flats of all sizes)',
'font': {'size': 24}, 'x': 0.5, 'xanchor': 'center'}
)
return fig
# ------------------ SCRAPED BY RASP.PI. PER DAY ---------------------------------------------
@app.callback(
Output('scraped-per-day', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(area):
data = get_json()
dff = data["scraped_per_day"]
dff = pd.DataFrame(dff.items(), columns=['Date', 'Value'])
dff['Type'] = 'Value'
dff_ma = data["scraped_per_day_m_avg"]
dff_ma = pd.DataFrame(dff_ma.items(), columns=['Date', 'Value'])
dff_ma['Type'] = 'Moving Average (7 days)'
df = dff.append(dff_ma, ignore_index=True)
df = df.sort_values(by=['Date'])
df_full = df.loc[df['Date'] != today]
fig = px.line(df_full, x='Date', y='Value', color='Type')
fig.update_layout(template='xgridoff',
yaxis={'title': 'Number of ads scraped'},
xaxis={'title': 'Date'},
title={'text': f'Ads scraped daily',
'font': {'size': 24}, 'x': 0.5, 'xanchor': 'center'}
)
return fig
# ------------------ POSTED PER DAY ---------------------------------------------
@app.callback(
Output('posted-per-day', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(area):
data = get_json()
dff = data["posted_per_day"]
dff = pd.DataFrame(dff.items(), columns=['Date', 'Value'])
dff['Type'] = 'Value'
dff_ma = data["posted_per_day_m_avg"]
dff_ma = pd.DataFrame(dff_ma.items(), columns=['Date', 'Value'])
dff_ma['Type'] = 'Moving Average (7 days)'
df = dff.append(dff_ma, ignore_index=True)
df = df.sort_values(by=['Date'])
df_full = df.loc[df['Date'] != today]
fig = px.line(df_full, x='Date', y='Value', color='Type')
fig.update_layout(template='xgridoff',
yaxis={'title': 'Number of ads posted'},
xaxis={'title': 'Date'},
title={'text': f'Ads posted daily',
'font': {'size': 24}, 'x': 0.5, 'xanchor': 'center'}
)
return fig
# ------------------ PRICE CHANGES (COUNT) PER DAY ---------------------------------------------
@app.callback(
Output('price-changes-per-day', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(area):
data = get_json()
dff = data["changes_per_day"]
dff = pd.DataFrame(dff.items(), columns=['Date', 'Value'])
dff['Type'] = 'Value'
dff_ma = data["changed_per_day_m_avg"]
dff_ma = pd.DataFrame(dff_ma.items(), columns=['Date', 'Value'])
dff_ma['Type'] = 'Moving Average (7 days)'
df = dff.append(dff_ma, ignore_index=True)
df = df.sort_values(by=['Date'])
df_full = df.loc[df['Date'] != today]
fig = px.line(df_full, x='Date', y='Value', color='Type')
fig.update_layout(template='xgridoff',
yaxis={'title': 'Number of price changes per day'},
xaxis={'title': 'Date'},
title={'text': 'Daily price changes',
'font': {'size': 24}, 'x': 0.5, 'xanchor': 'center'}
)
return fig
@app.callback(
Output('pie_flats_per_location', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(selected_city):
data = get_json()
dff = data["flats_per_location"]
dff = pd.DataFrame(dff.items(), columns=['Location', 'Value'])
fig = px.pie(dff, values='Value', names='Location')
return fig
@app.callback(
Output('pie_flats_per_area_cat', 'figure'),
Input('area-dropdown', 'value'))
def update_figure(selected_city):
data = get_json()
dff = data["flats_per_area_cat"]
dff = pd.DataFrame(dff.items(), columns=['Area', 'Value'])
fig = px.pie(dff, values='Value', names='Area')
return fig
if __name__ == '__main__':
app.run_server(debug=True)