-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
139 lines (102 loc) · 4.24 KB
/
Copy pathapp.py
File metadata and controls
139 lines (102 loc) · 4.24 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
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
import os
cwd = os.getcwd()
print("My current working directory is: {} ".format(cwd))
os.chdir("/Users/apple/Desktop/bootcamp/9-Advanced-Data-Storage-and-Retrieval/sqlalchemy-challenge")
cwd = os.getcwd()
print("My current working directory is: {} ".format(cwd))
##################################################
#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)
cwd = os.getcwd()
print("My current working directory is: {} ".format(cwd))
# Save references to each table
Measurement = Base.classes.measurement
Station = Base.classes.station
# Flask Setup
#################################################
app = Flask(__name__)
@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")
#Convert the query results to a dictionary using `date` as the key and `prcp` as the value.
def precipitation():
#Start Session
session=Session(engine)
#Query result
last_twelve=session.query(Measurement.date,func.avg(Measurement.prcp)).\
filter(Measurement.date>='2016-08-23').\
group_by(Measurement.date).\
order_by(Measurement.date).all()
session.close()
#Define Dictionary
precipitation_dict={}
for i in range(len(last_twelve)):
precipitation_dict[last_twelve[i][0]]=last_twelve[i][1]
#Return the JSON representation of your dictionary.
return jsonify(precipitation_dict)
@app.route("/api/v1.0/stations")
def stations():
#Query result
session=Session(engine)
observation_counts=session.query(Measurement.station,func.count(Measurement.id)).\
group_by(Measurement.station).\
order_by(func.count(Measurement.id).desc()).all()
session.close()
#Create list of Stations
station_list=[]
for i in range(len(observation_counts)):
station_list.append(observation_counts[i][0])
#Return a JSON list of stations from the dataset
return jsonify(station_list)
@app.route("/api/v1.0/tobs")
#Query the dates and temperature observations of the most active station for the last year of data.
#Return a JSON list of temperature observations (TOBS) for the previous year.
def tobs():
#Query the dates and temperature observations of the most active station for the last year of data.
session=Session(engine)
Station_list=session.query(Measurement.station).group_by(Measurement.station).order_by(func.count(Measurement.station).desc()).all()
MA_station=Station_list[0][0]
MA_station_data=session.query(Measurement.date,Measurement.tobs).\
filter(Measurement.date>='2016-08-23').\
filter(Measurement.station==MA_station).all()
session.close()
#Return a JSON list of temperature observations (TOBS) for the previous year.
return jsonify(MA_station_data)
@app.route("/api/v1.0/<start>")
def start(start):
#When given the start only, calculate `TMIN`, `TAVG`, and `TMAX` for all dates greater than and equal to the start date.
session=Session(engine)
TMIN_TAVG_TMAX=session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
filter(Measurement.date >= start).all()
session.close()
return jsonify(TMIN_TAVG_TMAX)
@app.route("/api/v1.0/<start>/<end>")
#When given the start and the end date, calculate the `TMIN`, `TAVG`, and `TMAX` for dates between the start and end date inclusive.
def calc_temps(start, end):
session=Session(engine)
TMIN_TAVG_TMAX_se=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()
return jsonify(TMIN_TAVG_TMAX_se)
if __name__ == "__main__":
app.run(debug=True)