-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_web_app.py
44 lines (32 loc) · 1.14 KB
/
weather_web_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
37
38
39
40
41
42
43
44
from flask import Flask, render_template, request
# import json to load JSON data to a python dictionary
import json
# urllib.request to make a request to api
import urllib.request
app = Flask(__name__)
@app.route('/', methods =['POST', 'GET'])
def weather():
if request.method == 'POST':
city = request.form['city']
else:
# for default name mathura
city = 'mathura'
# your API key will come here
api = api_key_here
# source contain json data from api
source = urllib.request.urlopen('http://api.openweathermap.org/data/2.5/weather?q =' + city + '&appid =' + api).read()
# converting JSON data to a dictionary
list_of_data = json.loads(source)
# data for variable list_of_data
data = {
"country_code": str(list_of_data['sys']['country']),
"coordinate": str(list_of_data['coord']['lon']) + ' '
+ str(list_of_data['coord']['lat']),
"temp": str(list_of_data['main']['temp']) + 'k',
"pressure": str(list_of_data['main']['pressure']),
"humidity": str(list_of_data['main']['humidity']),
}
print(data)
return render_template('index.html', data = data)
if __name__ == '__main__':
app.run(debug = True)