-
Notifications
You must be signed in to change notification settings - Fork 0
/
climate-app.py
155 lines (102 loc) · 4.61 KB
/
climate-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
147
148
149
150
151
152
153
154
import numpy as np
import pandas as pd
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)
Base.classes.keys()
# Save reference to the table
Measurement = Base.classes.measurement
Station = Base.classes.station
session= Session(engine)
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@app.route("/")
def home_page():
return (
f"Available Routes: <br/>"
f"/api/v1.0/precipitation <br/>"
f"/api/v1.0/stations <br/>"
f"/api/v1.0/tobs <br/>"
f"-------------------------------------------<br/>"
f"-----data_search (yyyy-mm-dd)<br/>"
f"/api/v1.0/start_date <br/>"
f"/api/v1.0/start_date/end_date <br/>"
)
@app.route("/api/v1.0/stations")
def stations():
results = session.query(Station.name).all()
# Convert list of tuples into normal list
all_stations = list(np.ravel(results))
return jsonify(all_stations)
@app.route("/api/v1.0/precipitation")
def precipitation():
recent_date = (session.query(Measurement.date).order_by(Measurement.date.desc()).first())
recent_date = list(np.ravel(recent_date))[0]
recent_date = dt.datetime.strptime(recent_date, '%Y-%m-%d')
recent_year = int(dt.datetime.strftime(recent_date, '%Y'))
recent_month = int(dt.datetime.strftime(recent_date,'%m'))
recent_day = int(dt.datetime.strftime(recent_date,'%d'))
previous_year = dt.date(recent_year,recent_month,recent_day) - dt.timedelta(days=365)
previous_year = dt.datetime.strftime(previous_year, '%Y-%m-%d')
results = session.query(Measurement.date, Measurement.prcp, Measurement.station).filter(Measurement.date > previous_year).order_by(Measurement.date).all()
precipitation_data = []
for result in results:
precipitation_dict = {result.date : result.prcp}
precipitation_data.append(precipitation_dict)
return jsonify(precipitation_data)
@app.route("/api/v1.0/tobs")
def tobs():
recent_date = (session.query(Measurement.date).order_by(Measurement.date.desc()).first())
recent_date = list(np.ravel(recent_date))[0]
recent_date = dt.datetime.strptime(recent_date, '%Y-%m-%d')
recent_year = int(dt.datetime.strftime(recent_date, '%Y'))
recent_month = int(dt.datetime.strftime(recent_date,'%m'))
recent_day = int(dt.datetime.strftime(recent_date,'%d'))
previous_year = dt.date(recent_year,recent_month,recent_day) - dt.timedelta(days=365)
previous_year = dt.datetime.strftime(previous_year, '%Y-%m-%d')
results = (session.query(Measurement.date, Measurement.tobs, Measurement.station).\
filter(Measurement.date > previous_year).order_by(Measurement.date).all())
return jsonify(results)
@app.route("/api/v1.0/<start>")
def start_temp(start):
Start_date = dt.datetime.strptime(start, "%Y-%m-%d")
temp_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.date >= Start_date).all()
session.close()
# Convert list of tuples into normal list
result = list(np.ravel(temp_data))
return jsonify(temp_data)
@app.route("/api/v1.0/<start>/<end>")
def trip_date(start, end):
sel = [Measurement.date, func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)]
start = dt.datetime.strptime(start, "%Y-%m-%d")
end = dt.datetime.strptime(end, "%Y-%m-%d")
results = (session.query(*sel).\
filter(func.strftime("%Y-%m-%d", Measurement.date) >= start).\
filter(func.strftime("%Y-%m-%d", Measurement.date) <= end).group_by(Measurement.date).all())
dates =[]
for result in results:
start_end_dict = {}
start_end_dict['Date'] = result[0]
start_end_dict["Min Temp"] = result[1]
start_end_dict["Avg Temp"] = result[2]
start_end_dict["Min Temp"] = result[3]
dates.append(start_end_dict)
return jsonify(dates)
if __name__ == '__main__':
app.run(debug=True)