-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
173 lines (136 loc) · 5.03 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
###################
# Dependencies
##################
import sqlalchemy
import numpy as np
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify
import datetime as dt
import pandas as pd
##################
# Database Setup
##################
engine = create_engine("sqlite:///Resources/hawaii.sqlite")
#reflect an existing database into a new model
Base = automap_base()
#reflect the tables
Base.prepare(engine, reflect = True)
#Save the reference to the tables
Measurement = Base.classes.measurement
Station = Base.classes.station
##################
# Flask Setup
##################
app = Flask(__name__)
##################
# Flask Routes
##################
@app.route("/")
def welcome():
#List all available routes
return(
f'Available Routes <br/>'
f'/api/v1.0/precipitation <br/>'
f'/api/v1.0/stations <br/>'
f'/api/v1.0/tobs <br/>'
f'/api/v1.0/start <br/>'
f'/api/v1.0/start/end'
)
@app.route("/api/v1.0/precipitation")
def precipitation(): #Displays dictionary of all dates and prcps
#Create session link
session = Session(engine)
#Convert the query results to a dictionary using date as the key and prcp as the value.
results = session.query(Measurement.date, Measurement.prcp).all()
session.close()
all_precipitation = []
for date, prcp in results:
precipitation_dict = {}
precipitation_dict['date'] = date
precipitation_dict['prcp'] = prcp
all_precipitation.append(precipitation_dict)
return jsonify(all_precipitation)
@app.route("/api/v1.0/stations")
def stations(): #Lists the stations in the dataset
#Create session link
session = Session(engine)
#Return a JSON list of stations from the dataset.
results = session.query(Station.station, Station.name).all()
session.close()
all_stations = []
for station in results:
all_stations.append(station)
return jsonify(all_stations)
@app.route("/api/v1.0/tobs")
def tobs(): #Displays the dates and temp. observations of the most active station for the last year of data.
# Create a session link
session = Session(engine)
# query to find the last date of the data
last_date = session.query(Measurement.date).\
order_by(Measurement.date.desc()).first()
#save the last date in a date variable (query_date)
last_date = last_date.date
yr = int(last_date[0:4])
mo = int(last_date[5:7])
day = int(last_date[8:10])
query_date = dt.date(yr, mo, day)
#find the station with the most observations in the last year of data
data = session.query(Measurement.tobs, Measurement.station).\
filter(Measurement.date > query_date - dt.timedelta(days = 365)).\
order_by(Measurement.date).all()
data_df = pd.DataFrame(data)
grouped = data_df.groupby('station').count().sort_values('tobs', ascending = False)
most_active = grouped.index[0]
#query the data to find the most active station's observations in the last year
results = session.query(Measurement.date, Measurement.tobs).\
filter(Measurement.date > query_date - dt.timedelta(days = 365)).\
filter(Measurement.station == most_active).all()
session.close()
all_observations = []
for date, tobs in results:
observations_dict = {}
observations_dict['date'] = date
observations_dict['tobs'] = tobs
all_observations.append(observations_dict)
return jsonify(all_observations)
@app.route("/api/v1.0/<start>")
def temp_breakdown(start): #Returns a list of the minimum temp, the average temp, and the max temp for given start date.
#Create session link
session = Session(engine)
#query all the data from the start date on
data = session.query(Measurement.tobs).\
filter(Measurement.date >= start).all()
session.close()
#Converts to df and uses describe() to find the max, min, and mean
data_df = pd.DataFrame(data)
described = data_df.describe()
return(
f'Starting Date: {start}<br/>'
f'minimum temp: {described.iloc[3,0]}<br/>'
f'maximum temp: {described.iloc[7,0]}<br/>'
f'average temp: {round(described.iloc[1,0], 1)}'
)
@app.route("/api/v1.0/<start>/<end>")
def start_end_breakdown(start, end):
#Create session link
session = Session(engine)
#query all the data from the start date on
data = session.query(Measurement.tobs).\
filter(Measurement.date >= start).\
filter(Measurement.date <= end).all()
session.close()
#Converts to df and uses describe() to find the max, min, and mean
data_df = pd.DataFrame(data)
described = data_df.describe()
return(
f'Starting Date: {start}<br/>'
f'Ending Date: {end}<br/>'
f'<br/>'
f'minimum temp: {described.iloc[3,0]}<br/>'
f'maximum temp: {described.iloc[7,0]}<br/>'
f'average temp: {round(described.iloc[1,0], 1)}'
)
if __name__ == '__main__':
app.run(debug = True)