-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
146 lines (97 loc) · 3.61 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
#################################### AN APP.PY file
#DEPENDENCIES
###############################################
import datetime as dt
import numpy as np
import pandas as pd
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:///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
# Create our session (link) from Python to the DB
session = Session(engine)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# FLASK ROUTES
#################################################
@app.route("/")
def welcome():
return (
f"Welcome to the Climate Analysis API!<br/>"
f"Available Routes:<br/>"
f"/api/v1.0/precipitation<br/>"
f"/api/v1.0/stations<br/>"
f"/api/v1.0/tobs<br/>"
f"/api/v1.0/temp/start/end"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
prev_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
precipitation = session.query(Measurement.date, Measurement.prcp).\
filter(Measurement.date >= prev_year).all()
precip = {date: prcp for date, prcp in precipitation}
session.close()
return jsonify(precip)
@app.route("/api/v1.0/stations")
def stations():
results = session.query(Station.station).all()
stations = list(np.ravel(results))
session.close()
return jsonify(stations=stations)
@app.route("/api/v1.0/tobs")
def temp_monthly():
#Return the temperature observations (tobs) for previous year.
# Calculate the date 1 year ago from last date in database
prev_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
# Query the primary station for all tobs from the last year
results = session.query(Measurement.tobs).\
filter(Measurement.station == 'USC00519281').\
filter(Measurement.date >= prev_year).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(results))
session.close()
# Return the results
return jsonify(temps)
@app.route("/api/v1.0/temp/<start>")
@app.route("/api/v1.0/temp/<start>/<end>")
def stats(start=None, end=None):
##Return TMIN, TAVG, TMAX.
# Select statement
sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
print ("=============")
print (start)
print (end)
print ("=============")
if not end:
# calculate TMIN, TAVG, TMAX for dates greater than start
results = session.query(*sel).\
filter(Measurement.date >= start).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(results))
session.close()
return jsonify(temps)
# calculate TMIN, TAVG, TMAX with start and stop
results = session.query(*sel).\
filter(Measurement.date >= start).\
filter(Measurement.date <= end).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(results))
session.close()
return jsonify(temps)
if __name__ == '__main__':
app.run(debug=True)