-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathapp.py
41 lines (32 loc) · 1 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
from flask import Flask, render_template, request
import requests
import var
import os
app = Flask(__name__)
def get_weather(api_key, city):
base_url = "http://api.openweathermap.org/data/2.5/weather"
params = {
'q': city,
'appid': api_key,
'units': 'metric'
}
try:
response = requests.get(base_url, params=params)
data = response.json()
if response.status_code == 200:
return data
else:
return None
except Exception as e:
return None
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
city = request.form['city']
api_key = var.key
weather_data = get_weather(api_key, city)
return render_template('index.html', weather_data=weather_data)
return render_template('index.html', weather_data=None)
if __name__ == "__main__":
#app.run(debug=True,port=8080)
app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0',debug=True)