-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
289 lines (251 loc) · 10.8 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
import os
import dash
import dash_core_components as dcc
import dash_html_components as html
import dash_auth
import psycopg2
import pandas as pd
import plotly.graph_objs as go
import numpy as np
#connect to the database and read in the necessary tables
DATABASE_URL = os.environ['DATABASE_URL']
USERNAME = os.environ['USERNAME']
PASSWORD = os.environ['PASSWORD']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
inflow = pd.read_sql_query("select * from inflow", con=conn)
los = pd.read_sql_query("select * from los", con=conn)
phexits = pd.read_sql_query("select * from phexits", con=conn)
def filter_data(dataframe, selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household):
'''function that filters data based on inputs'''
filtered_df = dataframe
if selected_race == 'all':
filtered_df = filtered_df
else:
filtered_df = filtered_df[filtered_df.race == selected_race]
if selected_ethnicity == 'all':
filtered_df = filtered_df
else:
filtered_df = filtered_df[filtered_df.ethnicity == selected_ethnicity]
if selected_gender == 'all':
filtered_df = filtered_df
else:
filtered_df = filtered_df[filtered_df.gender == selected_gender]
if selected_veteran == 'all':
filtered_df = filtered_df
else:
filtered_df = filtered_df[filtered_df.veteranstatus == selected_veteran]
if selected_household == 'all':
filtered_df = filtered_df
else:
filtered_df = filtered_df[filtered_df.householdtype == selected_household]
return filtered_df
VALID_USERNAME_PASSWORD_PAIRS = [
[USERNAME, PASSWORD]
]
app = dash.Dash('auth')
auth = dash_auth.BasicAuth(
app,
VALID_USERNAME_PASSWORD_PAIRS
)
#app = dash.Dash(__name__)
server = app.server
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/bWLwgP.css"})
app.layout = html.Div([
html.Div(className="app-header", children=[html.Div('System Exploration App - Boston CoC', className="app-header--title")], style={"margin-left": "10px", "margin-right": "10px"}),
html.P("""The goal of this web app is to allow and empower the Supportive Housing Divison to explore and ask questions of the HMIS data. The app provides answers to broad system performance questions like "is the average length of stay decreasing?" but also allows users to dig deeper and uncover insights from the data, like "White veterans have shorter lengths of stay than veterans of color." The baseline measures displayed in the app are an adaption/expansion of HUD's System Performance Measures. The Supportive Housing Division collaborated to workshop and tailor these measures to be more relevant to the goals of our CoC.""", style={"margin-left": "10px", "margin-right": "10px"}),
html.Div([
html.Div([
html.Div([
html.H5('Race'),
dcc.Dropdown(
id='race-dropdown',
options=[{'label': 'All', 'value': 'all'},
{'label': 'White', 'value': 'White'},
{'label': 'Black / African American', 'value': 'BlackAfAmerican'},
{'label': 'American Indian / Alaska Native', 'value': 'AmIndAKNative'},
{'label': 'Asian', 'value': 'Asian'},
{'label': 'Native Hawaiian / Other Pacific Islander', 'value': 'NativeHIOtherPacific'}],
value='all'),
html.Br(),
html.H5('Veteran Status'),
dcc.Dropdown(
id='veteran-dropdown',
options=[{'label': 'All', 'value': 'all'},
{'label': 'Veterans', 'value': '1.0'},
{'label': 'Non-Veterans', 'value': '0.0'}],
value='all'
)], className="four columns"),
html.Div([
html.H5('Ethnicity'),
dcc.Dropdown(
id='ethnicity-dropdown',
options=[{'label': 'All', 'value': 'all'},
{'label': 'Hispanic / Latinx', 'value': '1.0'},
{'label': 'Not Hispanic / Latinx', 'value': '0.0'}],
value='all'),
html.Br(),
html.H5('Household Status'),
dcc.Dropdown(
id='household-dropdown',
options=[{'label': 'All', 'value': 'all'},
{'label': 'Individual Adults (25+)', 'value': 'individual adult (25+)'},
{'label': 'Unaccompanied Youth (18-24)', 'value': 'unaccompanied youth'},
{'label': 'Families', 'value': 'family member'}],
value='all')], className="four columns"),
html.Div([
html.H5('Gender'),
dcc.Dropdown(
id='gender-dropdown',
options=[{'label': 'All', 'value': 'all'},
{'label': 'Male', 'value': '1.0'},
{'label': 'Female', 'value': '0.0'},
{'label': 'Trans Female', 'value': '2.0'},
{'label': 'Trans Male', 'value': '3.0'},
{'label': 'Gender Non-Conforming', 'value': '4.0'}],
value='all')], className="four columns"
)], className = "row", style={'margin-left':'50px', 'margin-right':'100px'})
]),
html.Br(),
html.Br(),
html.H4("Inflow / First time Homeless", style={'font-weight': 'bold', 'margin-left':'50px'}),
html.Hr(style={'font-weight': 'bold', 'margin-left':'50px', 'color':'#d2d2d2'}),
html.Div([
html.Div([
html.H6("What am I looking at?"),
html.P("In a given year, how many clients experienced their first-ever overnight stay in a Boston CoC shelter or on the street?")
], className="three columns"),
html.Div([
html.H6("Context:"),
html.P("This metric gives a sense of how many new clients per year the CoC is serving, which can provide context for the overall census")
], className="three columns"),
], className="row", style={'margin-left':'50px', 'margin-right':'100px'}),
dcc.Graph(id='inflow-graph', style={'margin-left':'80px', 'margin-right':'80px'}),
html.H4("Length of Stay", style={'font-weight': 'bold', 'margin-left':'50px'}),
html.Hr(style={'font-weight': 'bold', 'margin-left':'50px', 'color':'#d2d2d2'}),
html.Div([
html.Div([
html.H6("What am I looking at?"),
html.P("In a given year, how long on average have clients who appeared in a Boston CoC shelter / on the street that year spent cumulatively in a Boston CoC shelter or on the street?")
], className="three columns"),
html.Div([
html.H6("Context:"),
html.P("This metric gives a sense of how long individuals in the current CoC population have been experiencing homelessness in the CoC. This metric can shed light on whether the CoC's efforts to house its longest-term stayers have been successful.")
], className="three columns"),
], className="row", style={'margin-left':'50px', 'margin-right':'100px'}),
dcc.Graph(id='los-graph', style={'margin-left':'80px', 'margin-right':'80px'}),
html.H4("Exits to Permanent Housing", style={'font-weight': 'bold', 'margin-left':'50px'}),
html.Hr(style={'font-weight': 'bold', 'margin-left':'50px', 'color':'#d2d2d2'}),
html.Div([
html.Div([
html.H6("What am I looking at?"),
html.P("In a given year, how many clients exited to a permanent housing destination?")
], className="three columns"),
html.Div([
html.H6("Context:"),
html.P("This metric gives a sense of how many clients per year the CoC is helping exit to a permanent housing destination, which can provide insight on whether the CoC is achieving its goal of increasing exits to permanent housing")
], className="three columns"),
], className="row", style={'margin-left':'50px', 'margin-right':'100px'}),
dcc.Graph(id='ph-graph', style={'margin-left':'80px', 'margin-right':'80px'})
])
'''callback for inflow'''
@app.callback(dash.dependencies.Output('inflow-graph', 'figure'),
[dash.dependencies.Input('race-dropdown', 'value'),
dash.dependencies.Input('ethnicity-dropdown', 'value'),
dash.dependencies.Input('gender-dropdown', 'value'),
dash.dependencies.Input('veteran-dropdown', 'value'),
dash.dependencies.Input('household-dropdown', 'value')
])
def update_figure(selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household):
#call function to filter data
filtered_df = filter_data(inflow, selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household)
#agreggate table into reporting format
yearly_inflow = pd.DataFrame(filtered_df.groupby('year')['count'].sum())
#dynamically render figure
return {
'data': [go.Scatter(x = yearly_inflow.index,
y= yearly_inflow['count'],
mode="lines+markers",
marker=dict(
size=15,
color='rgba(78,145,221, .9)',
line=dict(
color='rgba(0, 0, 0, .9)',
width = 2,
)
)
)
],
'layout': go.Layout(
xaxis={'title': 'Year'},
yaxis=dict(title='Number of clients',
range= [0,max(yearly_inflow['count']) + 0.2*(max(yearly_inflow['count']))]),
hovermode='closest',
margin=dict(t=0)
)
}
'''callback for Length of Stay'''
@app.callback(dash.dependencies.Output('los-graph', 'figure'),
[dash.dependencies.Input('race-dropdown', 'value'),
dash.dependencies.Input('ethnicity-dropdown', 'value'),
dash.dependencies.Input('gender-dropdown', 'value'),
dash.dependencies.Input('veteran-dropdown', 'value'),
dash.dependencies.Input('household-dropdown', 'value')
])
def update_figure(selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household):
#call function to filter data
filtered_df = filter_data(los, selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household)
grouped_df = filtered_df.groupby('year')
def compute_los(grp):
grp['weighted_los'] = np.average(grp['avglos'], weights = grp['numclients'])
return grp
grouped_df = grouped_df.apply(compute_los)
yearly_los = grouped_df
print(yearly_los)
#dynamically render figure
return {
'data': [go.Bar(x = yearly_los['year'],
y= yearly_los['weighted_los'],
marker=dict(
color='rgba(78,145,221, .9)',
line=dict(
width=1.5),
)
)],
'layout': go.Layout(
xaxis={'title': 'Year'},
yaxis={'title': 'Average Length of Stay'},
hovermode='closest',
margin=dict(t=0)
)
}
'''callback for PH exits'''
@app.callback(dash.dependencies.Output('ph-graph', 'figure'),
[dash.dependencies.Input('race-dropdown', 'value'),
dash.dependencies.Input('ethnicity-dropdown', 'value'),
dash.dependencies.Input('gender-dropdown', 'value'),
dash.dependencies.Input('veteran-dropdown', 'value'),
dash.dependencies.Input('household-dropdown', 'value')
])
def update_figure(selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household):
#call function to filter data
filtered_df = filter_data(phexits, selected_race, selected_ethnicity, selected_gender, selected_veteran, selected_household)
#agreggate table into reporting format
yearly_ph = pd.DataFrame(filtered_df.groupby('year')['count'].sum())
#dynamically render figure
return {
'data': [go.Bar(x = yearly_ph.index,
y= yearly_ph['count'],
marker=dict(
color='rgba(78,145,221, .9)',
line=dict(
width=1.5),
))],
'layout': go.Layout(
xaxis={'title': 'Year'},
yaxis={'title': 'Number of clients'},
hovermode='closest',
margin=dict(t=0)
)
}
if __name__ == '__main__':
app.run_server(debug=True)