-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
47 lines (37 loc) · 1.21 KB
/
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
45
46
47
import requests
from flask import Flask, render_template
from form import SearchForm
url = 'http://api.openweathermap.org/data/2.5/weather?q={},uk&APPID=e16a84cd226983d486986572b34a3896'
app = Flask(__name__)
app.config['SECRET_KEY'] = 'suvasishdas114'
@app.route('/', methods=['GET', 'POST'])
@app.route('/index/')
def index():
city_name = ""
search_form = SearchForm()
city_name = search_form.city_name.data
print(city_name)
r = requests.get(url.format(city_name)).json()
weather = {
'icon': r['weather'][0]['icon'],
'city_name': r['name'],
'tempareture': round((r['main']['temp'])-273.15),
'description': r['weather'][0]['description']
}
print(weather)
return render_template('index.html', search_form=search_form, weather=weather)
# errors
@app.errorhandler(404)
@app.errorhandler(500)
def error(e):
error_code = 200
error_name = 'oops, error happen'
if e == 404:
error_code = 404
error_name = 'page not found'
else:
error_code = 500
error_name = 'internal server error'
return render_template('error.html', error_code=error_code, error_name=error_name)
if __name__ == '__main__':
app.run(debug=True)