Spaces:
No application file
No application file
Commit
路
bf7aaba
1
Parent(s):
30319a2
Script feito em FastAPI
Browse files
1_Building_a_Python_Weather_App_using_Docker_and_Cloud_Run/app_FastAPI.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 ---> $ uvicorn app_FastAPI:app --reload
|
15 |
+
|
16 |
+
INMET no Brasil: https://previsao.inmet.gov.br/
|
17 |
+
"""
|
18 |
+
from fastapi import FastAPI, Request, Form
|
19 |
+
from fastapi.responses import HTMLResponse
|
20 |
+
from fastapi.templating import Jinja2Templates
|
21 |
+
import requests
|
22 |
+
import var
|
23 |
+
import os
|
24 |
+
|
25 |
+
app = FastAPI(title='馃 Usando FastAPI para o Dados Meteorol贸gicos 馃',
|
26 |
+
version='1.0',
|
27 |
+
description="""Data Scientist.: Dr. Eddy Giusepe Chirinos Isidro\n
|
28 |
+
Projeto que determina os dados meteorol贸gicos de uma Cidade""")
|
29 |
+
|
30 |
+
templates = Jinja2Templates(directory="templates")
|
31 |
+
|
32 |
+
def get_weather(api_key, city):
|
33 |
+
base_url = "http://api.openweathermap.org/data/2.5/weather"
|
34 |
+
params = {
|
35 |
+
'q': city,
|
36 |
+
'appid': api_key,
|
37 |
+
'units': 'metric',
|
38 |
+
'lang': 'pt_br'
|
39 |
+
}
|
40 |
+
|
41 |
+
try:
|
42 |
+
response = requests.get(base_url, params=params)
|
43 |
+
data = response.json()
|
44 |
+
|
45 |
+
if response.status_code == 200:
|
46 |
+
return data
|
47 |
+
else:
|
48 |
+
return None
|
49 |
+
|
50 |
+
except Exception as e:
|
51 |
+
return None
|
52 |
+
|
53 |
+
@app.get("/", response_class=HTMLResponse)
|
54 |
+
@app.post("/", response_class=HTMLResponse)
|
55 |
+
async def index(request: Request, city: str = Form(None)): # "Form" 茅 usado para lidar com os dados do formul谩rio HTML.
|
56 |
+
if request.method == 'POST' and city:
|
57 |
+
api_key = var.key
|
58 |
+
weather_data = get_weather(api_key, city)
|
59 |
+
return templates.TemplateResponse("index.html", {"request": request, "weather_data": weather_data, "city": city})
|
60 |
+
else:
|
61 |
+
return templates.TemplateResponse("index.html", {"request": request, "weather_data": None, "city": None})
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
import uvicorn
|
67 |
+
uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)), debug=True)
|