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