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.
59 lines
1.6 KiB
59 lines
1.6 KiB
from flask import jsonify, Flask
|
|
import requests
|
|
import json
|
|
|
|
app = Flask(__name__)
|
|
|
|
#klíč získate na https://home.openweathermap.org/api_keys
|
|
KLIC = "f70d944f638a7ae77de89435d4d23c01"
|
|
MESTO = "Praha"
|
|
# AltGR + C = &
|
|
url1 = f"https://api.openweathermap.org/data/2.5/"
|
|
url2 = f"weather?q={MESTO}&appid={KLIC}&units=metric&lang=cz"
|
|
url = url1 + url2
|
|
|
|
def nacti_pocasi():
|
|
"""Získá data z OpenWeatherMap API"""
|
|
try:
|
|
response = requests.get(url)
|
|
response.raise_for_status()
|
|
if response.status_code == 200:
|
|
data = response.json()
|
|
# AltGr + B|N = {|}
|
|
results = {
|
|
"mesto":data["name"],
|
|
"teplota":data["main"]["temp"],
|
|
"vlhkost":data["main"]["humidity"],
|
|
"popis":data["weather"][0]["description"]
|
|
}
|
|
return results
|
|
else:
|
|
return {"chyba":f"{response.status_code}. Zkontroluj API klíč"}
|
|
except Exception as e:
|
|
return {"chyba":f"Chyba asi v síti: {e}"}
|
|
|
|
@app.route("/")
|
|
def index():
|
|
"""Hlavní stránka"""
|
|
pocasi = nacti_pocasi()
|
|
|
|
if "chyba" in pocasi:
|
|
return f"<h1>Chyba</h1><p>{pocasi['chyba']}</p>"
|
|
|
|
html = f"""
|
|
<h1>{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("-"*50)
|
|
print("Spouštím Dashboard na počasí")
|
|
print("adresa http://localhost:8080")
|
|
print(f"Město: {MESTO}")
|
|
print("-"*50)
|
|
app.run(host="0.0.0.0",port=8080,debug=True)
|