-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
123 lines (102 loc) · 3.74 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
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:///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 home():
return (
f"Welcome to the Climate API!<br/>"
f"<br/>"
f"Available Routes:<br/>"
f"/api/v1.0/precipitation<br/>"
f"/api/v1.0/stations<br/>"
f"/api/v1.0/tobs<br/>"
f"Searching example for the route below: /api/v1.0/2010-08-23 <= This is an Example!!!<br/>"
f"/api/v1.0/<start><br/>"
f"Searching example for the route below: /api/v1.0/2011-05-01/2011-05-04 <= This is an Example!!!<br/>"
f"/api/v1.0/<start>/<end><br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query all passengers
results = session.query(Measurement.date, Measurement.prcp).all()
session.close()
# Create a dictionary from the row data and append to a list of all_prcp
all_data = []
for prcp in results:
data_dict = {}
data_dict["Date"] = prcp
all_data.append(data_dict)
return jsonify(all_data)
@app.route("/api/v1.0/stations")
def stations():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query all stations
results = session.query(Station.station, Station.name, Station.latitude,
Station.longitude,Station.elevation).all()
session.close()
# Create a dictionary from the row data and append to a list of all_station
all_station = []
for station, name, latitude, longitude, elevation in results:
station_dict = {}
station_dict["Station"] = station
station_dict["Name"] = name
station_dict["Latitude"] = latitude
station_dict["Longitude"] = longitude
station_dict["Elevation"] = elevation
all_station.append(station_dict)
return jsonify(all_station)
@app.route("/api/v1.0/tobs")
def tobs():
# Create our session (link) from Python to the DB
session = Session(engine)
# Query all passengers
results = session.query(Measurement.station, Measurement.date, Measurement.tobs)\
.filter(Measurement.date <= dt.date(2017, 8, 23),Measurement.date >= dt.date(2016, 8, 23))\
.filter(Measurement.station == 'USC00519281')\
.all()
session.close()
# Create a dictionary from the row data and append to a list of all_station
all_tobs = []
for station, date, tobs in results:
tobs_dict = {}
tobs_dict["Station"] = station
tobs_dict["Date"] = date
tobs_dict["Temperature"] = tobs
all_tobs.append(tobs_dict)
return jsonify(all_tobs)
@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def search(start, end=None):
session = Session(engine)
# Query all variables
sel = [func.min(Measurement.tobs),
func.avg(Measurement.tobs),
func.max(Measurement.tobs)]
# if user does NOT provide end:
if end is None:
results = session.query(*sel).filter(Measurement.date >= start).all()
else:
results = session.query(*sel).filter(Measurement.date >= start,Measurement.date <= end).all()
session.close()
return(jsonify(results))
if __name__ == "__main__":
app.run(debug=True)