aromidvar1355 commited on
Commit
41598ea
·
verified ·
1 Parent(s): 10fdea6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -47
app.py CHANGED
@@ -96,67 +96,55 @@ def fetch_solunar_tide_data(lat, lon, datetime_input):
96
 
97
  # --- MAIN APPLICATION LOGIC ---
98
  def main():
99
- if data_source == "Weather Data":
100
- st.title("OpenWeather Data")
101
-
102
- col1, col2 = st.columns(2)
103
-
104
- with col1:
105
- st.subheader("Location and Time Settings")
106
-
107
- # Latitude and Longitude inputs
108
- lat = st.number_input("Latitude", value=25.7617, step=0.001, format="%.4f")
109
- lon = st.number_input("Longitude", value=-80.1918, step=0.001, format="%.4f")
110
 
 
111
 
112
- # Manage session state for date and time inputs
113
- if 'datetime_input' not in st.session_state:
114
- # Initialize with current date and time if not already present
115
- st.session_state['datetime_input'] = datetime.now()
116
 
117
- # Split the stored datetime into date and time components
118
- datetime_input = st.session_state['datetime_input']
119
- input_date = datetime_input.date() # Use the date part
120
- input_time = datetime_input.time() # Use the time part
121
-
122
- # Date and Time inputs
123
- input_date = st.date_input("Date", value=input)
124
- input_time = st.time_input("Time", value=input_time)
125
 
 
 
 
 
126
 
127
- # Combine date and time
128
- datetime_input = datetime.combine(input_date, input_time)
129
- st.session_state["datetime_input"] = datetime_input # Store the datetime in the session state
 
130
 
 
 
 
131
 
132
- # Map to show selected location
133
- map_data = pd.DataFrame({
134
- 'lat': [lat],
135
- 'lon': [lon]
136
- })
137
- st.map(map_data)
138
 
 
 
 
139
 
140
- if st.button("Get Current Weather"):
141
- with st.spinner("Fetching data..."):
142
- data = fetch_openweather_data(lat, lon, datetime_input)
 
143
 
144
- if data and 'data' in data:
145
- current_weather_data = data['data']
146
- if current_weather_data:
147
- current = current_weather_data[0]
148
 
149
- # Convert and format sunrise and sunset
150
- timezone = data.get('timezone', 'UTC')
151
- tz = pytz.timezone(timezone)
152
 
153
- sunrise_time = datetime.fromtimestamp(current['sunrise'], tz).strftime('%Y-%m-%d %H:%M:%S')
154
- sunset_time = datetime.fromtimestamp(current['sunset'], tz).strftime('%Y-%m-%d %H:%M:%S')
155
 
156
- st.subheader(f"Weather at {lat}, {lon} on {datetime_input}")
157
 
158
- weather_col1, weather_col2 = st.columns(2)
159
-
160
 
161
  with weather_col1:
162
  st.metric("Temperature", f"{current['temp']}°C")
 
96
 
97
  # --- MAIN APPLICATION LOGIC ---
98
  def main():
99
+ st.title("OpenWeather Data")
 
 
 
 
 
 
 
 
 
 
100
 
101
+ col1, col2 = st.columns(2)
102
 
103
+ with col1:
104
+ st.subheader("Location and Time Settings")
 
 
105
 
106
+ # Latitude and Longitude inputs
107
+ lat = st.number_input("Latitude", value=25.7617, step=0.001, format="%.4f")
108
+ lon = st.number_input("Longitude", value=-80.1918, step=0.001, format="%.4f")
 
 
 
 
 
109
 
110
+ # Manage session state for date and time inputs
111
+ if 'datetime_input' not in st.session_state:
112
+ # Initialize with current date and time if not already present
113
+ st.session_state['datetime_input'] = datetime.now()
114
 
115
+ # Split the stored datetime into date and time components
116
+ datetime_input = st.session_state['datetime_input']
117
+ input_date = datetime_input.date() # Use the date part
118
+ input_time = datetime_input.time() # Use the time part
119
 
120
+ # Allow user to set new date and time
121
+ input_date = st.date_input("Date", value=input_date)
122
+ input_time = st.time_input("Time", value=input_time)
123
 
124
+ # Combine date and time into a datetime object
125
+ datetime_input = datetime.combine(input_date, input_time)
126
+ st.session_state['datetime_input'] = datetime_input # Store the datetime in the session state
 
 
 
127
 
128
+ if st.button("Get Current Weather"):
129
+ with st.spinner("Fetching data..."):
130
+ data = fetch_openweather_data(lat, lon, datetime_input)
131
 
132
+ if data and 'data' in data:
133
+ current_weather_data = data['data']
134
+ if current_weather_data:
135
+ current = current_weather_data[0]
136
 
137
+ # Convert and format sunrise and sunset
138
+ timezone = data.get('timezone', 'UTC')
139
+ tz = pytz.timezone(timezone)
 
140
 
141
+ sunrise_time = datetime.fromtimestamp(current['sunrise'], tz).strftime('%Y-%m-%d %H:%M:%S')
142
+ sunset_time = datetime.fromtimestamp(current['sunset'], tz).strftime('%Y-%m-%d %H:%M:%S')
 
143
 
144
+ st.subheader(f"Weather at {lat}, {lon} on {datetime_input}")
 
145
 
146
+ weather_col1, weather_col2 = st.columns(2)
147
 
 
 
148
 
149
  with weather_col1:
150
  st.metric("Temperature", f"{current['temp']}°C")