aromidvar1355 commited on
Commit
fcc71ef
·
verified ·
1 Parent(s): da7510c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -26
app.py CHANGED
@@ -166,41 +166,35 @@ def fetch_solunar_tide_data(lat, lon, datetime_input):
166
  st.error(f"Error fetching Solunar and Tide data: {e}")
167
  return None
168
 
169
-
170
-
171
  def save_openweather_historical_to_csv(openweather_historical_readings):
172
- """Save historical OpenWeather data to CSV."""
173
  try:
174
  # Validate input data
175
  if not openweather_historical_readings:
176
  raise ValueError("No historical data available.")
177
 
178
  # Create a DataFrame from the readings
179
- df = pd.DataFrame(openweather_historical_readings)
180
 
181
  # Ensure the DataFrame is not empty
182
  if df.empty:
183
  raise ValueError("DataFrame is empty.")
184
 
185
- # Create a timestamped file name
186
- file_name = f"openweather_historical_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
187
-
188
- # Define a safe file path to save the CSV
189
- file_path = os.path.join("data", file_name)
190
- os.makedirs(os.path.dirname(file_path), exist_ok=True)
191
-
192
- # Save the DataFrame to a CSV file
193
- df.to_csv(file_path, index=False)
194
 
195
- print(f"Successfully saved historical data to {file_path}")
196
- return file_path # Return the file path for verification
 
197
 
198
  except ValueError as ve:
199
  print(f"Validation error: {ve}")
200
  return None
201
  except Exception as e:
202
  print(f"Error saving historical data: {str(e)}")
203
- return None
 
204
 
205
  def save_stormglass_marine_to_csv():
206
  """Save Stormglass marine data to CSV."""
@@ -306,27 +300,22 @@ def main():
306
 
307
  # Download button for historical data
308
  if st.button("Download Historical Weather Data"):
309
- file_path = save_openweather_historical_to_csv(openweather_historical_readings)
310
 
311
- if file_path:
312
  try:
313
- # Read the saved CSV file
314
- with open(file_path, 'rb') as file:
315
- csv_data = file.read()
316
-
317
- # Create a download button
318
  st.download_button(
319
  label="Download Historical Weather Data CSV",
320
  data=csv_data,
321
- file_name=os.path.basename(file_path),
322
  mime="text/csv",
323
  )
324
  st.success("Historical weather data saved and ready for download!")
325
-
326
  except Exception as e:
327
  st.error(f"Error preparing download: {str(e)}")
328
  else:
329
- st.error("No historical weather data available.")
330
 
331
 
332
 
 
166
  st.error(f"Error fetching Solunar and Tide data: {e}")
167
  return None
168
 
 
 
169
  def save_openweather_historical_to_csv(openweather_historical_readings):
170
+ """Save historical OpenWeather data to CSV for download."""
171
  try:
172
  # Validate input data
173
  if not openweather_historical_readings:
174
  raise ValueError("No historical data available.")
175
 
176
  # Create a DataFrame from the readings
177
+ df = pd.DataFrame([openweather_historical_readings]) # Assuming a single row of data
178
 
179
  # Ensure the DataFrame is not empty
180
  if df.empty:
181
  raise ValueError("DataFrame is empty.")
182
 
183
+ # Create a CSV buffer in memory
184
+ csv_buffer = io.StringIO()
185
+ df.to_csv(csv_buffer, index=False)
 
 
 
 
 
 
186
 
187
+ # Return the CSV data as a string
188
+ csv_data = csv_buffer.getvalue()
189
+ return csv_data
190
 
191
  except ValueError as ve:
192
  print(f"Validation error: {ve}")
193
  return None
194
  except Exception as e:
195
  print(f"Error saving historical data: {str(e)}")
196
+ return None
197
+
198
 
199
  def save_stormglass_marine_to_csv():
200
  """Save Stormglass marine data to CSV."""
 
300
 
301
  # Download button for historical data
302
  if st.button("Download Historical Weather Data"):
303
+ csv_data = save_openweather_historical_to_csv(openweather_historical_readings)
304
 
305
+ if csv_data:
306
  try:
307
+ # Create a download button
 
 
 
 
308
  st.download_button(
309
  label="Download Historical Weather Data CSV",
310
  data=csv_data,
311
+ file_name=f"openweather_historical_data_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.csv",
312
  mime="text/csv",
313
  )
314
  st.success("Historical weather data saved and ready for download!")
 
315
  except Exception as e:
316
  st.error(f"Error preparing download: {str(e)}")
317
  else:
318
+ st.error("No historical weather data available.")
319
 
320
 
321