Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (87 loc) · 3.56 KB
/
Copy pathapp.py
File metadata and controls
121 lines (87 loc) · 3.56 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
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
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 reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route("/")
def welcome():
"""List all available api routes."""
return (
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/<start><br/>"
f"/api/v1.0/<start>/<end>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
"""Convert the query results to a dictionary using `date` as the key and `prcp` as the value."""
# Query dates for the last year of data
year_ago = dt.date(2017,8,23) - dt.timedelta(days=365)
results = session.query(Measurement.date,Measurement.prcp).filter(Measurement.date>=year_ago).all()
session.close()
# Convert list of tuples into normal list
precipitation = {date:prcp for date,prcp in results}
return jsonify(precipitation)
@app.route("/api/v1.0/stations")
def stations():
# Create our session (link) from Python to the DB
session = Session(engine)
"""Return a JSON list of stations from the dataset."""
# Query all stations
results = session.query(Station.station).all()
session.close()
return jsonify(results)
@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 for the last year of data."""
# Query all stations
year_ago = dt.date(2017,8,23) - dt.timedelta(days=365)
results = session.query(Measurement.tobs).filter(Measurement.date>=year_ago).filter(Measurement.station == "USC00519281").all()
session.close()
return jsonify(results)
@app.route("/api/v1.0/<start>")
def temp(start):
session = Session(engine)
"""Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range."""
# Query all stations
sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
data = session.query(*sel).filter(Measurement.date > start).all()
session.close()
return jsonify(data)
@app.route("/api/v1.0/<start>/<end>")
def temprange(start,end):
session = Session(engine)
"""Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range."""
# Query all stations
sel = [func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
data = session.query(*sel).filter(Measurement.date > start).filter(Measurement.date < end).all()
session.close()
return jsonify(data)
if __name__ == '__main__':
app.run(debug=True)