Zadanie 5

This commit is contained in:
Mikołaj Kaczmarek 2025-06-15 22:12:02 +02:00
parent ee946112d7
commit 870b937bf4
No known key found for this signature in database
3 changed files with 17 additions and 13 deletions

View File

@ -93,11 +93,13 @@ def register():
sqlsession.commit()
return f"<h1>Zarejestrowano użytkownika: {username} ({nickname})</h1>"
@app.route("/pogoda")
def pogoda():
temp, humidity, type, rain = get_weather()
return render_template("pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain)
@app.route("/pogoda/<city>")
def pogoda(city):
result = get_weather(city)
if not result:
return f"<h2>Nie udało się pobrać pogody dla miasta: {city}</h2>", 404
temp, humidity, type, rain = result
return render_template("pogoda.html", temp=temp, humid=humidity, weathertype=type, rain=rain, city=city)
if __name__ == '__main__':
app.run(debug=False)

View File

@ -10,6 +10,7 @@ DIV ID col-sm-X gdzie x to szerokosc gdzie 12 to 100% strony (mozna podzielic st
<div id="pogoda">
<div class="container">
<div class="row">
<h2>Pogoda dla miasta: {{ city if city else 'nieznane' }}</h2>
<div id="weathertype">Weather: {{ weathertype }} </div>
<div class="col-sm-2">
<img src="/static/temp.png" class="weatherimg" id="tempimg">

View File

@ -1,3 +1,4 @@
import os
import requests
import json
@ -12,15 +13,15 @@ def decode_weather(weather):
return temp, humidity, type, rain
def get_weather():
weather_request = requests.get(
"http://samples.openweathermap.org/data/2.5/weather?q=Warsaw,pl&units=metric"
)
# Czyszczenie odpowiedzi...
def get_weather(city):
api_key = os.environ.get("OPENWEATHER_API_KEY")
if not api_key:
raise Exception("Brak klucza API w zmiennej środowiskowej OPENWEATHER_API_KEY")
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=metric&appid={api_key}&lang=pl"
weather_request = requests.get(url)
if weather_request.status_code != 200:
return None
weather = weather_request.content.strip()
temp, humidity, type, rain = decode_weather(weather)
return temp, humidity, type, rain