Proposed weather UI improvement
Browse files
app.py
CHANGED
|
@@ -8,6 +8,8 @@ import requests
|
|
| 8 |
from geopy.geocoders import Nominatim
|
| 9 |
from datetime import datetime, date, timedelta
|
| 10 |
import pandas as pd
|
|
|
|
|
|
|
| 11 |
|
| 12 |
dotenv_path = os.getenv('DOTENV_PATH', None)
|
| 13 |
if dotenv_path:
|
|
@@ -204,6 +206,40 @@ def get_weather_forecast_range(location_name, start_date, end_date):
|
|
| 204 |
|
| 205 |
except Exception as e:
|
| 206 |
return {"error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
demo = gr.TabbedInterface(
|
| 208 |
[
|
| 209 |
gr.Interface(
|
|
@@ -219,13 +255,13 @@ demo = gr.TabbedInterface(
|
|
| 219 |
api_name="get direction"
|
| 220 |
),
|
| 221 |
gr.Interface(
|
| 222 |
-
fn=
|
| 223 |
inputs=[
|
| 224 |
gr.Textbox(label="Location Name", placeholder="Enter a city or place name"),
|
| 225 |
gr.Textbox(label="Start Date (YYYY-MM-DD)", value=date.today()),
|
| 226 |
gr.Textbox(label="End Date (YYYY-MM-DD)", value=date.today() + timedelta(days=6)),
|
| 227 |
],
|
| 228 |
-
outputs=gr.
|
| 229 |
api_name="get weather forecast"
|
| 230 |
)
|
| 231 |
],
|
|
|
|
| 8 |
from geopy.geocoders import Nominatim
|
| 9 |
from datetime import datetime, date, timedelta
|
| 10 |
import pandas as pd
|
| 11 |
+
import json
|
| 12 |
+
from pathlib import Path
|
| 13 |
|
| 14 |
dotenv_path = os.getenv('DOTENV_PATH', None)
|
| 15 |
if dotenv_path:
|
|
|
|
| 206 |
|
| 207 |
except Exception as e:
|
| 208 |
return {"error": str(e)}
|
| 209 |
+
|
| 210 |
+
def get_weather_forecast_range_ui(location_name, start_date, end_date):
|
| 211 |
+
result = get_weather_forecast_range(location_name, start_date, end_date)
|
| 212 |
+
|
| 213 |
+
if "error" in result:
|
| 214 |
+
return f"**⚠️ Error:** {result['error']}"
|
| 215 |
+
|
| 216 |
+
# Save JSON silently
|
| 217 |
+
save_dir = Path("logs")
|
| 218 |
+
save_dir.mkdir(exist_ok=True)
|
| 219 |
+
save_path = save_dir / f"weather_{location_name}_{start_date}_to_{end_date}.json"
|
| 220 |
+
with open(save_path, "w", encoding="utf-8") as f:
|
| 221 |
+
json.dump(result, f, indent=2)
|
| 222 |
+
|
| 223 |
+
# Generate Markdown summary
|
| 224 |
+
md = f"## Weather Forecast for {location_name}\n\n"
|
| 225 |
+
for day in result:
|
| 226 |
+
md += f"### 📅 {day['date']}\n"
|
| 227 |
+
md += f"- 🌅 Sunrise: {day['sunrise']}\n"
|
| 228 |
+
md += f"- 🌇 Sunset: {day['sunset']}\n"
|
| 229 |
+
md += f"- 🌡️ Temp: {day['temp_min']}°C – {day['temp_max']}°C\n"
|
| 230 |
+
md += f"- 🌤️ Weather: {day['weather']}\n"
|
| 231 |
+
md += f"- ☀️ UV Index Max: {day['uv_max']}\n"
|
| 232 |
+
|
| 233 |
+
if "hourly" in day:
|
| 234 |
+
md += f"**Hourly Forecast:**\n"
|
| 235 |
+
for h in day["hourly"]:
|
| 236 |
+
md += f" - {h['time']}: {h['temp']}, {h['weather']}, UV {h['uv']}, Visibility {h['visibility']}\n"
|
| 237 |
+
elif "note" in day:
|
| 238 |
+
md += f"_{day['note']}_\n"
|
| 239 |
+
md += "\n"
|
| 240 |
+
|
| 241 |
+
return md
|
| 242 |
+
|
| 243 |
demo = gr.TabbedInterface(
|
| 244 |
[
|
| 245 |
gr.Interface(
|
|
|
|
| 255 |
api_name="get direction"
|
| 256 |
),
|
| 257 |
gr.Interface(
|
| 258 |
+
fn=get_weather_forecast_range_ui,
|
| 259 |
inputs=[
|
| 260 |
gr.Textbox(label="Location Name", placeholder="Enter a city or place name"),
|
| 261 |
gr.Textbox(label="Start Date (YYYY-MM-DD)", value=date.today()),
|
| 262 |
gr.Textbox(label="End Date (YYYY-MM-DD)", value=date.today() + timedelta(days=6)),
|
| 263 |
],
|
| 264 |
+
outputs=gr.Markdown(label="Readable Forecast"),
|
| 265 |
api_name="get weather forecast"
|
| 266 |
)
|
| 267 |
],
|