Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,67 +1,107 @@
|
|
| 1 |
-
|
| 2 |
-
import pandas as pd
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
from webdriver_manager.chrome import ChromeDriverManager
|
| 9 |
-
|
| 10 |
-
def scrape_snotel_data():
|
| 11 |
-
url = "https://www.weather.gov/wrh/timeseries?site=YCTIM"
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
rows = table.find_elements(By.TAG_NAME, "tr")
|
| 37 |
-
for row in rows[1:]: # Skip header row
|
| 38 |
-
cols = [td.text.strip() for td in row.find_elements(By.TAG_NAME, "td")]
|
| 39 |
-
if cols:
|
| 40 |
-
data.append(cols)
|
| 41 |
-
|
| 42 |
-
# Convert to DataFrame
|
| 43 |
-
df = pd.DataFrame(data, columns=headers)
|
| 44 |
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Gradio interface
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
inputs=None,
|
| 61 |
-
outputs="dataframe",
|
| 62 |
-
title="SNOTEL Data Viewer",
|
| 63 |
-
description="Scrapes and displays the latest SNOTEL data for YCTIM."
|
| 64 |
-
)
|
| 65 |
|
| 66 |
-
if __name__ == "__main__":
|
| 67 |
-
iface.launch()
|
|
|
|
| 1 |
+
# app.py
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
import plotly.express as px
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
def get_weather_data(station_id, hours=72):
|
| 9 |
+
"""
|
| 10 |
+
Fetch weather data from NWS API
|
| 11 |
+
"""
|
| 12 |
+
# NWS API requires a user agent header
|
| 13 |
+
headers = {
|
| 14 |
+
'User-Agent': '(Weather Data Fetcher, contact@yourdomain.com)',
|
| 15 |
+
'Accept': 'application/json'
|
| 16 |
+
}
|
| 17 |
|
| 18 |
+
# First, get the station details
|
| 19 |
+
station_url = f'https://api.weather.gov/stations/{station_id}'
|
|
|
|
| 20 |
try:
|
| 21 |
+
response = requests.get(station_url, headers=headers)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
station_data = response.json()
|
| 24 |
+
|
| 25 |
+
# Get observations
|
| 26 |
+
observations_url = f'https://api.weather.gov/stations/{station_id}/observations'
|
| 27 |
+
params = {
|
| 28 |
+
'limit': hours,
|
| 29 |
+
'start': (datetime.utcnow() - timedelta(hours=hours)).isoformat() + 'Z'
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
response = requests.get(observations_url, headers=headers, params=params)
|
| 33 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
data = response.json()
|
| 36 |
+
|
| 37 |
+
# Extract relevant information
|
| 38 |
+
records = []
|
| 39 |
+
for obs in data['features']:
|
| 40 |
+
props = obs['properties']
|
| 41 |
+
record = {
|
| 42 |
+
'timestamp': props['timestamp'],
|
| 43 |
+
'temperature': props['temperature']['value'],
|
| 44 |
+
'relative_humidity': props['relativeHumidity']['value'],
|
| 45 |
+
'wind_speed': props['windSpeed']['value'],
|
| 46 |
+
'wind_direction': props['windDirection']['value'],
|
| 47 |
+
'barometric_pressure': props['barometricPressure']['value']
|
| 48 |
+
}
|
| 49 |
+
records.append(record)
|
| 50 |
+
|
| 51 |
+
df = pd.DataFrame(records)
|
| 52 |
+
df['timestamp'] = pd.to_datetime(df['timestamp'])
|
| 53 |
+
|
| 54 |
+
# Convert temperature from Celsius to Fahrenheit
|
| 55 |
+
df['temperature'] = (df['temperature'] * 9/5) + 32
|
| 56 |
+
|
| 57 |
+
return df, None
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
return None, f"Error fetching data: {str(e)}"
|
| 61 |
+
|
| 62 |
+
def create_plot(df):
|
| 63 |
+
"""
|
| 64 |
+
Create an interactive plot using plotly
|
| 65 |
+
"""
|
| 66 |
+
fig = px.line(df, x='timestamp', y='temperature',
|
| 67 |
+
title='Temperature Over Time',
|
| 68 |
+
labels={'timestamp': 'Time', 'temperature': 'Temperature (°F)'})
|
| 69 |
+
return fig
|
| 70 |
|
| 71 |
+
def fetch_and_display(station_id, hours):
|
| 72 |
+
"""
|
| 73 |
+
Main function to fetch data and create visualization
|
| 74 |
+
"""
|
| 75 |
+
df, error = get_weather_data(station_id, hours)
|
| 76 |
|
| 77 |
+
if error:
|
| 78 |
+
return None, error
|
| 79 |
+
|
| 80 |
+
if df is not None and not df.empty:
|
| 81 |
+
fig = create_plot(df)
|
| 82 |
+
return fig, "Data fetched successfully!"
|
| 83 |
+
|
| 84 |
+
return None, "No data available for the specified parameters."
|
| 85 |
|
| 86 |
+
# Create Gradio interface
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("# Weather Data Viewer")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
station_id = gr.Textbox(label="Station ID", value="YCTIM")
|
| 92 |
+
hours = gr.Slider(minimum=1, maximum=168, value=72,
|
| 93 |
+
label="Hours of Data", step=1)
|
| 94 |
+
|
| 95 |
+
fetch_btn = gr.Button("Fetch Data")
|
| 96 |
+
plot = gr.Plot()
|
| 97 |
+
message = gr.Textbox(label="Status")
|
| 98 |
+
|
| 99 |
+
fetch_btn.click(
|
| 100 |
+
fn=fetch_and_display,
|
| 101 |
+
inputs=[station_id, hours],
|
| 102 |
+
outputs=[plot, message]
|
| 103 |
+
)
|
| 104 |
|
| 105 |
+
# Launch the app
|
| 106 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
|
|
|
|
|