-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
136 lines (92 loc) · 3.6 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
import numpy as np
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_table = Base.classes.measurement
station_table = 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><br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
"""Return a list of all passenger names"""
# Query all passengers
results = session.query(measurement_table.date,measurement_table.prcp).\
filter(measurement_table.date > '2016-08-23').all()
return jsonify(results)
session.close()
@app.route("/api/v1.0/stations")
def stations():
# Create our session (link) from Python to the DB
session = Session(engine)
# """Return a list of passenger data including the name, age, and sex of each passenger"""
# Query all passengers
results = session.query(station_table.station).all()
return jsonify(results)
session.close()
# # Create a dictionary from the row data and append to a list of all_passengers
# all_passengers = []
# for name, age, sex in results:
# passenger_dict = {}
# passenger_dict["name"] = name
# passenger_dict["age"] = age
# passenger_dict["sex"] = sex
# all_passengers.append(passenger_dict)
# return jsonify(all_passengers)
@app.route("/api/v1.0/tobs")
def temperature():
# Create our session (link) from Python to the DB
session = Session(engine)
results=session.query(measurement_table.tobs).filter(measurement_table.station=="USC00519281").\
filter(measurement_table.date >= '2016-08-23').all()
results_one=list(np.ravel(results))
return jsonify(results_one)
session.close()
@app.route("/api/v1.0/<start>")
def temp_start(start=None):
# Create our session (link) from Python to the DB
session = Session(engine)
results=session.query(func.min(measurement_table.tobs), func.avg(measurement_table.tobs), func.max(measurement_table.tobs)).\
filter(measurement_table.date >= start).all()
results=list(np.ravel(results))
return jsonify(results)
session.close()
@app.route("/api/v1.0/<start>/<end>")
def temp_start_end(start=None,end=None):
# Create our session (link) from Python to the DB
session = Session(engine)
results=session.query(func.min(measurement_table.tobs), func.avg(measurement_table.tobs), func.max(measurement_table.tobs)).\
filter(measurement_table.date >= start).filter(measurement_table.date <= end).all()
results=list(np.ravel(results))
return jsonify(results)
session.close()
if __name__ == '__main__':
app.run(debug=True)