-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimate_app.py
195 lines (144 loc) · 7.83 KB
/
climate_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
#Importing Dependecies
import numpy as np
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
import datetime as dt
from flask import Flask, jsonify
###########################################################
# Database Setup
engine = create_engine("sqlite:////Users/diannejardinez/Desktop/sql-alchemy-challenge/Resources/hawaii.sqlite")
# reflect an existing database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Save reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station
###########################################################
# Flask Setup
app = Flask(__name__)
###########################################################
# Flask Routes
@app.route("/")
def welcome():
return (
#Listing all available API routes without instructions
"<h2>Welcome to Surfs Up! Hawaii Climate App</h2>"
"<p><em>All available api routes without instructions:</em><br>"
"/api/v1.0/precipitation<br>"
"/api/v1.0/stations<br>"
"/api/v1.0/tobs<br>"
"/api/v1.0/<start><br>"
"/api/v1.0/<start>/<end>"
"<hr>"
#Listing all available API routes with instructions
"<p><em>All available api routes with instructions:</em>"
"<p>Below are a list of all the available API Routes to find: <br> "
"precipitation recordings, stations, temperature observations<br>(also minimum, average, and maximum temperature observations of specific dates of user's choosing)</li></ul><br>"
"<p><strong>Instructions:</strong><br>"
"Add the below endpoint (after each colon ':') to the end of this webpage's url</p>"
"<strong>Precipitation recordings API Route:</strong> /api/v1.0/precipitation<br>"
"<strong>Stations API Route:</strong> /api/v1.0/stations<br>"
"<strong>Temperature observations:</strong> /api/v1.0/tobs<br>"
"<strong>One date with minimum, average, and maximum temperature observations</strong><br> (replace <start> with a date in the format of yyyy-mm-dd): /api/v1.0/<start><br>"
"<strong>Start and end date with minimum, average, and maximum temperature observations</strong><br> (replace <start> and <end> with dates in the format of yyyy-mm-dd): /api/v1.0/<start>/<end>"
)
#Return the JSON representation of a dictionary for dates
#as the key and precipitation as the value from the Precipitation Analysis query
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
#Query for the last date and the last 12 months for precipitation
#year ago date: 2016-08-23
year_ago = dt.date(2017,8, 23) - dt.timedelta(days=365)
#Query for 2016-2017 dates in measurement table
#Date range: 08/23/2016 to 08/23/2017
precipitation_results = session.query(Measurement.date, Measurement.prcp).\
filter(Measurement.date >= year_ago).all()
session.close()
# Create a dictionary from the row data and append to a list of all precipitation measurements
all_precipitation_results = []
for date, prcp in precipitation_results:
precipitation_results_dict = {}
precipitation_results_dict["date"] = date
precipitation_results_dict["prcp"] = prcp
all_precipitation_results.append(precipitation_results_dict)
#Return a list of all dates and precipitation
return jsonify(all_precipitation_results)
#Return a JSON list of stations from the dataset.
@app.route("/api/v1.0/stations")
def stations():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query station and station name
station_info = session.query(Station.station, Station.name).all()
session.close()
# Convert list of tuples into normal list
station_info_list = list(np.ravel(station_info))
#Return a list of all information about each station
return jsonify(station_info_list)
# Return a JSON list of temperature observations (TOBS) for the previous year
@app.route("/api/v1.0/tobs")
def tobs():
# Create our session (link) from Python to the DB
session = Session(engine)
#Query for the last date and the last 12 months for temperature observations
#(year ago date: 2016-08-23)
year_ago = dt.date(2017,8, 23) - dt.timedelta(days=365)
#Query the dates of the most active station from last year for the previous year temperature observations
#(date range: 08/23/2016 to 08/23/2017 for station USC00519281)
tobs_results = session.query(Measurement.date, Measurement.tobs).\
filter(Measurement.station == 'USC00519281').\
filter(Measurement.date >= year_ago).all()
session.close()
# Convert list of tuples into normal list
all_tobs_results = list(np.ravel(tobs_results))
#Return a list of all dates and temperature observations from previous year(08/23/2016 to 08/23/2017) for station USC00519281
return jsonify(all_tobs_results)
#Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start
@app.route("/api/v1.0/<start>")
def start_date(start):
#Fetch the minimum temperature, the average temperature, and
#the max temperature whose start date matches
#the path variable supplied by the user
# Create our session (link) from Python to the DB
session = Session(engine)
# Query to calculate the min, avg, and max Temperatures for a given date in the format %Y-%m-%d
# When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date
# From active station USC00519281
results = session.query(Measurement.date, func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.station == 'USC00519281').\
filter(func.strftime("%Y-%m-%d", Measurement.date) >= start).all()
#For loop and if statement for results query where if user api route date equals query date
for result in results:
search_date = start
if search_date == start:
#Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start
return jsonify(result)
#Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start-end range
@app.route("/api/v1.0/<start>/<end>")
def start_end_date(start, end):
"""Fetch the minimum temperature, the average temperature, and
the max temperature whose start date and end date matches
the path variable supplied by the user"""
# Create our session (link) from Python to the DB
session = Session(engine)
# Query to calculate the min, avg, and max Temperatures for given dates in the format %Y-%m-%d
# When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive
# From active station USC00519281
results = session.query(Measurement.date, func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.station == 'USC00519281').\
filter(func.strftime("%Y-%m-%d", Measurement.date) >= start).\
filter(Measurement.date <= end).all()
#For loop and if statement for results query where if user api route dates equals query dates
for result in results:
search_date1= start
search_date2 = end
if search_date1 == start and search_date2 == end:
#Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start-end range
return jsonify(result)
if __name__ == '__main__':
app.run(debug=True)