This repository has been archived by the owner on Jun 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
226 lines (174 loc) · 8.31 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Engineer Climate App
# Now that I have finished my precipitation and weather stations analysis, I must now use FLASK to engineer an API based on
# the queries that I have just executed.
# Load Modules
# Customizable style sheets and parameters for matplotlib
# from matplotlib import style
# style.use('fivethirtyeight')
# # Plot generator for bar, pie, line, scatter, box and other plots
# import matplotlib.pyplot as plt
# High-level math and basic algebra computing tool
import numpy as np
# Dataframe generator that prints out various types of datasets
import pandas as pd
# Date and time attributes such as year, month, day, hour, minute, second
import datetime as dt
# Python SQL toolkit and Object Relational Mapper
import sqlalchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, inspect, func
# Load flask function that serializes data to Java Open Script Notation format
from flask import Flask, jsonify
# Database Setup
##################
# Define file path and create engine
sqlite_file_path = "Resources/hawaii.sqlite"
sqlite_engine = create_engine(f"sqlite:///{sqlite_file_path}", connect_args={'check_same_thread': False})
# Reflect tables, detect and define classes
base = automap_base()
base.prepare(sqlite_engine, reflect=True)
Measurement = base.classes.measurement
Station = base.classes.station
# Flask Setup
###############
# Start session and initiate Flask
session = Session(sqlite_engine)
app = Flask(__name__)
# Flask Routes
################
# Identify All Possible Flask App Routes
@app.route("/")
def main():
# Intro
intro = (f"Welcome to Mathew Miller's Hawaii Climate Analysis API!<br/>"
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/temp/start/end")
return (intro)
@app.route("/api/v1.0/precipitation")
def precipitation():
print("Received precipitation api request.")
# Find the most recent date in the data set.
latest_dataset_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
latest_dataset_date_print = dt.date(2017,8,23)
# Define variable that finds the date one year ago to the day from the most recent date in the dataset
last_year = dt.date(2017,8,23) - dt.timedelta(days=365)
# Define variable that represents the query of the last 12 months of Percipitation data
last_years_precipitation_data = session.query(Measurement.date, Measurement.prcp).\
filter(Measurement.date >= last_year).\
order_by(Measurement.date).all()
# prepare the dictionary with the date as the key and the prcp value as the value
results_dict = {}
for result in last_years_precipitation_data:
results_dict[result[0]] = result[1]
return jsonify(results_dict)
@app.route("/api/v1.0/stations")
def stations():
raw_stations = session.query(Station.station).all()
all_stations = list(np.ravel(raw_stations))
return jsonify(stations=all_stations)
# print("Received station api request.")
# # Define variable that calculates the total number weather stations in the dataset
# total_weather_stations = session.query(Measurement.station).distinct().count()
# # Create the stations list of dictionaries
# stations_list = []
# for station in total_weather_stations:
# station_dict = {}
# station_dict["id"] = station.id
# station_dict["station"] = station.station
# station_dict["name"] = station.name
# station_dict["latitude"] = station.latitude
# station_dict["longitude"] = station.longitude
# station_dict["elevation"] = station.elevation
# stations_list.append(station_dict)
# return jsonify(stations_list)
@app.route("/api/v1.0/tobs")
def temp_monthl():
print("Received tobs api request.")
# # Find the most recent date in the data set.
# latest_dataset_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
# latest_dataset_date_print = dt.date(2017,8,23)
# # Define variable that finds the date one year ago to the day from the most recent date in the dataset
# last_year = dt.date(2017,8,23) - dt.timedelta(days=365)
# # With the most live station id, query the last 12 months of temperature observations (tobs)
# raw_stations = session.query(Measurement.tobs).\
# filter(Measurement.date >= last_year).\
# filter(Measurement.station == "USC00519281").\
# order_by(Measurement.date).all()
# # Create the tobs list of dictionaries
# tobs_list = []
# for result in raw_stations:
# tobs_dict = {}
# tobs_dict["date"] = result.date
# tobs_dict["station"] = result.station
# tobs_dict["tobs"] = result.tobs
# tobs_list.append(tobs_dict)
# return jsonify(tobs_list)
# Calculate the date 1 year ago from last date in database
prev_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
# Query the primary station for all tobs from the last year
raw_stations = session.query(Measurement.tobs).\
filter(Measurement.station == 'USC00519281').\
filter(Measurement.date >= prev_year).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(raw_stations))
# Return the results
return jsonify(temps=temps)
@app.route("/api/v1.0/temp/<start>")
# def start(start):
# print("Received start date api request.")
# # Find the most recent date in the data set.
# latest_dataset_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
# latest_dataset_date_print = dt.date(2017,8,23)
# # Define variable that calculates the minimum, maximum and average temperature from the Measurements class.
# temp_stats = [func.min(Measurement.tobs),
# func.max(Measurement.tobs),
# func.avg(Measurement.tobs)]
# # Create temperature stats list
# return_list = []
# date_dict = {'start_date': start, 'end_date': latest_dataset_date}
# return_list.append(date_dict)
# return_list.append({'Observation': 'TMIN', 'Temperature': temp_stats[0][0]})
# return_list.append({'Observation': 'TAVG', 'Temperature': temp_stats[0][1]})
# return_list.append({'Observation': 'TMAX', 'Temperature': temp_stats[0][2]})
# return jsonify(return_list)
@app.route("/api/v1.0/temp/<start>/<end>")
def stats(start=None, end=None):
print("Received start date and end date api request.")
# # Find the most recent date in the data set.
# latest_dataset_date = session.query(Measurement.date).order_by(Measurement.date.desc()).first()
# latest_dataset_date_print = dt.date(2017,8,23)
# # Define variable that finds the date one year ago to the day from the most recent date in the dataset
# last_year = dt.date(2017,8,23) - dt.timedelta(days=365)
# Define variable that calculates the minimum, maximum and average temperature from the Measurements class.
temp_stats = [func.min(Measurement.tobs),
func.max(Measurement.tobs),
func.avg(Measurement.tobs)]
# #create a list
# return_list = []
# date_dict = {'start_date': last_year, 'end_date': latest_dataset_date}
# return_list.append(date_dict)
# return_list.append({'Observation': 'TMIN', 'Temperature': temp_stats[0][0]})
# return_list.append({'Observation': 'TAVG', 'Temperature': temp_stats[0][1]})
# return_list.append({'Observation': 'TMAX', 'Temperature': temp_stats[0][2]})
# return jsonify(return_list)
if not end:
# calculate TMIN, TAVG, TMAX for dates greater than start
raw_stations = session.query(*temp_stats).\
filter(Measurement.date >= start).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(raw_stations))
return jsonify(temps)
# calculate TMIN, TAVG, TMAX with start and stop
raw_stations = session.query(*temp_stats).\
filter(Measurement.date >= start).\
filter(Measurement.date <= end).all()
# Unravel results into a 1D array and convert to a list
temps = list(np.ravel(raw_stations))
return jsonify(temps=temps)
#code to actually run
if __name__ == "__main__":
app.run()