Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import folium
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# ===========================
|
| 6 |
+
# Configuración
|
| 7 |
+
# ===========================
|
| 8 |
+
# Coordenadas de ciudades en EE.UU.
|
| 9 |
+
ciudades = {
|
| 10 |
+
'Nueva York': [40.7128, -74.0060],
|
| 11 |
+
'Los Ángeles': [34.0522, -118.2437],
|
| 12 |
+
'Chicago': [41.8781, -87.6298],
|
| 13 |
+
'Houston': [29.7604, -95.3698],
|
| 14 |
+
'Miami': [25.7617, -80.1918],
|
| 15 |
+
'Seattle': [47.6062, -122.3321],
|
| 16 |
+
'Denver': [39.7392, -104.9903],
|
| 17 |
+
'Boston': [42.3601, -71.0589],
|
| 18 |
+
'Atlanta': [33.7490, -84.3880],
|
| 19 |
+
'San Francisco': [37.7749, -122.4194]
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# API abierta: Open-Meteo (no necesitas token)
|
| 23 |
+
API_URL = "https://api.open-meteo.com/v1/forecast"
|
| 24 |
+
|
| 25 |
+
# ===========================
|
| 26 |
+
# Función para obtener datos climáticos en tiempo real
|
| 27 |
+
# ===========================
|
| 28 |
+
def obtener_datos_climaticos(lat, lon, variables):
|
| 29 |
+
params = {
|
| 30 |
+
"latitude": lat,
|
| 31 |
+
"longitude": lon,
|
| 32 |
+
"current_weather": True
|
| 33 |
+
}
|
| 34 |
+
response = requests.get(API_URL, params=params)
|
| 35 |
+
if response.status_code == 200:
|
| 36 |
+
data = response.json()["current_weather"]
|
| 37 |
+
# Filtramos solo las variables seleccionadas
|
| 38 |
+
resultado = {var: data.get(var, 'N/A') for var in variables}
|
| 39 |
+
resultado['temperature'] = data.get('temperature', 'N/A')
|
| 40 |
+
resultado['windspeed'] = data.get('windspeed', 'N/A')
|
| 41 |
+
resultado['weathercode'] = data.get('weathercode', 'N/A')
|
| 42 |
+
return resultado
|
| 43 |
+
else:
|
| 44 |
+
return None
|
| 45 |
+
|
| 46 |
+
# ===========================
|
| 47 |
+
# Crear Mapa Interactivo
|
| 48 |
+
# ===========================
|
| 49 |
+
def crear_mapa(ciudades_seleccionadas, variable_minima, filtro_variable):
|
| 50 |
+
mapa = folium.Map(location=[39.8283, -98.5795], zoom_start=4, tiles='CartoDB positron')
|
| 51 |
+
variables_a_consultar = ["temperature", "windspeed", "weathercode"]
|
| 52 |
+
|
| 53 |
+
for ciudad in ciudades_seleccionadas:
|
| 54 |
+
lat, lon = ciudades[ciudad]
|
| 55 |
+
datos = obtener_datos_climaticos(lat, lon, variables_a_consultar)
|
| 56 |
+
if datos:
|
| 57 |
+
valor_filtro = datos.get(filtro_variable, 0)
|
| 58 |
+
if valor_filtro != 'N/A' and float(valor_filtro) >= variable_minima:
|
| 59 |
+
popup_text = (
|
| 60 |
+
f"<b>{ciudad}</b><br>"
|
| 61 |
+
f"🌡️ Temp: {datos['temperature']}°C<br>"
|
| 62 |
+
f"💨 Viento: {datos['windspeed']} km/h<br>"
|
| 63 |
+
f"⛅ Código Clima: {datos['weathercode']}"
|
| 64 |
+
)
|
| 65 |
+
folium.CircleMarker(
|
| 66 |
+
location=[lat, lon],
|
| 67 |
+
radius=8,
|
| 68 |
+
fill=True,
|
| 69 |
+
color='blue',
|
| 70 |
+
fill_color='cyan',
|
| 71 |
+
fill_opacity=0.7,
|
| 72 |
+
popup=popup_text
|
| 73 |
+
).add_to(mapa)
|
| 74 |
+
|
| 75 |
+
return mapa._repr_html_()
|
| 76 |
+
|
| 77 |
+
# ===========================
|
| 78 |
+
# Interfaz Gradio
|
| 79 |
+
# ===========================
|
| 80 |
+
gr.Interface(
|
| 81 |
+
fn=crear_mapa,
|
| 82 |
+
inputs=[
|
| 83 |
+
gr.CheckboxGroup(choices=list(ciudades.keys()), label="Selecciona ciudades", value=["Nueva York", "Los Ángeles"]),
|
| 84 |
+
gr.Slider(0, 50, step=1, value=10, label="Umbral mínimo para filtrar"),
|
| 85 |
+
gr.Dropdown(choices=["temperature", "windspeed"], value="temperature", label="Variable para filtrar")
|
| 86 |
+
],
|
| 87 |
+
outputs=gr.HTML(label="🌎 Mapa Interactivo"),
|
| 88 |
+
title="🌦️ Mapa Clima USA en Tiempo Real",
|
| 89 |
+
description="Visualiza el clima actual de ciudades de EE.UU. Filtra por temperatura o viento.",
|
| 90 |
+
theme="soft",
|
| 91 |
+
live=True
|
| 92 |
+
).launch()
|