-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeteo_app.py
64 lines (44 loc) · 1.78 KB
/
meteo_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
from flask import Flask, render_template,request, flash, jsonify, url_for
import requests
import os
app = Flask(__name__)
with open('weather_id.txt','r') as file_id:
weather_id = str(file_id.read())
@app.route('/')
def index():
return render_template("weather_template.html")
@app.route('/weather_today',methods=['POST'])
def get_weather_today():
city_input = request.form['city_input']
if request.method == 'POST':
city = city_input.capitalize()
url = '''http://api.openweathermap.org/data/2.5/weather?q='''+ city + '''&APPID=''' + weather_id + '''&units=metric'''
res = requests.get(url)
#dati in formato json
data = res.json()
data_code = str(data['cod'])
if data_code == '404':
city = "The city \t "+ city + '\t not found'
return jsonify({ 'response' : data_code,
'city': city })
else:
return jsonify({
'response' : data_code,
'condition_code': int(data['weather'][0]['id']),
'weather_description' : data['weather'][0]['description'],
'weather_temp_min' : round(data['main']['temp_min'],1),
'weather_temp_max' : round(data['main']['temp_max'],1),
'city': city })
#updates css when changed
@app.context_processor
def override_url_for():
return dict(url_for=dated_url_for)
def dated_url_for(endpoint, **values):
if endpoint == 'static':
filename = values.get('filename', None)
if filename:
file_path = os.path.join(app.root_path,endpoint, filename)
values['q'] = int(os.stat(file_path).st_mtime)
return url_for(endpoint, **values)
if __name__ == '__main__':
app.run(debug=True)