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).")