Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
| Download
Project: Dash
Views: 354import dash1from dash.dependencies import Input, Output2import dash_core_components as dcc3import dash_html_components as html45import flask6import pandas as pd7import time8import os9import numpy as np10import plotly11import plotly.graph_objs as go12import scipy.stats as scs1314server = flask.Flask('app')15server.secret_key = os.environ.get('secret_key', 'secret')16cocalc_project_id = os.environ['COCALC_PROJECT_ID']1718city = pd.DataFrame()19boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]20for boro in boros:21soql_url = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\22'$select=health,count(tree_id)' +\23'&$where=boroname='+boro +\24'AND NOT health=\'NaN\''25'&$group=health').replace(' ', '%20')26#print(soql_url)27soql_health = pd.read_json(soql_url)28soql_health['boroname'] = boro29city = city.append(soql_health)3031# With steward, Good Heatlth3233city2 = pd.DataFrame()34boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]35for boro in boros:36soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\37'$select=steward, count(health)' +\38'&$where=boroname='+boro +\39'AND NOT health=\'NaN\'' +\40'AND NOT steward=\'NaN\'' +\41'AND NOT steward=\'None\'' +\42'AND NOT health=\'Poor\'' +\43'&$group=steward').replace(' ', '%20')44soql_steward = pd.read_json(soql_url2)45soql_steward['boroname'] = boro46city2 = city2.append(soql_steward)47city2 = city2.replace('3or4', 1)48city2 = city2.replace('4orMore', 1)49city2 = city2.replace('1or2', 1)5051city2 = city2.groupby('boroname').sum()52city2 = city2.drop(columns = 'steward')53city25455# No Steward, Good Health5657city3 = pd.DataFrame()58boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]59for boro in boros:60soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\61'$select=steward, count(health)' +\62'&$where=boroname='+boro +\63'AND NOT health=\'NaN\'' +\64'AND steward=\'None\'' +\65'AND NOT steward=\'NaN\'' +\66'AND NOT health=\'Poor\'' +\67'&$group=steward').replace(' ', '%20')68soql_steward = pd.read_json(soql_url2)69soql_steward['boroname'] = boro70city3 = city3.append(soql_steward)7172city3 = city3.replace({'None': 0})73city3 = city3.groupby('boroname').sum()74city3 = city3.drop(columns = 'steward')75city37677# With steward, Bad Health7879city4 = pd.DataFrame()80boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]81for boro in boros:82soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\83'$select=steward, count(health)' +\84'&$where=boroname='+boro +\85'AND NOT health=\'NaN\'' +\86'AND NOT steward=\'NaN\'' +\87'AND NOT steward=\'None\'' +\88'AND health=\'Poor\'' +\89'&$group=steward').replace(' ', '%20')90soql_steward = pd.read_json(soql_url2)91soql_steward['boroname'] = boro92city4 = city4.append(soql_steward)93city4 = city4.replace('3or4', 1)94city4 = city4.replace('4orMore', 1)95city4 = city4.replace('1or2', 1)9697city4 = city4.groupby('boroname').sum()98city4 = city4.drop(columns = 'steward')99city4100101# No Steward, Bad Health102103city5 = pd.DataFrame()104boros = ["'Bronx'","'Staten Island'", "'Manhattan'", "'Queens'", "'Brooklyn'"]105for boro in boros:106soql_url2 = ('https://data.cityofnewyork.us/resource/nwxe-4ae8.json?' +\107'$select=steward, count(health)' +\108'&$where=boroname='+boro +\109'AND NOT health=\'NaN\'' +\110'AND steward=\'None\'' +\111'AND NOT steward=\'NaN\'' +\112'AND health=\'Poor\'' +\113'&$group=steward').replace(' ', '%20')114soql_steward = pd.read_json(soql_url2)115soql_steward['boroname'] = boro116city5 = city5.append(soql_steward)117118city5 = city5.replace({'None': 0})119city5 = city5.groupby('boroname').sum()120city5 = city5.drop(columns = 'steward')121city5122123# Make big df for analysis124125steward_good = city2126steward_bad = city4127128no_good = city3129no_bad = city5130big = pd.DataFrame()131132steward_good133134135big['steward good'] = steward_good['count_health']136big['steward bad'] = steward_bad['count_health']137big['no steward good'] = no_good['count_health']138big['no steward bad'] = no_bad['count_health']139140big = big.transpose()141142# Statistics Test143144obs = [big[boro][0], big[boro][1] ]145exp = [big[boro][2], big[boro][3] ]146147obs_total = np.sum(obs)148exp_total = np.sum(exp)149150obs = obs/obs_total * 100151exp = exp/exp_total * 100152153result = scs.chisquare(obs,exp)154result = bool(result[1]<.05)155if result == True:156conclusion = "We have sufficient evidence to suggest that stewardship effects tree health."157else:158conclusion = "We have insufficient evidence to suggest that stewardship effects tree health."159app = dash.Dash('app', server=server)160161app.scripts.config.serve_locally = False162dcc._js_dist[0]['external_url'] = 'https://cdn.plot.ly/plotly-basic-latest.min.js'163164app.layout = html.Div([165html.H1('Tree Health by Boro'),166dcc.Dropdown(167id='my-dropdown',168options=[169{'label': 'Queens', 'value': "'Queens'"},170{'label': 'Brooklyn', 'value': "'Brooklyn'"},171{'label': 'Manhattan', 'value': "'Manhattan'"},172{'label': 'Staten Island', 'value': "'Staten Island'"},173{'label': 'Bronx', 'value': "'Bronx'"}174],175value="'Queens'"176),177dcc.Graph(id='my-graph'),178html.H1('Tree Health vs Stewardship'),179dcc.Dropdown(180id='my-dropdown2',181options=[182{'label': 'Queens', 'value': "'Queens'"},183{'label': 'Brooklyn', 'value': "'Brooklyn'"},184{'label': 'Manhattan', 'value': "'Manhattan'"},185{'label': 'Staten Island', 'value': "'Staten Island'"},186{'label': 'Bronx', 'value': "'Bronx'"}187],188value="'Queens'"189),190dcc.Graph(id='total-graph'),191html.H2("Conclustion: "),192html.H3(conclusion)193], className="container")194195#interactive graph196@app.callback(Output('my-graph', 'figure'),197[Input('my-dropdown', 'value')])198def update_graph(selected_dropdown_value):199dff = city[city['boroname'] == selected_dropdown_value]200201return {202'data': [{203'x': dff.health,204'y': dff.count_tree_id,205'type': 'bar'206}],207'layout': {208'margin': {209'l': 30,210'r': 20,211'b': 30,212't': 20213}214}215}216217218# graph 2219@app.callback(Output('total-graph', 'figure'),220[Input('my-dropdown2', 'value')])221def update_graph2(selected_dropdown_value):222boro = selected_dropdown_value223trace1 = go.Bar(224x=["Good Health", "Bad Health"],225y= [big[boro][0], big[boro][1]],226name='With Steward'227)228trace2 = go.Bar(229x= ["Good Health", "Bad Health"],230y= [big[boro][2], big[boro][3]],231name='Without Steward'232)233return {234'data': [trace1, trace2],235'layout': {236'margin': {237'l': 30,238'r': 20,239'b': 30,240't': 20241}242}243}244245port = 9990246pfx = "/{}/server/{}/".format(cocalc_project_id, port)247app.config.requests_pathname_prefix = pfx248249if __name__ == '__main__':250print("browse to https://cocalc.com{}".format(pfx))251app.run_server(debug=True, port=port, host='0.0.0.0')252253