Skip to content

Commit dcc0c2d

Browse files
Added a Docker file and UI
1 parent da22fa5 commit dcc0c2d

File tree

4 files changed

+52
-28
lines changed

4 files changed

+52
-28
lines changed

Weather Alert/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ COPY . .
1010
# Install Libraries
1111
RUN pip install -r requirement.txt
1212

13-
# run app
13+
# Run app
1414
CMD ["python", "main.py"]
1515

Weather Alert/main.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
from flask import Flask, render_template, request
12
import requests
2-
import json
3-
import time
43
import os
4+
import logging
55
from dotenv import load_dotenv
66

7+
app = Flask(__name__)
8+
79
# Load environment variables from .env file
810
load_dotenv()
911

@@ -16,54 +18,50 @@
1618
UNIT = 'metric'
1719
BASE_URL = 'https://api.openweathermap.org/data/2.5/find'
1820

19-
# Load additional environment variables
20-
CITY = os.getenv('CITY', 'London') # Default to 'London' if not provided
21-
TEMP_THRESHOLD = float(os.getenv('TEMP_THRESHOLD', 30.0)) # Default to 30°C
22-
WIND_SPEED_THRESHOLD = float(os.getenv('WIND_SPEED_THRESHOLD', 10.0)) # Default to 10 m/s
21+
# Set up basic logging
22+
logging.basicConfig(level=logging.INFO)
2323

24+
@app.route('/', methods=['GET', 'POST'])
25+
def index():
26+
alerts = []
27+
if request.method == 'POST':
28+
city = request.form.get('city', '').strip()
29+
if city:
30+
weather_data = fetch_weather(city)
31+
alerts = check_alerts(weather_data)
32+
return render_template('index.html', alerts=alerts)
2433

2534
def fetch_weather(city):
2635
"""Fetches weather data for a given city."""
2736
url = f"{BASE_URL}?q={city}&appid={API_KEY}&units={UNIT}"
37+
logging.info(f"Fetching weather for {city}: {url}") # Log the URL being called
2838
response = requests.get(url)
2939
if response.status_code == 200:
3040
return response.json()
3141
else:
32-
print(f"Error fetching data: {response.status_code}")
42+
logging.error(f"Error fetching data: {response.status_code}, {response.text}") # Log the error response
3343
return None
3444

35-
36-
def check_alerts(data, temp_threshold, wind_speed_threshold):
45+
def check_alerts(data):
3746
"""Checks for temperature and wind speed alerts in weather data."""
3847
if not data or 'list' not in data or not data['list']:
39-
print("No data available to check alerts.")
40-
return
48+
return ["No data available to check alerts."]
4149

4250
weather_info = data['list'][0]
4351
temp = weather_info['main']['temp']
4452
wind_speed = weather_info['wind']['speed']
4553

4654
alerts = []
55+
temp_threshold = float(os.getenv('TEMP_THRESHOLD', 30.0)) # Default to 30°C
56+
wind_speed_threshold = float(os.getenv('WIND_SPEED_THRESHOLD', 10.0)) # Default to 10 m/s
57+
4758
if temp > temp_threshold:
4859
alerts.append(f"Temperature alert! Current temperature: {temp}°C")
4960
if wind_speed > wind_speed_threshold:
5061
alerts.append(f"Wind speed alert! Current wind speed: {wind_speed} m/s")
5162

52-
if alerts:
53-
print("\n".join(alerts))
54-
else:
55-
print("No alerts. Weather conditions are normal.")
56-
57-
58-
def main():
59-
"""Continuously checks for weather alerts based on environment variables."""
60-
while True:
61-
weather_data = fetch_weather(CITY)
62-
check_alerts(weather_data, TEMP_THRESHOLD, WIND_SPEED_THRESHOLD)
63-
print("Waiting for the next check...")
64-
time.sleep(3600) # check every hour
65-
63+
return alerts if alerts else ["No alerts. Weather conditions are normal."]
6664

6765
if __name__ == "__main__":
68-
main()
66+
app.run(host='0.0.0.0', port=80, debug=True)
6967

Weather Alert/requirement.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
Flask
12
requests
2-
python-dotenv
3+
python-dotenv
4+
v

Weather Alert/templates/index.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Weather Alerts</title>
7+
</head>
8+
<body>
9+
<h1>Weather Alerts</h1>
10+
<form method="post">
11+
<label for="city">Enter City Name:</label>
12+
<input type="text" id="city" name="city" required>
13+
<button type="submit">Check Weather</button>
14+
</form>
15+
16+
<h2>Alerts:</h2>
17+
<ul>
18+
{% for alert in alerts %}
19+
<li>{{ alert }}</li>
20+
{% endfor %}
21+
</ul>
22+
</body>
23+
</html>
24+

0 commit comments

Comments
 (0)