From b3c58c63f0cba1a67c8669f776b2e7c841e1997a Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sun, 11 Jun 2023 09:36:33 -0400 Subject: Show location with weather info --- src/app.py | 27 +++++++++++++++++++-------- src/static/style.css | 23 +++++++++++++++++++++-- src/templates/weather.html | 19 +++++++++++++------ 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/app.py b/src/app.py index 6a59ac7..449f37a 100644 --- a/src/app.py +++ b/src/app.py @@ -17,7 +17,7 @@ def check_submission(location): if (latitude, longitude) == (0, 0): return flask.redirect(flask.url_for('location', location=location)) else: - return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) + return flask.redirect(flask.url_for('weather', location=location, latitude=latitude, longitude=longitude)) @app.route('/', methods=('GET', 'POST')) @@ -32,18 +32,28 @@ def index(): @app.route('/weather', methods=('GET', 'POST')) def weather(): + location = flask.request.args.get('location') latitude = flask.request.args.get('latitude', type=str) longitude = flask.request.args.get('longitude', type=str) data = library.get_data(latitude, longitude) - hour = library.get_current_rounded_time(data["timezone"]).hour form = forms.WeatherForm() + + url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" + headers = {"User-Agent": "pywttr 0.1"} + if 'location_index' in flask.request.args: + location_data = requests.get(url, headers=headers).json( + )["results"][flask.request.args.get('location_index', type=int)] + else: + location_data = requests.get(url, headers=headers).json()["results"][0] + if form.validate_on_submit(): location = form.location.data return check_submission(location) else: return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, - weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) + weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon, + location_data=location_data) @app.route('/location', methods=('GET', 'POST')) @@ -67,13 +77,14 @@ def location(): form.location.choices = choices form.location.default = choices[0] if form.is_submitted(): - location = data["results"][int(form.location.data)] - latitude = location["latitude"] - longitude = location["longitude"] - return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) + index = int(form.location.data) + location_data = data["results"][index] + latitude = location_data["latitude"] + longitude = location_data["longitude"] + return flask.redirect(flask.url_for('weather', location=location, location_index=index, latitude=latitude, longitude=longitude)) else: return flask.render_template("location.html", data=data, form=form) if __name__ == "__main__": - app.run(debug=True) + app.run() diff --git a/src/static/style.css b/src/static/style.css index 1ea44f5..768b816 100644 --- a/src/static/style.css +++ b/src/static/style.css @@ -58,13 +58,32 @@ html, body, .container { border: 3px solid var(--accent); } +div.topbar { + display: flex; + width: 100%; +} + +div.location { + display: flex; + align-items: center; + justify-content: center; + width: auto; + margin-left: auto; + margin-right: 5px; +} + + +p.location { + text-align: center; + font-size: 16pt; +} + div.searchbar { display: flex; - align-items: left; justify-content: left; margin-left: 5px; - width: 100%; margin-top: 1%; + width: 50%; } div.current { diff --git a/src/templates/weather.html b/src/templates/weather.html index a222ad8..ccd736c 100644 --- a/src/templates/weather.html +++ b/src/templates/weather.html @@ -1,12 +1,19 @@ {% extends 'base.html' %} {% block content %}
-