Spaces:
No application file
No application file
Commit
路
30319a2
1
Parent(s):
c2cda06
Script feito em Flask
Browse files
1_Building_a_Python_Weather_App_using_Docker_and_Cloud_Run/app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro
|
4 |
+
|
5 |
+
Building a Python Weather App using Docker and Cloud Run
|
6 |
+
=========================================================
|
7 |
+
Aqui construimos um App Meteorol贸gico com Python e usando a
|
8 |
+
API de "OpenWeather". OpenWeather fornece dados meteorol贸gicos
|
9 |
+
hist贸ricos, atuais e previstos por meio de APIs de velocidade
|
10 |
+
da luz. Sede em Londres, Reino Unido.
|
11 |
+
|
12 |
+
Este projeto est谩 baseado no maravilhoso tutorial de "TechTrapture".
|
13 |
+
|
14 |
+
Executando o Script ---> $ python app.py
|
15 |
+
"""
|
16 |
+
from flask import Flask, render_template, request
|
17 |
+
import requests
|
18 |
+
import var # Arquivo Python onde est谩 sua vari谩vel de Ambiente.
|
19 |
+
import os
|
20 |
+
|
21 |
+
app = Flask(__name__)
|
22 |
+
|
23 |
+
def get_weather(api_key, city):
|
24 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather" # https://openweathermap.org/current
|
25 |
+
params = {
|
26 |
+
'q': city,
|
27 |
+
'appid': api_key,
|
28 |
+
'units': 'metric',
|
29 |
+
'lang': 'pt_br'
|
30 |
+
}
|
31 |
+
|
32 |
+
try:
|
33 |
+
response = requests.get(base_url, params=params)
|
34 |
+
data = response.json()
|
35 |
+
|
36 |
+
if response.status_code == 200:
|
37 |
+
return data
|
38 |
+
else:
|
39 |
+
return None
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return None
|
43 |
+
|
44 |
+
@app.route('/', methods=['GET', 'POST'])
|
45 |
+
def index():
|
46 |
+
if request.method == 'POST':
|
47 |
+
city = request.form['city']
|
48 |
+
api_key = var.key
|
49 |
+
weather_data = get_weather(api_key, city)
|
50 |
+
return render_template('index.html', weather_data=weather_data)
|
51 |
+
|
52 |
+
return render_template('index.html', weather_data=None)
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
#app.run(debug=True,port=8080)
|
56 |
+
app.run(port=int(os.environ.get("PORT", 8080)),host='0.0.0.0', debug=True)
|
57 |
+
|