-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
148 lines (120 loc) · 5.36 KB
/
app.py
File metadata and controls
148 lines (120 loc) · 5.36 KB
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
from flask import Flask, jsonify
import numpy as np
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
######################################
#Setting path to the database file
database_path = "Resources/hawaii.sqlite"
# create engine to hawaii.sqlite
engine = create_engine(f"sqlite:///{database_path}")
# 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__)
#####################################
#Available routes
#####################################
#Home: display available routes
@app.route("/")
def home():
return(
f'Available routes <br/>'
f'----------------- <br/>'
f'Precipitation information: /api/v1.0/precipitation <br/>'
f'List of stations: /api/v1.0/stations <br/>'
f'Temperature observations of the most active station for the last year of data: /api/v1.0/tobs <br/>'
f'Minimum, average and maximum temperature observed, calculated from a given start date through the most recent date that readings were taken: /api/v1.0/<start><br/>'
f'(Enter date as 4 digit year, 2 digit month and 2 digit day: xxxx-xx-xx) <br/>'
f'Minimum, average and maximum temperature observed, calculated from a given start date through a given end date: /api/v1.0/<start>/<end> <br/>'
f'(Enter date as 4 digit year, 2 digit month and 2 digit day: YYYY-MM-DD)'
)
#Route for displaying all precipiation data from all stations
@app.route('/api/v1.0/precipitation')
def precipitation():
session = Session(engine)
sel = [Measurement.date, Measurement.prcp]
result = session.query(*sel).all()
session.close()
#Create a list, which will contain a dictionary of the dates and prcp information. Display in JSON format.
precip = []
for date, prcp in result:
precip_dict = {}
precip_dict['Date'] = date
precip_dict['Precipitation'] = prcp
precip.append(precip_dict)
return jsonify(precip)
#Route for displaying all stations (I also displayed station name with number)
@app.route('/api/v1.0/stations')
def stations():
session = Session(engine)
sel = [Station.station, Station.name]
result = session.query(*sel).group_by(Station.station)
session.close()
#Create a list, which will contain a dictionary of the station IDs and names. Display in JSON format.
stations = []
for station, name in result:
station_dict = {}
station_dict["Station"] = station
station_dict["Name"] = name
stations.append(station_dict)
return jsonify (stations)
#Route for displaying temperature readings from the most active station for the last year of data (using information found in jupyter notebook analysis)
@app.route('/api/v1.0/tobs')
def temps():
session = Session(engine)
last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
sel = [Measurement.date, Measurement.tobs]
result = session.query(*sel).filter(Measurement.date >= last_year).filter(Measurement.station == "USC00519281").all()
#Create a list, which will contain a dictionary of the dates and temp information. Display in JSON format.
temps = []
for date, temp in result:
temps_dict = {}
temps_dict["Date"] = date
temps_dict["Temperature"] = temp
temps.append(temps_dict)
return jsonify (temps)
#Route for displaying the min, avg, and max temps over a range from a given start date through the remainder of the data
@app.route('/api/v1.0/<start>')
def temps_start(start):
session = Session(engine)
temp_stats_start = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).all()
session.close()
#Create a list, which will contain a dictionary of the min, avg and max temp for the date range. Display in JSON format.
temp_start = []
for min, avg, max in temp_stats_start:
temp_start_dict = {}
temp_start_dict["Min Temp"] = min
temp_start_dict["Average Temp"] = avg
temp_start_dict["Max Temp"] = max
temp_start.append(temp_start_dict)
return jsonify(temp_start)
#Route for displaying the min, avg and max temps over a range from a given start and end date
@app.route('/api/v1.0/<start>/<end>')
def temps_stop(start,end):
session = Session(engine)
temp_stats_stop = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= start).filter(Measurement.date<= end).all()
session.close()
#Create a list, which will contain a dictionary of the min, avg and max temp for the date range. Display in JSON format.
temp_stop = []
for min, avg, max in temp_stats_stop:
temp_stop_dict = {}
temp_stop_dict["Min Temp"] = min
temp_stop_dict["Average Temp"] = avg
temp_stop_dict["Max Temp"] = max
temp_stop.append(temp_stop_dict)
return jsonify(temp_stop)
#Define main behavior
if __name__ == "__main__":
app.run(debug=True)