-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
168 lines (129 loc) · 5.35 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
import numpy as np
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, inspect
from flask import Flask, jsonify
import datetime as dt
# Relative Date
#################################################
# Database Setup
#################################################
engine = create_engine("sqlite:///Resources/hawaii.sqlite", connect_args={'check_same_thread': False}, echo=True)
# 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 our session (link) from Python to the DB
session = Session(engine)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
"""List all available api routes."""
return"""<html>
<h1>List of all available Honolulu, HI API routes</h1>
<ul>
<br>
<li>
Return a list of precipitations from last year:
<br>
<a href="/api/v1.0/precipitation">/api/v1.0/precipitation</a>
</li>
<br>
<li>
Return a JSON list of stations from the dataset:
<br>
<a href="/api/v1.0/stations">/api/v1.0/stations</a>
</li>
<br>
<li>
Return a JSON list of Temperature Observations (tobs) for the previous year:
<br>
<a href="/api/v1.0/tobs">/api/v1.0/tobs</a>
</li>
<br>
<li>
Return a JSON list of tmin, tmax, tavg for the dates greater than or equal to the date provided:
<br>Replace <start> with a date in Year-Month-Day format.
<br>
<a href="/api/v1.0/2017-01-01">/api/v1.0/2017-01-01</a>
</li>
<br>
<li>
Return a JSON list of tmin, tmax, tavg for the dates in range of start date and end date inclusive:
<br>
Replace <start> and <end> with a date in Year-Month-Day format.
<br>
<br>
<a href="/api/v1.0/2017-01-01/2017-01-07">/api/v1.0/2017-01-01/2017-01-07</a>
</li>
<br>
</ul>
</html>
"""
@app.route("/api/v1.0/precipitation")
def precipitation():
# Docstring
"""Return a list of precipitations from last year"""
# Design a query to retrieve the last 12 months of precipitation data and plot the results
max_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
# Get the first element of the tuple
max_date = max_date[0]
# Calculate the date 1 year ago from today
# The days are equal 366 so that the first day of the year is included
year_ago = dt.datetime.strptime(max_date, "%Y-%m-%d") - dt.timedelta(days=366)
# Perform a query to retrieve the data and precipitation scores
results_precipitation = session.query(Measurement.date, Measurement.prcp).filter(Measurement.date >= year_ago).all()
# Convert list of tuples into normal list
precipitation_dict = dict(results_precipitation)
return jsonify(precipitation_dict)
@app.route("/api/v1.0/stations")
def stations():
# Docstring
"""Return a JSON list of stations from the dataset."""
# Query stations
results_stations = session.query(Measurement.station).group_by(Measurement.station).all()
# Convert list of tuples into normal list
stations_list = list(np.ravel(results_stations))
return jsonify(stations_list)
@app.route("/api/v1.0/tobs")
def tobs():
# Docstring
"""Return a JSON list of Temperature Observations (tobs) for the previous year."""
# Design a query to retrieve the last 12 months of precipitation data and plot the results
max_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
# Get the first element of the tuple
max_date = max_date[0]
# Calculate the date 1 year ago from today
# The days are equal 366 so that the first day of the year is included
year_ago = dt.datetime.strptime(max_date, "%Y-%m-%d") - dt.timedelta(days=366)
# Query tobs
results_tobs = session.query(Measurement.date, Measurement.tobs).filter(Measurement.date >= year_ago).all()
# Convert list of tuples into normal list
tobs_list = list(results_tobs)
return jsonify(tobs_list)
@app.route("/api/v1.0/<start>")
def start(start=None):
# Docstring
"""Return a JSON list of tmin, tmax, tavg for the dates greater than or equal to the date provided"""
from_start = session.query(Measurement.date, func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).group_by(Measurement.date).all()
from_start_list=list(from_start)
return jsonify(from_start_list)
@app.route("/api/v1.0/<start>/<end>")
def start_end(start=None, end=None):
# Docstring
"""Return a JSON list of tmin, tmax, tavg for the dates in range of start date and end date inclusive"""
between_dates = session.query(Measurement.date, func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).filter(Measurement.date <= end).group_by(Measurement.date).all()
between_dates_list=list(between_dates)
return jsonify(between_dates_list)
if __name__ == '__main__':
app.run(debug=True)