-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
72 lines (51 loc) · 2.06 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Creates a flask app using a template website.
"""
import requests
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from configparser import ConfigParser
config = ConfigParser()
config.read('config/config.ini')
APP = Flask(__name__)
APP.config['DEBUG'] = config.get('main', 'DEBUG')
APP.config['SQLALCHEMY_DATABASE_URI'] = config.get('main', 'SQLALCHEMY_DATABASE_URI')
APP.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config.get('main', 'SQLALCHEMY_TRACK_MODIFICATIONS')
print('config loded successfully')
DB = SQLAlchemy(APP)
class City(DB.Model):
id = DB.Column(DB.Integer, primary_key=True)
name = DB.Column(DB.String(50), nullable=False)
@APP.route('/', methods=['GET', 'POST'])
def index():
"""Checks the request made is POST and commits else gets the data of the cities already
present in the sqlite database.
Returns:
render_template : returns the rendered template.
"""
if request.method == 'POST':
new_city = request.form.get('city')
if new_city:
new_city_obj = City(name=new_city)
DB.session.add(new_city_obj)
DB.session.commit()
cities = City.query.all()
config.read('secret/secret.ini')
api_key=config.get('main', 'api_key')
print('secret loded successfully')
url = 'http://api.openweathermap.org/data/2.5/weather?q={}&units=imperial&appid='+api_key
weather_data = []
for city in cities:
req_city = requests.get(url.format(city.name)).json()
weather = {
'city' : city.name,
'temperature' : req_city['main']['temp'],
'description' : req_city['weather'][0]['description'],
'icon' : req_city['weather'][0]['icon'],
}
weather_data.append(weather)
return render_template('weather.html', weather_data=weather_data)
if __name__ == "__main__":
from waitress import serve
serve(APP, host="0.0.0.0", port=5000)
# APP.run(host='0.0.0.0', port=5000, debug=True)