1 changed files with 51 additions and 0 deletions
@ -0,0 +1,51 @@ |
|||||
|
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) |
||||
Loading…
Reference in new issue