nakas commited on
Commit
482e825
·
verified ·
1 Parent(s): 1e79b2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -57
app.py CHANGED
@@ -1,67 +1,107 @@
1
- import time
2
- import pandas as pd
3
  import gradio as gr
4
- from selenium import webdriver
5
- from selenium.webdriver.chrome.service import Service
6
- from selenium.webdriver.common.by import By
7
- from selenium.webdriver.chrome.options import Options
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
- # Set up Selenium with headless Chrome
14
- options = Options()
15
- options.add_argument("--headless")
16
- options.add_argument("--disable-gpu")
17
- options.add_argument("--no-sandbox")
 
 
 
 
18
 
19
- service = Service(ChromeDriverManager().install())
20
- driver = webdriver.Chrome(service=service, options=options)
21
-
22
  try:
23
- driver.get(url)
24
- time.sleep(5) # Allow JavaScript to load (adjust time if necessary)
25
-
26
- # Locate table
27
- table = driver.find_element(By.TAG_NAME, "table")
28
- if not table:
29
- return pd.DataFrame([["Error", "No data table found on the page"]], columns=["Status", "Message"])
30
-
31
- # Extract table headers
32
- headers = [th.text.strip() for th in table.find_elements(By.TAG_NAME, "th")]
33
-
34
- # Extract table rows
35
- data = []
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
- return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
- except Exception as e:
48
- return pd.DataFrame([["Error", str(e)]], columns=["Status", "Message"])
 
 
 
49
 
50
- finally:
51
- driver.quit()
 
 
 
 
 
 
52
 
53
- # Gradio interface
54
- def display_data():
55
- df = scrape_snotel_data()
56
- return df
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- iface = gr.Interface(
59
- fn=display_data,
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