-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
36 lines (28 loc) · 827 Bytes
/
app.py
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
29
30
31
32
33
34
35
36
from flask import Flask, render_template, request
import json
import urllib
def get_weather(query):
api_url = "http://api.openweathermap.org/data/2.5/weather?q={}&units=metric&appid=e20f1fb921b19bded4f8ddd7134a9486"
query = urllib.parse.quote(query)
url = api_url.format(query)
data = urllib.request.urlopen(url).read()
parsed = json.loads(data)
weather = None
if parsed['weather']:
weather = {
'description': parsed['weather'][0]['description'],
'temperature': parsed['main']['temp'],
'city': parsed['name'],
'country': parsed['sys']['country']
}
return weather
app = Flask(__name__)
@app.route("/")
def index():
city = request.args.get('city')
if not city:
city = "London,Uk"
return render_template(
"home.html",
weather=get_weather(city)
)