-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
130 lines (106 loc) · 4.04 KB
/
app.py
File metadata and controls
130 lines (106 loc) · 4.04 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
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, inspect
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)
# Save reference to the table
Measurement= Base.classes.measurement
Station= Base.classes.station
#################################################
# Flask Setup
#################################################
app = Flask(__name__)
#################################################
# Flask Routes
#################################################
@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>(start date as 'yyyy-mm-dd')<br/>"
f"/api/v1.0/<start>/<end>(end start date as 'yyyy-mm-dd')<br/>"
)
@app.route("/api/v1.0/precipitation")
def precipitation():
# Create our session (link) from Python to the DB
session = Session(engine)
# previous year calculate
# Query all passengers
previous_year = dt.date(2017,8,23) - dt.timedelta(days=365)
query_result= session.query(Measurement.date, Measurement.prcp).\
filter(Measurement.date > previous_year).all()
precipitation_results=dict(query_result)
session.close()
return jsonify(precipitation_results)
@app.route("/api/v1.0/stations")
def stations():
# Create our session (link) from Python to the DB
session = Session(engine)
station_list=session.query(Station.station).all()
stations=list(np.ravel(station_list))
session.close()
return jsonify(stations)
@app.route("/api/v1.0/tobs")
def temperature():
# Create our session (link) from Python to the DB
session = Session(engine)
previous_year = dt.date(2017,8,23) - dt.timedelta(days=365)
most_lastyear_tobs=session.query(Measurement.tobs)\
.filter(Measurement.station=='USC00519281')\
.filter(Measurement.date >= previous_year).all()
temperature=list(np.ravel(most_lastyear_tobs))
session.close()
return jsonify(temperature)
@app.route("/api/v1.0/<start>")
def start(start):
# Create our session (link) from Python to the DB
session = Session(engine)
results=session.query(func.min(Measurement.tobs),func.max(Measurement.tobs),func.avg(Measurement.tobs)).\
filter(Measurement.date>=start).all()
results_start=list(np.ravel(results))
session.close()
return jsonify(results_start)
@app.route("/api/v1.0/<start>/<end>")
def start_end(start, end):
# Create our session (link) from Python to the DB
session = Session(engine)
results=session.query(func.min(Measurement.tobs),func.max(Measurement.tobs),func.avg(Measurement.tobs)).\
filter(Measurement.date>=start).\
filter(Measurement.date<=end).all()
results_start=list(np.ravel(results))
return jsonify(results_start)
session.close()
# @app.route("/api/v1.0/passengers")
# def passengers():
# # Create our session (link) from Python to the DB
# session = Session(engine)
# """Return a list of passenger data including the name, age, and sex of each passenger"""
# # Query all passengers
# results = session.query(Passenger.name, Passenger.age, Passenger.sex).all()
# session.close()
# # Create a dictionary from the row data and append to a list of all_passengers
# all_passengers = []
# for name, age, sex in results:
# passenger_dict = {}
# passenger_dict["name"] = name
# passenger_dict["age"] = age
# passenger_dict["sex"] = sex
# all_passengers.append(passenger_dict)
# return jsonify(all_passengers)
if __name__ == '__main__':
app.run(debug=True)