-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
56 lines (49 loc) · 1.86 KB
/
Copy pathapp.py
File metadata and controls
56 lines (49 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import Flask, request, render_template_string
import requests
app = Flask(__name__)
class WeatherData:
def __init__(self, city, temperature, humidity, description):
self.city = city
self.temperature = temperature
self.humidity = humidity
self.description = description
def display_info(self):
return (f"City: {self.city}<br>"
f"Temperature: {self.temperature}°C<br>"
f"Humidity: {self.humidity}%<br>"
f"Weather: {self.description}")
def fetch_weather_data(city):
api_key = "RActivate API Key with Open Weather Map Services and insert here"
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(url)
data = response.json()
weather = WeatherData(
city=data['name'],
temperature=data['main']['temp'],
humidity=data['main']['humidity'],
description=data['weather'][0]['description']
)
return weather
@app.route('/', methods=['GET', 'POST'])
def index():
city_name = request.form.get('city', 'Cape Town') if request.method == 'POST' else 'Cape Town'
weather_info = fetch_weather_data(city_name)
return render_template_string(f"""
<html>
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<h1>Weather Information</h1>
<form method="post">
<input type="text" name="city" placeholder="Enter city name">
<button type="submit">Get Weather</button>
</form>
<div class="weather-info">
<p>{weather_info.display_info()}</p>
</div>
</body>
</html>
""")
if __name__ == "__main__":
app.run(debug=True)