-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
109 lines (82 loc) · 3.52 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
# Import the dependencies.
from flask import Flask, jsonify
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, desc, func
import pandas as pd
import datetime as dt
#################################################
# Database Setup
#################################################
app = Flask(__name__)
# reflect an existing database into a new model
# reflect the tables
# Save references to each table
engine = create_engine("sqlite:///Starter_Code/Resources/hawaii.sqlite")
Base = automap_base()
Base.prepare(autoload_with = engine)
measurement = Base.classes.measurement
station = Base.classes.station
# Create our session (link) from Python to the DB
session = Session(engine)
#################################################
# Flask Setup
#################################################
## PRECIPITATION
# last 12 months of data
date = session.query(measurement.date).order_by(desc(measurement.date)).first()
past_year = dt.datetime.strptime(date[0], '%Y-%m-%d') - dt.timedelta(365)
past_year_data = session.query(measurement.date, measurement.prcp).\
filter(measurement.date >= past_year).all()
prcp_dict = {d:p for d, p in past_year_data}
## STATION
stations = session.query(station.station, station.name).all()
stations_list = {id:loc for id, loc in stations}
## TOBS
# Using the most active station id
# Query the last 12 months of temperature observation data for this station and plot the results as a histogram
most_active_station = session.query(measurement.station, func.count(measurement.station)).group_by(measurement.station).order_by(desc(func.count(measurement.station))).first()
most_active_station_latest_date = session.query(measurement.date).order_by(desc(measurement.date)).filter(measurement.station == most_active_station[0]).first()
most_active_station_latest_date = dt.datetime.strptime(most_active_station_latest_date[0], '%Y-%m-%d')
most_active_station_past_year = most_active_station_latest_date - dt.timedelta(365)
most_active_station_past_year_data = session.query(measurement.date, measurement.tobs).\
filter(measurement.date >= most_active_station_past_year).\
filter(measurement.station == most_active_station[0]).all()
prcp_df = pd.DataFrame(most_active_station_past_year_data, columns = ['date', 'temp'])
prcp_df.values.tolist()
prcp_df['date'] = pd.to_datetime(prcp_df['date'])
tobs = {d:t for d, t in most_active_station_past_year_data}
#################################################
# Flask Routes
#################################################
@app.route("/")
def main():
return '''
<h2>Available routes:</h2>
/api/v1.0/precipitation<br>
/api/v1.0/stations<br>
/api/v1.0/tobs<br>
/api/v1.0/[start] and /api/v1.0/[start]/[end]
'''
@app.route("/api/v1.0/precipitation")
def prcp():
print(prcp_dict)
return prcp_dict
@app.route("/api/v1.0/stations")
def stations():
return stations_list
@app.route("/api/v1.0/tobs")
def most_active_station():
return tobs
@app.route("/api/v1.0/<start>")
@app.route("/api/v1.0/<start>/<end>")
def date_range(start, end = '2017-08-23'):
session = Session(engine)
[min,avg,max] = session.query(func.min(measurement.tobs),func.avg(measurement.tobs),func.max(measurement.tobs)).\
filter((measurement.date>=start)&(measurement.date<=end)).first()
return {'Min':min,'AVG':avg,'Max':max}
if __name__ == "__main__":
app.run(debug=True)
# @app.route("/api/v1.0/<start>")
# @app.route("/api/v1.0/<start>/<end>")
session.close()