Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,61 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_folium import st_folium
|
| 3 |
import folium
|
| 4 |
-
import requests
|
| 5 |
from datetime import datetime
|
| 6 |
-
import cdsapi # To connect with ECMWF's Copernicus Data Service
|
| 7 |
|
| 8 |
# Streamlit App
|
| 9 |
st.title("Weather Explorer 🌍☀️")
|
| 10 |
|
| 11 |
st.markdown("Search and click a location on the map to get current and historical temperature data.")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
# Map setup
|
|
|
|
| 14 |
m = folium.Map(location=[20, 0], zoom_start=2)
|
| 15 |
map_data = st_folium(m, width=700, height=500)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
if map_data and map_data.get("last_clicked"):
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
st.
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
st.
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from streamlit_folium import st_folium
|
| 3 |
import folium
|
|
|
|
| 4 |
from datetime import datetime
|
|
|
|
| 5 |
|
| 6 |
# Streamlit App
|
| 7 |
st.title("Weather Explorer 🌍☀️")
|
| 8 |
|
| 9 |
st.markdown("Search and click a location on the map to get current and historical temperature data.")
|
| 10 |
|
| 11 |
+
# Date input (defaults to today)
|
| 12 |
+
default_date = datetime.utcnow().date()
|
| 13 |
+
date_input = st.text_input("Enter Date (YYYY-MM-DD)", value=str(default_date))
|
| 14 |
+
|
| 15 |
+
# Latitude and Longitude Inputs (initialized empty)
|
| 16 |
+
lat_input = st.text_input("Latitude", value="")
|
| 17 |
+
lon_input = st.text_input("Longitude", value="")
|
| 18 |
+
|
| 19 |
# Map setup
|
| 20 |
+
st.subheader("Select a location on the map:")
|
| 21 |
m = folium.Map(location=[20, 0], zoom_start=2)
|
| 22 |
map_data = st_folium(m, width=700, height=500)
|
| 23 |
|
| 24 |
+
# If user clicks on the map, populate the lat/lon fields
|
| 25 |
if map_data and map_data.get("last_clicked"):
|
| 26 |
+
clicked_lat = map_data["last_clicked"]["lat"]
|
| 27 |
+
clicked_lon = map_data["last_clicked"]["lng"]
|
| 28 |
+
st.session_state['clicked_lat'] = clicked_lat
|
| 29 |
+
st.session_state['clicked_lon'] = clicked_lon
|
| 30 |
+
|
| 31 |
+
# Pre-fill lat/lon if available
|
| 32 |
+
if 'clicked_lat' in st.session_state and 'clicked_lon' in st.session_state:
|
| 33 |
+
lat_input = st.text_input("Latitude", value=str(st.session_state['clicked_lat']))
|
| 34 |
+
lon_input = st.text_input("Longitude", value=str(st.session_state['clicked_lon']))
|
| 35 |
+
|
| 36 |
+
# 'Enter' Button
|
| 37 |
+
if st.button("Enter"):
|
| 38 |
+
if lat_input and lon_input and date_input:
|
| 39 |
+
try:
|
| 40 |
+
# Convert input to correct types
|
| 41 |
+
lat = float(lat_input)
|
| 42 |
+
lon = float(lon_input)
|
| 43 |
+
selected_date = datetime.strptime(date_input, "%Y-%m-%d").date()
|
| 44 |
+
|
| 45 |
+
st.success(f"Fetching weather data for {lat:.4f}, {lon:.4f} on {selected_date}")
|
| 46 |
+
|
| 47 |
+
# --------- Replace below with actual data fetching ----------
|
| 48 |
+
# Example placeholder (replace with real ECMWF or API call)
|
| 49 |
+
current_temp = 25 # Placeholder: current temperature
|
| 50 |
+
historical_min_temp = 15 # Placeholder: historical minimum
|
| 51 |
+
historical_max_temp = 35 # Placeholder: historical maximum
|
| 52 |
+
|
| 53 |
+
st.metric("Current Temperature (°C)", f"{current_temp}°C")
|
| 54 |
+
st.metric(f"Historical Min (°C) on {selected_date.strftime('%B %d')}", f"{historical_min_temp}°C")
|
| 55 |
+
st.metric(f"Historical Max (°C) on {selected_date.strftime('%B %d')}", f"{historical_max_temp}°C")
|
| 56 |
+
|
| 57 |
+
except ValueError:
|
| 58 |
+
st.error("Please enter valid latitude, longitude, and date values.")
|
| 59 |
+
|
| 60 |
+
else:
|
| 61 |
+
st.error("Please provide all inputs (date, latitude, longitude).")
|