-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
44 lines (33 loc) · 1.09 KB
/
app.py
File metadata and controls
44 lines (33 loc) · 1.09 KB
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
import requests
import configparser
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def weather_dashboard():
return render_template('home.html')
@app.route('/results', methods=['POST'])
def weather_results():
zip_code = request.form['zipCode']
api_key = get_api_key()
data = get_weather_results(zip_code, api_key)
temperature = "{0:.2f}".format(data["main"]["temp"])
feels_like = "{0:.2f}".format(data["main"]["feels_like"])
weather = data["weather"][0]["main"]
location = data["name"]
return render_template(
"results.html",
location=location,
temperature=temperature,
feels_like=feels_like,
weather=weather
)
def get_api_key():
config = configparser.ConfigParser()
config.read('config.ini')
return config['openweathermap']['api']
def get_weather_results(zip_code, api_key):
api_url = f"http://api.openweathermap.org/data/2.5/weather?zip={zip_code}&units=metric&appid={api_key}"
r = requests.get(api_url)
return r.json()
if __name__ == '__main__':
app.run()