-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
201 lines (167 loc) · 8.46 KB
/
main.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
import pandas as pd
import json
import plotly.express as px
import plotly.graph_objects as go
from dash import Dash, html, dcc, Input, Output
# ==================================================================================================================== #
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
# ==================================================================================================================== #
# MAIN PAGE DATAFRAMES AND DATA PREPROCESSING
# ==================================================================================================================== #
# CRIMES DATAFRAME
df_crimes = pd.read_csv('./source-files/Occurrences_Last_90_Days.csv', low_memory=False)
df_crimes_category = df_crimes.groupby(['Occurrence_Category'], as_index=False).size()
def occurence_group_controller(crime_stats):
new_df = df_crimes[df_crimes.Occurrence_Category == crime_stats]
dfg = new_df.groupby('Occurrence_Group').count().reset_index()
return dfg["Occurrence_Group"].values.tolist()
def occurence_group_type_controller(occurence_group):
new_df = df_crimes[df_crimes.Occurrence_Group == occurence_group]
dfg = new_df.groupby('Occurrence_Type_Group').count().reset_index()
return dfg["Occurrence_Type_Group"].values.tolist()
def mapped_crime_list():
ret = {"Disorder": occurence_group_controller("Disorder"),
"Non-Violent": occurence_group_controller("Non-Violent"),
"Violent": occurence_group_controller("Violent"),
"Traffic": occurence_group_controller("Traffic"),
"Weapons": occurence_group_controller("Weapons"),
"Drugs": occurence_group_controller("Drugs"),
"Other": occurence_group_controller("Other")}
return ret
crimes_list = df_crimes['Occurrence_Category'].unique()
crimes_color_map = dict(zip(crimes_list, px.colors.qualitative.G10))
# ==================================================================================================================== #
# NEIGHBOURHOOD GEOJSON FOR CHOROPLETH MAP
# set 'neighbourhood' as a global value to avoid loading same data everytime
with open('./source-files/City of Edmonton - Neighbourhoods.geojson', 'r') as f:
neighbourhood = json.load(f)
# ==================================================================================================================== #
# COMPARISON PAGE DATAFRAMES AND SETUP
# ==================================================================================================================== #
# colours
bg_assessedValue = "#1C6387"
fg_assessedValue = "white"
crime_file = "./source-files/Occurrences_Last_90_Days.csv"
assessment_file = "./source-files/Property_Assessment_Data__Current_Calendar_Year_.csv"
languages_file = "./source-files/2016_Census_-_Dwelling_Unit_by_Language__Neighbourhood_Ward_.csv"
def create_graph_languages(value):
df = pd.read_csv(languages_file)
dff = df.loc[df['Neighbourhood'] == value]
if (len(dff.index) == 0):
return html.Div(
html.H3(f"No records found for Neighbourhood: {value.title()}",
style={'margin': '5px',
'padding': '50px',
'border-radius': '10px',
'text-align': 'center',
'color': '#636EFA',
'background-color': '#E5ECF6'}
)
)
else:
headers = list(dff.columns.values)[4:14]
values = [
dff.iloc[0, 4], dff.iloc[0, 5], dff.iloc[0, 6], dff.iloc[0, 7],
dff.iloc[0, 8], dff.iloc[0, 9], dff.iloc[0, 10], dff.iloc[0, 11],
dff.iloc[0, 12], dff.iloc[0, 13],
]
column_headers = headers[::-1]
value_list = values[::-1]
state = all([val == 0 for val in values])
#print(f"{len(column_headers)} & {len(value_list)}")
if (state == False):
fig = go.Figure(
data=[go.Bar(
x=value_list,
y=column_headers,
orientation="h",
)],
layout=go.Layout(
title=f"Languages per household in {value.title()}",
margin=dict(l=5, r=5, t=35, b=5)),
)
return dcc.Graph(figure=fig, config=dict({'displayModeBar': False}))
else:
return html.Div(
html.H3(f"No languages listed in Neighbourhood: {value.title()}",
style={'margin': '5px',
'padding': '50px',
'border-radius': '10px',
'text-align': 'center',
'color': '#636EFA',
'background-color': '#E5ECF6'}
)
)
def create_average_assessment(value):
residential = "RESIDENTIAL"
df = pd.read_csv(assessment_file)
dff = df.query("`Neighbourhood`==@value & `Assessment Class % 1`==100 & `Assessment Class 1`==@residential")["Assessed Value"]
if (len(dff.index)==0): return "$0.00"
else:
average = dff.mean()
currency_string = "${:,.2f}".format(average)
return currency_string
# ==================================================================================================================== #
# DASH APP INTERFACE SETUP
# ==================================================================================================================== #
# APP LAYOUT
app = Dash(__name__)
app.layout = html.Div([
html.Div(children=[
dcc.RadioItems(options=['Main Page', 'Comparison'],
value='Main Page',
persistence=True,
style={'text-align-last': 'end', 'color': '#1C6387', 'position': 'absolute', 'right': 10},
id='change_page')
]),
# MAIN PAGE
html.Div(children=[
html.Div(children=[
html.Div(children=[
html.Label('Moving to Edmonton Made Easy',
style={
'color': 'white',
'padding': '30, 30, 30, 0',
'width': 60,
'font': 'Poppins',
'font-size': 25,
'text-align': 'center',
}),
html.Br(),
html.Br(),
html.Br(),
html.Label('Neighbourhoods Within Assessment Range', style={'color': 'white', 'margin-bottom': 8}),
dcc.Dropdown(options=neighbourhood_list,
placeholder='Enter Neighbourhood', id='Neighbourhood_input',
style={'marginRight': '10px', 'width': 350},
),
html.Br(),
html.Br(),
html.Label('Crime Occurrences', style={'color': 'white', 'margin-bottom': 8}),
dcc.Dropdown(crimes_list, id='crime_dropdown', multi=True,
style={'marginRight': '10px', 'width': 350}),
], id='map_filters', style={'padding': 30, 'background': '#1C6387'}),
html.Div(children=[
html.Label('Neighbourhood Assessment Average', style={'color': '#1C6387', 'padding': 25}),
html.Div(children=[
dcc.RangeSlider(
min=100000,
max=1000000,
step=25000,
value=[150000, 450000],
id='Neighbourhood_Average'),
], style={'color': '#1C6387'}),
# Contains Map graph and progress indicator
html.Div([
dcc.Loading(
dcc.Graph(id='the_graph',
config={'doubleClick': 'reset', 'showTips': True, 'displayModeBar': False,
'watermark': False}, style={'height': 550})),
], id='map_container', style={'padding': 0, 'flex': 1}),
], id='map_slider_container', style={'width': '55vw', 'flex': 1}),
], id='top-container', style={'display': 'flex', 'flex-direction': 'row', 'height': '60vh'}),
# ============================================================================================================ #
])
], id='main-container', style={'display': 'block', 'padding': 4})