# Import dependencies
import datetime as dt
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:///Hawaii.sqlite")
# reflect the database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Save reference to the table
Station = Base.classes.station
Measurement = Base.classes.measurement
# Create our session (link) from Python to the DB
session = Session(engine)

# Flask setup
app = Flask(__name__)

# Flask routes
@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"Available Routes:<br/>"
        f"<br/>"
        f"/api/v1.0/precipitation<br/>"
        f"- List of prior year rain totals from all stations<br/>"
        f"<br/>"
        f"/api/v1.0/stations<br/>"
        f"- List of Station numbers and names<br/>"
        f"<br/>"
        f"/api/v1.0/tobs<br/>"
        f"- List of prior year temperatures from all stations<br/>"
        f"<br/>"
        f"/api/v1.0/start<br/>"
        f"- When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date<br/>"
        f"<br/>"
        f"/api/v1.0/start/end<br/>"
        f"- When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive<br/>"
    )

# List of prior year rain totals from all stations
@app.route("/api/v1.0/precipitation")
def precipitation():
    last_date = session.query(Measurement.date).order_by(
        Measurement.date.desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    rain = session.query(Measurement.date, Measurement.prcp).\
        filter(Measurement.date > last_year).\
        order_by(Measurement.date).all()
# Convert the query results to a dictionary using date as the key and precipitation as the value
    rain_totals = []
    for result in rain:
        row = {}
        row["date"] = rain[0]
        row["prcp"] = rain[1]
        rain_totals.append(row)

    return jsonify(rain_totals)

# Return a JSON list of stations from the dataset
@app.route("/api/v1.0/stations")
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())

# Query for the dates and temperature observations from a year from the last data point
@app.route("/api/v1.0/tobs")
def tobs():
    last_date = session.query(Measurement.date).\
        order_by(Measurement.date.desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).\
        filter(Measurement.date > last_year).\
        order_by(Measurement.date).all()
# Return a JSON list of Temperature Observations (tobs) for the previous year
    temp_totals = []
    for result in temp:
        row = {}
        row["date"] = temp[0]
        row["tobs"] = temp[1]
        temp_totals.append(row)

    return jsonify(temp_totals)

# 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(start):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date-last_year
    end = dt.date(2017, 8, 23)
    trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
        filter(Measurement.date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)

# 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 start_end(start, end):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    end_date = dt.datetime.strptime(end, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date-last_year
    end = end_date-last_year
    trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
        filter(Measurement.date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


# Program run
if __name__ == "__main__":
    app.run(debug=True)

9b3604233a8779d22501ed692577297aab415de9
<mask token>


@app.route('/api/v1.0/stations')
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())


@app.route('/api/v1.0/tobs')
def tobs():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    temp_totals = []
    for result in temp:
        row = {}
        row['date'] = temp[0]
        row['tobs'] = temp[1]
        temp_totals.append(row)
    return jsonify(temp_totals)


<mask token>

<mask token>


@app.route('/')
def welcome():
    """List all available api routes."""
    return (
        f'Available Routes:<br/><br/>/api/v1.0/precipitation<br/>- List of prior year rain totals from all stations<br/><br/>/api/v1.0/stations<br/>- List of Station numbers and names<br/><br/>/api/v1.0/tobs<br/>- List of prior year temperatures from all stations<br/><br/>/api/v1.0/start<br/>- When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date<br/><br/>/api/v1.0/start/end<br/>- When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive<br/>'
        )


@app.route('/api/v1.0/precipitation')
def precipitation():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    rain = session.query(Measurement.date, Measurement.prcp).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    rain_totals = []
    for result in rain:
        row = {}
        row['date'] = rain[0]
        row['prcp'] = rain[1]
        rain_totals.append(row)
    return jsonify(rain_totals)


@app.route('/api/v1.0/stations')
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())


@app.route('/api/v1.0/tobs')
def tobs():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    temp_totals = []
    for result in temp:
        row = {}
        row['date'] = temp[0]
        row['tobs'] = temp[1]
        temp_totals.append(row)
    return jsonify(temp_totals)


@app.route('/api/v1.0/<start>')
def start(start):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date - last_year
    end = dt.date(2017, 8, 23)
    trip_data = session.query(func.min(Measurement.tobs), func.avg(
        Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.
        date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


<mask token>

<mask token>


@app.route('/')
def welcome():
    """List all available api routes."""
    return (
        f'Available Routes:<br/><br/>/api/v1.0/precipitation<br/>- List of prior year rain totals from all stations<br/><br/>/api/v1.0/stations<br/>- List of Station numbers and names<br/><br/>/api/v1.0/tobs<br/>- List of prior year temperatures from all stations<br/><br/>/api/v1.0/start<br/>- When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date<br/><br/>/api/v1.0/start/end<br/>- When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive<br/>'
        )


@app.route('/api/v1.0/precipitation')
def precipitation():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    rain = session.query(Measurement.date, Measurement.prcp).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    rain_totals = []
    for result in rain:
        row = {}
        row['date'] = rain[0]
        row['prcp'] = rain[1]
        rain_totals.append(row)
    return jsonify(rain_totals)


@app.route('/api/v1.0/stations')
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())


@app.route('/api/v1.0/tobs')
def tobs():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    temp_totals = []
    for result in temp:
        row = {}
        row['date'] = temp[0]
        row['tobs'] = temp[1]
        temp_totals.append(row)
    return jsonify(temp_totals)


@app.route('/api/v1.0/<start>')
def start(start):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date - last_year
    end = dt.date(2017, 8, 23)
    trip_data = session.query(func.min(Measurement.tobs), func.avg(
        Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.
        date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


@app.route('/api/v1.0/<start>/<end>')
def start_end(start, end):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    end_date = dt.datetime.strptime(end, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date - last_year
    end = end_date - last_year
    trip_data = session.query(func.min(Measurement.tobs), func.avg(
        Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.
        date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


<mask token>

<mask token>
engine = create_engine('sqlite:///Hawaii.sqlite')
Base = automap_base()
Base.prepare(engine, reflect=True)
Station = Base.classes.station
Measurement = Base.classes.measurement
session = Session(engine)
app = Flask(__name__)


@app.route('/')
def welcome():
    """List all available api routes."""
    return (
        f'Available Routes:<br/><br/>/api/v1.0/precipitation<br/>- List of prior year rain totals from all stations<br/><br/>/api/v1.0/stations<br/>- List of Station numbers and names<br/><br/>/api/v1.0/tobs<br/>- List of prior year temperatures from all stations<br/><br/>/api/v1.0/start<br/>- When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date<br/><br/>/api/v1.0/start/end<br/>- When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive<br/>'
        )


@app.route('/api/v1.0/precipitation')
def precipitation():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    rain = session.query(Measurement.date, Measurement.prcp).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    rain_totals = []
    for result in rain:
        row = {}
        row['date'] = rain[0]
        row['prcp'] = rain[1]
        rain_totals.append(row)
    return jsonify(rain_totals)


@app.route('/api/v1.0/stations')
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())


@app.route('/api/v1.0/tobs')
def tobs():
    last_date = session.query(Measurement.date).order_by(Measurement.date.
        desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).filter(
        Measurement.date > last_year).order_by(Measurement.date).all()
    temp_totals = []
    for result in temp:
        row = {}
        row['date'] = temp[0]
        row['tobs'] = temp[1]
        temp_totals.append(row)
    return jsonify(temp_totals)


@app.route('/api/v1.0/<start>')
def start(start):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date - last_year
    end = dt.date(2017, 8, 23)
    trip_data = session.query(func.min(Measurement.tobs), func.avg(
        Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.
        date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


@app.route('/api/v1.0/<start>/<end>')
def start_end(start, end):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    end_date = dt.datetime.strptime(end, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date - last_year
    end = end_date - last_year
    trip_data = session.query(func.min(Measurement.tobs), func.avg(
        Measurement.tobs), func.max(Measurement.tobs)).filter(Measurement.
        date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


if __name__ == '__main__':
    app.run(debug=True)

# Import dependencies
import datetime as dt
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:///Hawaii.sqlite")
# reflect the database into a new model
Base = automap_base()
# reflect the tables
Base.prepare(engine, reflect=True)
# Save reference to the table
Station = Base.classes.station
Measurement = Base.classes.measurement
# Create our session (link) from Python to the DB
session = Session(engine)

# Flask setup
app = Flask(__name__)

# Flask routes
@app.route("/")
def welcome():
    """List all available api routes."""
    return (
        f"Available Routes:<br/>"
        f"<br/>"
        f"/api/v1.0/precipitation<br/>"
        f"- List of prior year rain totals from all stations<br/>"
        f"<br/>"
        f"/api/v1.0/stations<br/>"
        f"- List of Station numbers and names<br/>"
        f"<br/>"
        f"/api/v1.0/tobs<br/>"
        f"- List of prior year temperatures from all stations<br/>"
        f"<br/>"
        f"/api/v1.0/start<br/>"
        f"- When given the start only, calculate TMIN, TAVG, and TMAX for all dates greater than and equal to the start date<br/>"
        f"<br/>"
        f"/api/v1.0/start/end<br/>"
        f"- When given the start and the end date, calculate the TMIN, TAVG, and TMAX for dates between the start and end date inclusive<br/>"
    )

# List of prior year rain totals from all stations
@app.route("/api/v1.0/precipitation")
def precipitation():
    last_date = session.query(Measurement.date).order_by(
        Measurement.date.desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    rain = session.query(Measurement.date, Measurement.prcp).\
        filter(Measurement.date > last_year).\
        order_by(Measurement.date).all()
# Convert the query results to a dictionary using date as the key and precipitation as the value
    rain_totals = []
    for result in rain:
        row = {}
        row["date"] = rain[0]
        row["prcp"] = rain[1]
        rain_totals.append(row)

    return jsonify(rain_totals)

# Return a JSON list of stations from the dataset
@app.route("/api/v1.0/stations")
def station():
    station_query = session.query(Station.name, Station.station)
    stations = pd.read_sql(station_query.statement, station_query.session.bind)
    return jsonify(stations.to_dict())

# Query for the dates and temperature observations from a year from the last data point
@app.route("/api/v1.0/tobs")
def tobs():
    last_date = session.query(Measurement.date).\
        order_by(Measurement.date.desc()).first()
    last_year = dt.date(2017, 8, 23) - dt.timedelta(days=365)
    temp = session.query(Measurement.date, Measurement.tobs).\
        filter(Measurement.date > last_year).\
        order_by(Measurement.date).all()
# Return a JSON list of Temperature Observations (tobs) for the previous year
    temp_totals = []
    for result in temp:
        row = {}
        row["date"] = temp[0]
        row["tobs"] = temp[1]
        temp_totals.append(row)

    return jsonify(temp_totals)

# 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(start):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date-last_year
    end = dt.date(2017, 8, 23)
    trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
        filter(Measurement.date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)

# 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 start_end(start, end):
    start_date = dt.datetime.strptime(start, '%Y-%m-%d')
    end_date = dt.datetime.strptime(end, '%Y-%m-%d')
    last_year = dt.timedelta(days=365)
    start = start_date-last_year
    end = end_date-last_year
    trip_data = session.query(func.min(Measurement.tobs), func.avg(Measurement.tobs), func.max(Measurement.tobs)).\
        filter(Measurement.date >= start).filter(Measurement.date <= end).all()
    trip = list(np.ravel(trip_data))
    return jsonify(trip)


# Program run
if __name__ == "__main__":
    app.run(debug=True)
