-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
186 lines (145 loc) · 5.87 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import numpy as np
import sqlalchemy
import datetime as dt
import numpy as np
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, func, distinct, desc
from flask import Flask, jsonify
from datetime import date
#################################################
# 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 reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
return (
"<strong>Precipitation</strong><br> http://127.0.0.1:5000/api/v1.0/precipitation<br/>"
"<strong>Stations</strong><br> http://127.0.0.1:5000/api/v1.0/stations<br/>"
"<strong>Temperature observations of the most active station for the last year</strong><br> http://127.0.0.1:5000/api/v1.0/tobs<br/>"
"<strong>Temperature observations by starting date (year-month-day)</strong><br> http://127.0.0.1:5000/api/v1.0/{date}<br/>"
"<strong>Temperature observations by range of dates (year-month-day)</strong><br> http://127.0.0.1:5000/api/v1.0/{star_date}/{end_date}<br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation_query():
session = Session(engine)
today = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
today_datetime = date.fromisoformat(today[0])
one_year_ago = today_datetime - dt.timedelta(days=365)
precipitation_by_date_results = session.query(
Measurement.date, Measurement.prcp
).filter(
Measurement.date >= one_year_ago
).all()
session.close()
list = []
for row in precipitation_by_date_results:
list.append({row.date: row.prcp})
return jsonify(list)
@app.route("/api/v1.0/stations")
def stations_query():
session = Session(engine)
results = session.query(
Station.id, Station.station, Station.name, Station.latitude, Station.longitude, Station.elevation
).all()
session.close()
stations = list(np.ravel(results))
return jsonify(results)
@app.route("/api/v1.0/tobs")
def statistical_temperature_query():
session = Session(engine)
today = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
today_datetime = date.fromisoformat(today[0])
one_year_ago = today_datetime - dt.timedelta(days=365)
stations_numbers = session.query(
Measurement.station,
func.count(Measurement.station).label("stations_count")
).group_by(
Measurement.station
).order_by(
desc("stations_count")
).all()
first_one = True
most_active_station = ""
for row in stations_numbers:
if first_one :
most_active_station = row.station
first_one = False
temperature_observations = session.query(
Measurement.tobs,
func.count(Measurement.tobs).label("observations")
).filter(
Measurement.date >= one_year_ago
).filter(
Measurement.station == most_active_station
).group_by(
Measurement.tobs
).order_by(
desc("observations")
).all()
session.close()
return jsonify(temperature_observations)
@app.route("/api/v1.0/<date>")
def statistical_temperature_query_by_initial_date(date):
date_parts = date.split('-')
start_date = dt.date(int(date_parts[0]), int(date_parts[1]), int(date_parts[2]))
session = Session(engine)
station_temperature = session.query(
func.max(Measurement.tobs).label("highest_temperature"),
func.avg(Measurement.tobs).label("average_temperature"),
func.min(Measurement.tobs).label("lowest_temperature")
).filter(
Measurement.date >= start_date
)
session.close()
results = station_temperature.one()
highest_temperature = results.highest_temperature
lowest_temperature = results.lowest_temperature
average_temperature = results.average_temperature
list = []
list.append({"Highest temperature" : highest_temperature})
list.append({"Lowest temperature" : lowest_temperature})
list.append({"Average temperature" : average_temperature})
return jsonify(list)
@app.route("/api/v1.0/<star_date>/<end_date>")
def statistical_temperature_query_by_dates_range(star_date, end_date):
start_date_parts = star_date.split('-')
end_date_parts = end_date.split('-')
start_date_date = dt.date(int(start_date_parts[0]), int(start_date_parts[1]), int(start_date_parts[2]))
end_date_date = dt.date(int(end_date_parts[0]), int(end_date_parts[1]), int(end_date_parts[2]))
session = Session(engine)
station_temperature = session.query(
func.max(Measurement.tobs).label("highest_temperature"),
func.avg(Measurement.tobs).label("average_temperature"),
func.min(Measurement.tobs).label("lowest_temperature")
).filter(
Measurement.date >= start_date_date
).filter(
Measurement.date <= end_date_date
)
session.close()
results = station_temperature.one()
highest_temperature = results.highest_temperature
lowest_temperature = results.lowest_temperature
average_temperature = results.average_temperature
list = []
list.append({"Highest temperature" : highest_temperature})
list.append({"Lowest temperature" : lowest_temperature})
list.append({"Average temperature" : average_temperature})
return jsonify(list)
if __name__ == '__main__':
app.run(debug=True)