diff options
| author | Sam Chudnick <sam@chudnick.com> | 2023-06-11 09:36:33 -0400 | 
|---|---|---|
| committer | Sam Chudnick <sam@chudnick.com> | 2023-06-11 09:36:33 -0400 | 
| commit | b3c58c63f0cba1a67c8669f776b2e7c841e1997a (patch) | |
| tree | 7a2def8ca7195646586fba04150c5a802d9654fa /src | |
| parent | 83f821550b5ddcf8dcb1859294f68868425aa904 (diff) | |
Show location with weather info
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.py | 27 | ||||
| -rw-r--r-- | src/static/style.css | 23 | ||||
| -rw-r--r-- | src/templates/weather.html | 19 | 
3 files changed, 53 insertions, 16 deletions
| @@ -17,7 +17,7 @@ def check_submission(location): | |||
| 17 | if (latitude, longitude) == (0, 0): | 17 | if (latitude, longitude) == (0, 0): | 
| 18 | return flask.redirect(flask.url_for('location', location=location)) | 18 | return flask.redirect(flask.url_for('location', location=location)) | 
| 19 | else: | 19 | else: | 
| 20 | return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) | 20 | return flask.redirect(flask.url_for('weather', location=location, latitude=latitude, longitude=longitude)) | 
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | @app.route('/', methods=('GET', 'POST')) | 23 | @app.route('/', methods=('GET', 'POST')) | 
| @@ -32,18 +32,28 @@ def index(): | |||
| 32 | 32 | ||
| 33 | @app.route('/weather', methods=('GET', 'POST')) | 33 | @app.route('/weather', methods=('GET', 'POST')) | 
| 34 | def weather(): | 34 | def weather(): | 
| 35 | location = flask.request.args.get('location') | ||
| 35 | latitude = flask.request.args.get('latitude', type=str) | 36 | latitude = flask.request.args.get('latitude', type=str) | 
| 36 | longitude = flask.request.args.get('longitude', type=str) | 37 | longitude = flask.request.args.get('longitude', type=str) | 
| 37 | data = library.get_data(latitude, longitude) | 38 | data = library.get_data(latitude, longitude) | 
| 38 | |||
| 39 | hour = library.get_current_rounded_time(data["timezone"]).hour | 39 | hour = library.get_current_rounded_time(data["timezone"]).hour | 
| 40 | form = forms.WeatherForm() | 40 | form = forms.WeatherForm() | 
| 41 | |||
| 42 | url = f"https://geocoding-api.open-meteo.com/v1/search?name={location}&count=10&language=en&format=json" | ||
| 43 | headers = {"User-Agent": "pywttr 0.1"} | ||
| 44 | if 'location_index' in flask.request.args: | ||
| 45 | location_data = requests.get(url, headers=headers).json( | ||
| 46 | )["results"][flask.request.args.get('location_index', type=int)] | ||
| 47 | else: | ||
| 48 | location_data = requests.get(url, headers=headers).json()["results"][0] | ||
| 49 | |||
| 41 | if form.validate_on_submit(): | 50 | if form.validate_on_submit(): | 
| 42 | location = form.location.data | 51 | location = form.location.data | 
| 43 | return check_submission(location) | 52 | return check_submission(location) | 
| 44 | else: | 53 | else: | 
| 45 | return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, | 54 | return flask.render_template("weather.html", data=data, form=form, weather_codes=library.weather_codes, datetime=datetime, | 
| 46 | weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon) | 55 | weather_icons=library.weather_icons, hour=hour, get_direction_icon=library.get_direction_icon, | 
| 56 | location_data=location_data) | ||
| 47 | 57 | ||
| 48 | 58 | ||
| 49 | @app.route('/location', methods=('GET', 'POST')) | 59 | @app.route('/location', methods=('GET', 'POST')) | 
| @@ -67,13 +77,14 @@ def location(): | |||
| 67 | form.location.choices = choices | 77 | form.location.choices = choices | 
| 68 | form.location.default = choices[0] | 78 | form.location.default = choices[0] | 
| 69 | if form.is_submitted(): | 79 | if form.is_submitted(): | 
| 70 | location = data["results"][int(form.location.data)] | 80 | index = int(form.location.data) | 
| 71 | latitude = location["latitude"] | 81 | location_data = data["results"][index] | 
| 72 | longitude = location["longitude"] | 82 | latitude = location_data["latitude"] | 
| 73 | return flask.redirect(flask.url_for('weather', latitude=latitude, longitude=longitude)) | 83 | longitude = location_data["longitude"] | 
| 84 | return flask.redirect(flask.url_for('weather', location=location, location_index=index, latitude=latitude, longitude=longitude)) | ||
| 74 | else: | 85 | else: | 
| 75 | return flask.render_template("location.html", data=data, form=form) | 86 | return flask.render_template("location.html", data=data, form=form) | 
| 76 | 87 | ||
| 77 | 88 | ||
| 78 | if __name__ == "__main__": | 89 | if __name__ == "__main__": | 
| 79 | app.run(debug=True) | 90 | 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 { | |||
| 58 | border: 3px solid var(--accent); | 58 | border: 3px solid var(--accent); | 
| 59 | } | 59 | } | 
| 60 | 60 | ||
| 61 | div.topbar { | ||
| 62 | display: flex; | ||
| 63 | width: 100%; | ||
| 64 | } | ||
| 65 | |||
| 66 | div.location { | ||
| 67 | display: flex; | ||
| 68 | align-items: center; | ||
| 69 | justify-content: center; | ||
| 70 | width: auto; | ||
| 71 | margin-left: auto; | ||
| 72 | margin-right: 5px; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | p.location { | ||
| 77 | text-align: center; | ||
| 78 | font-size: 16pt; | ||
| 79 | } | ||
| 80 | |||
| 61 | div.searchbar { | 81 | div.searchbar { | 
| 62 | display: flex; | 82 | display: flex; | 
| 63 | align-items: left; | ||
| 64 | justify-content: left; | 83 | justify-content: left; | 
| 65 | margin-left: 5px; | 84 | margin-left: 5px; | 
| 66 | width: 100%; | ||
| 67 | margin-top: 1%; | 85 | margin-top: 1%; | 
| 86 | width: 50%; | ||
| 68 | } | 87 | } | 
| 69 | 88 | ||
| 70 | div.current { | 89 | 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 @@ | |||
| 1 | {% extends 'base.html' %} | 1 | {% extends 'base.html' %} | 
| 2 | {% block content %} | 2 | {% block content %} | 
| 3 | <div class=content> | 3 | <div class=content> | 
| 4 | <div class=searchbar> | 4 | <div class=topbar> | 
| 5 | <form method="post"> | 5 | <div class=searchbar> | 
| 6 | {{ form.csrf_token }} | 6 | <form method="post"> | 
| 7 | {{ form.location }} | 7 | {{ form.csrf_token }} | 
| 8 | {{ form.submit() }} | 8 | {{ form.location }} | 
| 9 | </form> | 9 | {{ form.submit() }} | 
| 10 | </form> | ||
| 11 | </div> | ||
| 12 | <div class=location> | ||
| 13 | <p class=location> | ||
| 14 | {{ location_data["name"]}}, {{ location_data["admin1"]}}, {{ location_data["country_code"]}} | ||
| 15 | </p> | ||
| 16 | </div> | ||
| 10 | </div> | 17 | </div> | 
| 11 | <div class=current> | 18 | <div class=current> | 
| 12 | {% set is_day = data["current_weather"]["is_day"] %} | 19 | {% set is_day = data["current_weather"]["is_day"] %} | 
