-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
39 lines (28 loc) · 1.11 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
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 render_results():
zip_code = request.form['zipCode']
api_key= get_api_key()
data = get_weather_results(zip_code, api_key)
temp= "{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, temp=temp, 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= "http://api.openweathermap.org/data/2.5/weather?zip={}&units=metric&appid={}".format(zip_code,api_key)
r= requests.get(api_url)
return r.json()
#print(get_weather_results("95129",get_api_key()))
if __name__ == '__main__':
app.run()