2 changed files with 84 additions and 0 deletions
@ -0,0 +1,59 @@ |
|||
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) |
|||
@ -0,0 +1,25 @@ |
|||
import requests |
|||
import json |
|||
|
|||
#klíč získate na https://home.openweathermap.org/api_keys |
|||
KLIC = "f70d944f638a7ae77de89435d4d23c01" |
|||
MESTO = input("Zadej město:") |
|||
# 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 |
|||
|
|||
print(f"Otevírám odkaz: {url}") |
|||
odpoved = requests.get(url) |
|||
odpoved.raise_for_status() |
|||
data = odpoved.json() |
|||
#pro výpis celé json odpovědi |
|||
print(json.dumps(data,indent=4,ensure_ascii=False)) |
|||
|
|||
popis = data["weather"][0]["description"] |
|||
teplota = data["main"]["temp"] |
|||
owm_mesto = data["name"] |
|||
|
|||
print(f"Město: {owm_mesto} (napsáno:{MESTO})") |
|||
print(f"Teplota: {teplota}°C") |
|||
print(f"Popis: {popis}") |
|||
Loading…
Reference in new issue