-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclimate_app.py
162 lines (118 loc) · 5.11 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
#import the dependencies
import numpy as np
import datetime as dt
from dateutil.relativedelta import relativedelta
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
#################################################
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 references to each table
Measurement = Base.classes.measurement
Station = Base.classes.station
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
# Create a home page route
@app.route("/")
def welcome():
"""List all available api routes."""
return (
f"Hawaii Climate Analysis and Exploration Routes:<br/><br/>"
f"/api/v1.0/precipitation<br/>Dictionary of date and precipitation<br/><br/>"
f"/api/v1.0/stations<br/>List of the weather stations<br/><br/>"
f"/api/v1.0/tobs<br/>Dictionary of date and tobs of the most active station for the last year of the data<br/><br/>"
f"/api/v1.0/<start_date_only><br/>Min, max, avg tobs for all dates greater than and equal to the start date.<br/><br/>"
f"/api/v1.0/<start_date>/<end_date><br/>Min, max, avg tobs for dates between the start and end date inclusive.<br/><br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query the date and prcp for the last 12 months
results = session.query(Measurement.date,Measurement.prcp).filter(Measurement.date>='2016-08-23').group_by(Measurement.date).order_by(Measurement.date).all()
session.close()
#create a dictionary using date as key and prcp as the value
precipitation_list = []
for date, prcp in results:
precipitation_dict = {}
precipitation_dict['date'] = date
precipitation_dict['prcp'] = prcp
precipitation_list.append(precipitation_dict)
#Return the JSON representation of your dictionary
return jsonify(precipitation_list)
@app.route("/api/v1.0/stations")
def stations ():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query the station name
result = session.query(Station.station).all()
session.close()
#create a list of stations
station_list = []
for station in result:
station_list.append(station)
#Return a JSON list of stations from the dataset
return jsonify(station_list)
@app.route("/api/v1.0/tobs")
def tobs():
# Create our session (link) from Python to the DB
session = Session(engine)
#Query the dates and temperature observations of the most active station
Result = session.query(Measurement.date,Measurement.tobs).filter(Measurement.date>='2016-08-23').filter(Station.station == Measurement.station).filter(Station.name == 'WAIHEE 837.5, HI US').all()
session.close()
#Return a JSON list of temperature observations (TOBS)
tobs_list = []
for date, tobs in Result:
tobs_dict = {}
tobs_dict['date'] = date
tobs_dict['tobs'] = tobs
tobs_list.append(tobs_dict)
#Return a JSON list of tobs from the dataset
return jsonify(tobs_list)
@app.route("/api/v1.0/<start_date_only>")
def StartDate(start_date_only):
# Create our session (link) from Python to the DB
session = Session(engine)
Results = session.query(Measurement.date, func.min(Measurement.tobs), func.max(Measurement.tobs),func.avg(Measurement.tobs)).filter(Measurement.date >= start_date_only).group_by(Measurement.date).all()
session.close()
#Return JSON list of max, min, avg tobs
start_list = []
for date,tmin,tmax,tavg in Results:
start_dict = {}
start_dict['Date'] = date
start_dict['TMIN'] = tmin
start_dict['TMAX'] = tmax
start_dict['TAVG'] = tavg
start_list.append(start_dict)
return jsonify(start_list)
@app.route("/api/v1.0/<start_date>/<end_date>")
def StartDateEndDate(start_date,end_date):
# Create our session (link) from Python to the DB
session = Session(engine)
Resultss = session.query(Measurement.date, func.min(Measurement.tobs), func.max(Measurement.tobs),func.avg(Measurement.tobs)).filter(Measurement.date >= start_date).filter(Measurement.date<=end_date).group_by(Measurement.date).all()
session.close()
#Return JSON list of max, min, avg tobs
startend_list = []
for date,Tmin,Tmax,Tavg in Resultss:
startend_dict = {}
startend_dict['Date'] = date
startend_dict['TMIN'] = Tmin
startend_dict['TMAX'] = Tmax
startend_dict['TAVG'] = Tavg
startend_list.append(startend_dict)
return jsonify(startend_list)
if __name__ == '__main__':
app.run(debug=True)