Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Dash
Views: 347
1
import dash
2
from dash.dependencies import Input, Output
3
import dash_core_components as dcc
4
import dash_html_components as html
5
6
import flask
7
import pandas as pd
8
import time
9
import os
10
import numpy as np
11
import plotly
12
import plotly.graph_objs as go
13
import scipy.stats as scs
14
15
server = flask.Flask('app')
16
server.secret_key = os.environ.get('secret_key', 'secret')
17
cocalc_project_id = os.environ['COCALC_PROJECT_ID']
18
19
city = pd.DataFrame()
20
boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]
21
for boro in boros:
22
soql_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
23
'$select=health,count(tree_id)' +\
24
'&$where=boroname='+boro +\
25
'AND NOT health=\'NaN\''
26
'&$group=health').replace(' ', '%20')
27
#print(soql_url)
28
soql_health = pd.read_json(soql_url)
29
soql_health['boroname'] = boro
30
city = city.append(soql_health)
31
32
# With steward, Good Heatlth
33
34
city2 = pd.DataFrame()
35
boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]
36
for boro in boros:
37
soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
38
'$select=steward, count(health)' +\
39
'&$where=boroname='+boro +\
40
'AND NOT health=\'NaN\'' +\
41
'AND NOT steward=\'NaN\'' +\
42
'AND NOT steward=\'None\'' +\
43
'AND NOT health=\'Poor\'' +\
44
'&$group=steward').replace(' ', '%20')
45
soql_steward = pd.read_json(soql_url2)
46
soql_steward['boroname'] = boro
47
city2 = city2.append(soql_steward)
48
city2 = city2.replace('3or4', 1)
49
city2 = city2.replace('4orMore', 1)
50
city2 = city2.replace('1or2', 1)
51
52
city2 = city2.groupby('boroname').sum()
53
city2 = city2.drop(columns = 'steward')
54
city2
55
56
# No Steward, Good Health
57
58
city3 = pd.DataFrame()
59
boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]
60
for boro in boros:
61
soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
62
'$select=steward, count(health)' +\
63
'&$where=boroname='+boro +\
64
'AND NOT health=\'NaN\'' +\
65
'AND steward=\'None\'' +\
66
'AND NOT steward=\'NaN\'' +\
67
'AND NOT health=\'Poor\'' +\
68
'&$group=steward').replace(' ', '%20')
69
soql_steward = pd.read_json(soql_url2)
70
soql_steward['boroname'] = boro
71
city3 = city3.append(soql_steward)
72
73
city3 = city3.replace({'None': 0})
74
city3 = city3.groupby('boroname').sum()
75
city3 = city3.drop(columns = 'steward')
76
city3
77
78
# With steward, Bad Health
79
80
city4 = pd.DataFrame()
81
boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]
82
for boro in boros:
83
soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
84
'$select=steward, count(health)' +\
85
'&$where=boroname='+boro +\
86
'AND NOT health=\'NaN\'' +\
87
'AND NOT steward=\'NaN\'' +\
88
'AND NOT steward=\'None\'' +\
89
'AND health=\'Poor\'' +\
90
'&$group=steward').replace(' ', '%20')
91
soql_steward = pd.read_json(soql_url2)
92
soql_steward['boroname'] = boro
93
city4 = city4.append(soql_steward)
94
city4 = city4.replace('3or4', 1)
95
city4 = city4.replace('4orMore', 1)
96
city4 = city4.replace('1or2', 1)
97
98
city4 = city4.groupby('boroname').sum()
99
city4 = city4.drop(columns = 'steward')
100
city4
101
102
# No Steward, Bad Health
103
104
city5 = pd.DataFrame()
105
boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]
106
for boro in boros:
107
soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\
108
'$select=steward, count(health)' +\
109
'&$where=boroname='+boro +\
110
'AND NOT health=\'NaN\'' +\
111
'AND steward=\'None\'' +\
112
'AND NOT steward=\'NaN\'' +\
113
'AND health=\'Poor\'' +\
114
'&$group=steward').replace(' ', '%20')
115
soql_steward = pd.read_json(soql_url2)
116
soql_steward['boroname'] = boro
117
city5 = city5.append(soql_steward)
118
119
city5 = city5.replace({'None': 0})
120
city5 = city5.groupby('boroname').sum()
121
city5 = city5.drop(columns = 'steward')
122
city5
123
124
# Make big df for analysis
125
126
steward_good = city2
127
steward_bad = city4
128
129
no_good = city3
130
no_bad = city5
131
big = pd.DataFrame()
132
133
steward_good
134
135
136
big['steward good'] = steward_good['count_health']
137
big['steward bad'] = steward_bad['count_health']
138
big['no steward good'] = no_good['count_health']
139
big['no steward bad'] = no_bad['count_health']
140
141
big = big.transpose()
142
143
# Statistics Test
144
145
obs = [big[boro][0], big[boro][1] ]
146
exp = [big[boro][2], big[boro][3] ]
147
148
obs_total = np.sum(obs)
149
exp_total = np.sum(exp)
150
151
obs = obs/obs_total * 100
152
exp = exp/exp_total * 100
153
154
result = scs.chisquare(obs,exp)
155
result = bool(result[1]<.05)
156
if result == True:
157
conclusion = "We have sufficient evidence to suggest that stewardship effects tree health."
158
else:
159
conclusion = "We have insufficient evidence to suggest that stewardship effects tree health."
160
app = dash.Dash('app', server=server)
161
162
app.scripts.config.serve_locally = False
163
dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-basic-latest.min.js'
164
165
app.layout = html.Div([
166
html.H1('Tree Health by Boro'),
167
dcc.Dropdown(
168
id='my-dropdown',
169
options=[
170
{'label': 'Queens', 'value': "'Queens'"},
171
{'label': 'Brooklyn', 'value': "'Brooklyn'"},
172
{'label': 'Manhattan', 'value': "'Manhattan'"},
173
{'label': 'Staten Island', 'value': "'Staten Island'"},
174
{'label': 'Bronx', 'value': "'Bronx'"}
175
],
176
value="'Queens'"
177
),
178
dcc.Graph(id='my-graph'),
179
html.H1('Tree Health vs Stewardship'),
180
dcc.Dropdown(
181
id='my-dropdown2',
182
options=[
183
{'label': 'Queens', 'value': "'Queens'"},
184
{'label': 'Brooklyn', 'value': "'Brooklyn'"},
185
{'label': 'Manhattan', 'value': "'Manhattan'"},
186
{'label': 'Staten Island', 'value': "'Staten Island'"},
187
{'label': 'Bronx', 'value': "'Bronx'"}
188
],
189
value="'Queens'"
190
),
191
dcc.Graph(id='total-graph'),
192
html.H2("Conclustion: "),
193
html.H3(conclusion)
194
], className="container")
195
196
#interactive graph
197
@app.callback(Output('my-graph', 'figure'),
198
[Input('my-dropdown', 'value')])
199
def update_graph(selected_dropdown_value):
200
dff = city[city['boroname'] == selected_dropdown_value]
201
202
return {
203
'data': [{
204
'x': dff.health,
205
'y': dff.count_tree_id,
206
'type': 'bar'
207
}],
208
'layout': {
209
'margin': {
210
'l': 30,
211
'r': 20,
212
'b': 30,
213
't': 20
214
}
215
}
216
}
217
218
219
# graph 2
220
@app.callback(Output('total-graph', 'figure'),
221
[Input('my-dropdown2', 'value')])
222
def update_graph2(selected_dropdown_value):
223
boro = selected_dropdown_value
224
trace1 = go.Bar(
225
x=["Good Health", "Bad Health"],
226
y= [big[boro][0], big[boro][1]],
227
name='With Steward'
228
)
229
trace2 = go.Bar(
230
x= ["Good Health", "Bad Health"],
231
y= [big[boro][2], big[boro][3]],
232
name='Without Steward'
233
)
234
return {
235
'data': [trace1, trace2],
236
'layout': {
237
'margin': {
238
'l': 30,
239
'r': 20,
240
'b': 30,
241
't': 20
242
}
243
}
244
}
245
246
port = 9990
247
pfx = "/{}/server/{}/".format(cocalc_project_id, port)
248
app.config.requests_pathname_prefix = pfx
249
250
if __name__ == '__main__':
251
print("browse to https://cocalc.com{}".format(pfx))
252
app.run_server(debug=True, port=port, host='0.0.0.0')
253