You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5 KiB

from flask import Flask
import requests
app = Flask(__name__)
#https://home.openweathermap.org/api_keys
API_KEY = "API_KLIC"
MESTO = "Štětí"
# altGR + C -> &
url1 = f"https://api.openweathermap.org/data/2.5/"
url2 = f"weather?q={MESTO}&appid={API_KEY}&units=metric&lang=cz"
url = url1+url2
def nacti_pocasi():
"""Získá aktualní data z OpenWeatherMap API"""
try:
response = requests.get(url)
if response.status_code == 200:
data = response.json()
#vytahování dat z JSON struktury
vysledek = {
"mesto": data["name"],
"teplota": data["main"]["temp"],
"vlhkost": data["main"]["humidity"],
"popis": data["weather"][0]["description"]
}
return vysledek
else:
return {"chyba":f"Chyba API: {response.status_code}. Zkontorluj API"}
except Exception as e:
return {"chyba":f"Nepovedená akce: {e}"}
@app.route("/")
def index():
"""Hlavní stránka dashboardu."""
pocasi = nacti_pocasi()
if "chyba" in pocasi:
return f"<h1>ERROR</h1><p>{pocasi["chyba"]}</p>"
html = f"""
<h1> Počasí {pocasi["mesto"]}</h1>
<ul>
<li>Teplota: {pocasi["teplota"]}°C</li>
<li>Vlhkost: {pocasi["vlhkost"]}%</li>
<li>Popis: {pocasi["popis"]}.</li>
</ul>
"""
return html
if __name__ == "__main__":
print("----Spouštím Dashboard pro počasí----")
print(f"Město: {MESTO}")
app.run(host="0.0.0.0",port=8080,debug=True)