Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import requests
|
|
@@ -169,14 +170,39 @@ def save_openweather_historical_to_csv():
|
|
| 169 |
df = pd.DataFrame(openweather_historical_readings)
|
| 170 |
return df
|
| 171 |
|
| 172 |
-
def
|
| 173 |
-
"""Save
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
def save_stormglass_marine_to_csv():
|
| 182 |
"""Save Stormglass marine data to CSV."""
|
|
@@ -279,20 +305,30 @@ def main():
|
|
| 279 |
).add_to(m)
|
| 280 |
folium_static(m)
|
| 281 |
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
|
| 298 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
|
|
|
| 170 |
df = pd.DataFrame(openweather_historical_readings)
|
| 171 |
return df
|
| 172 |
|
| 173 |
+
def save_openweather_historical_to_csv(openweather_historical_readings):
|
| 174 |
+
"""Save historical OpenWeather data to CSV."""
|
| 175 |
+
try:
|
| 176 |
+
# Validate input data
|
| 177 |
+
if not openweather_historical_readings:
|
| 178 |
+
raise ValueError("No historical data available.")
|
| 179 |
+
|
| 180 |
+
# Create a DataFrame from the readings
|
| 181 |
+
df = pd.DataFrame(openweather_historical_readings)
|
| 182 |
+
|
| 183 |
+
# Ensure the DataFrame is not empty
|
| 184 |
+
if df.empty:
|
| 185 |
+
raise ValueError("DataFrame is empty.")
|
| 186 |
+
|
| 187 |
+
# Create a timestamped file name
|
| 188 |
+
file_name = f"openweather_historical_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
| 189 |
+
|
| 190 |
+
# Define a safe file path to save the CSV
|
| 191 |
+
file_path = os.path.join("data", file_name)
|
| 192 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 193 |
+
|
| 194 |
+
# Save the DataFrame to a CSV file
|
| 195 |
+
df.to_csv(file_path, index=False)
|
| 196 |
+
|
| 197 |
+
print(f"Successfully saved historical data to {file_path}")
|
| 198 |
+
return file_path # Return the file path for verification
|
| 199 |
|
| 200 |
+
except ValueError as ve:
|
| 201 |
+
print(f"Validation error: {ve}")
|
| 202 |
+
return None
|
| 203 |
+
except Exception as e:
|
| 204 |
+
print(f"Error saving historical data: {str(e)}")
|
| 205 |
+
return None
|
| 206 |
|
| 207 |
def save_stormglass_marine_to_csv():
|
| 208 |
"""Save Stormglass marine data to CSV."""
|
|
|
|
| 305 |
).add_to(m)
|
| 306 |
folium_static(m)
|
| 307 |
|
| 308 |
+
|
| 309 |
+
# Download button for historical data
|
| 310 |
+
if st.button("Download Historical Weather Data"):
|
| 311 |
+
file_path = save_openweather_historical_to_csv(openweather_historical_readings)
|
| 312 |
+
|
| 313 |
+
if file_path:
|
| 314 |
+
try:
|
| 315 |
+
# Read the saved CSV file
|
| 316 |
+
with open(file_path, 'rb') as file:
|
| 317 |
+
csv_data = file.read()
|
| 318 |
+
|
| 319 |
+
# Create a download button
|
| 320 |
+
st.download_button(
|
| 321 |
+
label="Download Historical Weather Data CSV",
|
| 322 |
+
data=csv_data,
|
| 323 |
+
file_name=os.path.basename(file_path),
|
| 324 |
+
mime="text/csv",
|
| 325 |
+
)
|
| 326 |
+
st.success("Historical weather data saved and ready for download!")
|
| 327 |
+
|
| 328 |
+
except Exception as e:
|
| 329 |
+
st.error(f"Error preparing download: {str(e)}")
|
| 330 |
+
else:
|
| 331 |
+
st.error("No historical weather data available.")
|
| 332 |
|
| 333 |
|
| 334 |
|