-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweatherapp.py
127 lines (91 loc) · 4 KB
/
weatherapp.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
###############################################
# FLASK API for querying Hawaii Weather data
###############################################
import numpy as np
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func
from flask import Flask, jsonify
#################################################
# Database Setup and connection
#################################################
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 reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station
# Create a session (link) from Python to the DB
session = Session(engine)
#########################
# Flask Setup
#########################
app = Flask(__name__)
###########################
# Flask Routes
###########################
# Query and return dates and precipitation values in json from the last year.
#
@app.route("/api/v1.0/precipitation")
def precipitation():
""" Return a list of dates and precipitation measurements for the last 12 months """
# Query all dates and prcp and filter for the last 12 months
results = session.query(Measurement.date, Measurement.prcp).\
filter(Measurement.date >= '2016-08-23').order_by(Measurement.date).all()
# Create a dictionary from the rows of data and append to a list
precipitation_last12mths = []
for p in results:
prcp_dict = {}
prcp_dict["date"] = p.date
prcp_dict["prcp"] = p.prcp
precipitation_last12mths.append(prcp_dict)
return jsonify(precipitation_last12mths)
# Return a Json list of stations from the dataset.
#
@app.route("/api/v1.0/stations")
def stations():
"""Return a list of all station names"""
# Query all stations
results = session.query(Station.name).all()
# Convert list of tuples into normal list
station_names = list(np.ravel(results))
return jsonify({"List of Stations" : station_names})
# Return a Json list of temperature Observations (tobs) for the previous year
#
@app.route("/api/v1.0/tobs")
def tobs():
"""Return a list of all temperature observations for the previous year"""
# Query all tobs
results = session.query(Measurement.tobs).all()
# Convert list of tuples into normal list
tobs_last12mths = list(np.ravel(results))
return jsonify(tobs_last12mths)
# Return a Json list of the minimum, average and the max temperatures from a given start date
#
@app.route("/api/v1.0/<start>")
def temperatures_start(start):
""" Calculate TMIN, TAVG, and TMAX for all dates from start date."""
# Query tobs for dates starting from
results = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs),\
func.max(Measurement.tobs)).filter(Measurement.date >= start).all()
# Convert list of tuples into normal list
temperatures_start_date = list(np.ravel(results))
return jsonify({"TMIN": temperatures_start_date[0], "TAVG": temperatures_start_date[1],
"TMAX": temperatures_start_date[2]})
# 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>/<end>")
def temperatures_start_end(start, end):
""" calculate the TMIN, TAVG,and TMAX from start and to end date inclusive."""
# Query all tobs and filter for dates given
results = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.date >= start).filter(Measurement.date <= end).all()
# Convert list of tuples into normal list
temperatures_start_end = list(np.ravel(results))
return jsonify({"TMIN" : temperatures_start_end[0], "TAVG" : temperatures_start_end[1],
"TMAX": temperatures_start_end[2]})
if __name__ == "__main__":
app.run(debug=True)