-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
129 lines (102 loc) · 4.66 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
# Step 2 - Climate App
# Use FLASK to create your routes.
#import dependencies
from flask import Flask, jsonify
import datetime as dt
import numpy as np
import pandas as pd
import datetime as dt
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
# Database Setup
engine = create_engine("sqlite:///Resources/hawaii.sqlite")
# reflect the database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Save reference to the table
Station = Base.classes.station
Measurement = Base.classes.measurement
# Create our session (link) from Python to the DB
session = Session(engine)
#Flask setup
app = Flask(__name__)
#Home routes /
@app.route("/")
def home():
return(
f"Available Routes:<br/>"
f"<br/>"
f"1) List of prior year precipitation from all stations<br/>"
f"/api/v1.0/precipitation<br/>"
f"<br/>"
f"2) List of Station numbers and names<br/>"
f"/api/v1.0/stations<br/>"
f"<br/>"
f"3) List of prior year temperatures from all stations<br/>"
f"/api/v1.0/tobs<br/>"
f"<br/>"
f"4) When given the start date as YYYY-MM-DD, calculates the MIN/AVG/MAX temperature for all dates greater than and equal to the start date<br/>"
f"/api/v1.0/<start><br/>"
f"<br/>"
f"5) When given the start and end date as YYYY-MM-DD/YYYY-MM-DD, calculate the MIN/AVG/MAX temperature for dates between the start and end date inclusive<br/>"
f"/api/v1.0/<start><end><br/>"
)
#/api/v1.0/precipitation
# Convert the query results to a Dictionary using date as the key and prcp as the value.
# (query to retrieve the last 12 months of precipitation data)
# Return the JSON representation of your dictionary.
@app.route("/api/v1.0/precipitation")
def precipitation():
latest_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
year_prior = dt.datetime.strptime(latest_date[0], '%Y-%m-%d') - dt.timedelta(days=365)
#dt.datetime(2017, 8, 23) - dt.timedelta(days=365)
last_12 = session.query(Measurement.date, Measurement.prcp).filter(Measurement.date >= year_prior.date()).all()
last_yr_rain = []
for date, prcp in last_12:
rain = {}
rain["date"] = date
rain["prcp"] = prcp
last_yr_rain.append(rain)
return jsonify(last_yr_rain)
# # Return a JSON list of stations from the dataset.
@app.route("/api/v1.0/stations")
def stations():
stations_query = session.query(Station.name, Station.station).all()
station_list = list(np.ravel(stations_query))
return jsonify(station_list)
# query for the dates and temperature observations from a year from the last data point.
# Return a JSON list of Temperature Observations (tobs) for the previous year.
@app.route("/api/v1.0/tobs")
def tobs():
latest_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
year_prior = dt.datetime.strptime(latest_date[0], '%Y-%m-%d') - dt.timedelta(days=365)
temp_results = session.query(Measurement.date, Measurement.station, Measurement.tobs).filter(Measurement.date >= year_prior.date()).all()
temp_list = list(np.ravel(temp_results))
return jsonify(temp_list)
# # Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range.
# # When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date.
# # When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive.
@app.route("/api/v1.0/<start>")
def startdate(start):
start_date= dt.datetime.strptime(start, '%Y-%m-%d')
last_year = dt.timedelta(days=365)
start = start_date-last_year
end = dt.date(2017, 8, 23)
trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).filter(Measurement.date <= end).all()
trip = list(np.ravel(trip_data))
return jsonify(trip)
@app.route("/api/v1.0/<start>/<end>")
def startend(start,end):
start_date= dt.datetime.strptime(start, '%Y-%m-%d')
end_date= dt.datetime.strptime(end,'%Y-%m-%d')
last_year = dt.timedelta(days=365)
start = start_date-last_year
end = end_date-last_year
trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).filter(Measurement.date <= end).all()
trip = list(np.ravel(trip_data))
return jsonify(trip)
if __name__ == "__main__":
app.run(debug=True)