Skip to content

Commit 229c3ca

Browse files
committed
Checkpoint prof-rossetti#4
1 parent 19e89d0 commit 229c3ca

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

web_app/routes/weather_routes.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# web_app/routes/weather_routes.py
22

3-
from flask import Blueprint, request, jsonify
3+
from flask import Blueprint, request, jsonify, render_template, redirect
44

55
from app.weather_service import get_hourly_forecasts
66

77
weather_routes = Blueprint("weather_routes", __name__)
88

99
@weather_routes.route("/weather/forecast.json")
1010
def weather_forecast_api():
11+
1112
print("WEATHER FORECAST (API)...")
1213
print("URL PARAMS:", dict(request.args))
1314

@@ -18,4 +19,31 @@ def weather_forecast_api():
1819
if results:
1920
return jsonify(results)
2021
else:
21-
return jsonify({"message":"Invalid Geography. Please try again."}), 404
22+
return jsonify({"message":"Invalid Geography. Please try again."}), 404
23+
24+
@weather_routes.route("/weather/form")
25+
def weather_form():
26+
print("WEATHER FORM...")
27+
return render_template("weather_form.html")
28+
29+
@weather_routes.route("/weather/forecast", methods=["GET", "POST"])
30+
def weather_forecast():
31+
print("WEATHER FORECAST...")
32+
33+
if request.method == "GET":
34+
print("URL PARAMS:", dict(request.args))
35+
request_data = dict(request.args)
36+
elif request.method == "POST": # the form will send a POST
37+
print("FORM DATA:", dict(request.form))
38+
request_data = dict(request.form)
39+
40+
country_code = request_data.get("country_code") or "US"
41+
zip_code = request_data.get("zip_code") or "20057"
42+
43+
results = get_hourly_forecasts(country_code=country_code, zip_code=zip_code)
44+
if results:
45+
#flash(f"Weather Forecast Generated Successfully!", "success")
46+
return render_template("weather_forecast.html", country_code=country_code, zip_code=zip_code, results=results)
47+
else:
48+
#flash(f"Geography Error. Please try again!", "danger")
49+
return redirect("/weather/form")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{% extends "layout.html" %}
2+
3+
{% block content %}
4+
5+
<h2>Weather Forecast for {{ results["city_name"] }}</h2>
6+
7+
<p>Zip Code: {{ zip_code }}</p>
8+
9+
<!-- TODO: consider using a table instead of a list -->
10+
<!-- TODO: consider adding images using the provided icon urls -->
11+
<ul>
12+
{% for forecast in results["hourly_forecasts"] %}
13+
<li>{{ forecast["timestamp"] }} | {{ forecast["temp"] }} | {{ forecast["conditions"].upper() }}</li>
14+
{% endfor %}
15+
</ul>
16+
17+
{% endblock %}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{% extends "layout.html" %}
2+
3+
{% block content %}
4+
5+
<h2>Weather Form</h2>
6+
7+
<p>Request an hourly forecast for your zip code...</p>
8+
9+
<form action="/weather/forecast" method="POST">
10+
11+
<label>Country Code:</label>
12+
<input type="text" name="country_code" placeholder="US" value="US">
13+
<br>
14+
15+
<label>Zip Code:</label>
16+
<input type="text" name="zip_code" placeholder="20057" value="20057">
17+
<br>
18+
19+
<button>Submit</button>
20+
</form>
21+
22+
{% endblock %}

0 commit comments

Comments
 (0)