-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
172 lines (104 loc) · 4.63 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
from asyncio import start_server
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
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)
Measurement = Base.classes.measurement
Station= Base.classes.station
app = Flask(__name__)
# ## Step 2 - Climate App
# Now that you have completed your initial analysis, design a Flask API based on the queries that you have just developed.
# * Use Flask to create your routes.
# ### Routes
# * `/`
# * Home page.
# * List all routes that are available.
@app.route("/")
def Greetings():
return (
f"Available Routes in Hawaii Climate API:<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"
)
# * `/api/v1.0/precipitation`
# * Convert the query results to a dictionary using `date` as the key and `prcp` as the value.
# * Return the JSON representation of your dictionary.
@app.route("/api/v1.0/precipitation")
def rain():
session=Session(engine)
rain_by_date= session.query(Measurement.date, Measurement.prcp).filter(Measurement.date >= '2016-08-23')
session.close()
rain_latestyear=[]
for date, prcp in rain_by_date:
rain_dictionary={}
rain_dictionary["date"] = date
rain_dictionary["prcp"] = prcp
rain_latestyear.append(rain_dictionary)
return jsonify(rain_latestyear)
# * `/api/v1.0/stations`
@app.route("/api/v1.0/stations")
def stations():
session= Session(engine)
#create a query for all stations in the dataset
all_stations= session.query(Station.station).all()
session.close()
stations_list=list(np.ravel(all_stations))
# * Return a JSON list of stations from the dataset.
return jsonify(stations_list)
# * `/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.
@app.route("/api/v1.0/tobs")
def temp():
session=Session(engine)
dates_temp_mostactive=active_station_recenttemp=session.query(Measurement.date, Measurement.tobs)\
.filter(Measurement.station=='USC00519281')\
.filter(Measurement.date>= '2016-08-23').all()
session.close()
all_days_temp=list(np.ravel(dates_temp_mostactive))
return jsonify(all_days_temp)
# * When given the start only, calculate `TMIN`, `TAVG`, and `TMAX` for all dates greater than and equal to the start date.
@app.route("/api/v1.0/<start>")
def start_temp(start):
session=Session(engine)
specific_day_temp_stats=session.query(Measurement.date,func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
.filter(Measurement.date >=start).all()
session.close()
temps_pickday=list(np.ravel(specific_day_temp_stats))
return jsonify(temps_pickday)
# * When given the start and the end date, calculate the `TMIN`, `TAVG`, and `TMAX` for dates between the start and end date inclusive (>= & <=).
@app.route("/api/v1.0/<start>/<end>")
def end_to_start(start,end):
session=Session(engine)
date_interval_stats=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()
user_picks_dates=list(np.ravel(date_interval_stats))
return jsonify(user_picks_dates)
# * Return a JSON list of the minimum temperature, the average temperature, and the max temperature for a given start or start-end range.
@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def input(start=None, end=None):
session=Session(engine)
if not end:
only_start_andgreater=session.query(func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
.filter(Measurement.date >=start).all()
only_start_andgreater_stats=list(np.ravel(only_start_andgreater))
return jsonify(only_start_andgreater_stats)
start_and_end=session.query(func.min(Measurement.tobs),func.avg(Measurement.tobs),func.max(Measurement.tobs))\
.filter(Measurement.date >=start).filter(Measurement.date<=end).all()
start_and_end_stats=list(np.ravel(start_and_end))
return jsonify(start_and_end_stats)
session.close()
if __name__ == '__main__':
app.run(debug=True)