-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
28 lines (20 loc) · 734 Bytes
/
app.py
File metadata and controls
28 lines (20 loc) · 734 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from flask import Flask, request, render_template, redirect, url_for
import requests
with open(".env/api-key.pub", "r") as f:
KEY = f.readline()
app = Flask(__file__)
title = "Weather app"
@app.route("/")
def dashboard():
return render_template("index.html", title=title)
@app.route("/weather", methods=["POST"])
def weather():
city = request.form.get("city")
if city in [None,""]:
return redirect("/")
params = {"q": city, "appid": KEY, "units": "metric"}
url = "https://api.openweathermap.org/data/2.5/weather"
response = requests.get(url, params=params)
data = response.json()
response.close()
return render_template("results.html", weather=data, title=f"Weather in {city}")