Spaces:
Runtime error
Runtime error
Commit
·
29c745c
1
Parent(s):
b37d56c
Update app.py
Browse files
app.py
CHANGED
@@ -4,21 +4,22 @@ import json
|
|
4 |
# Replace YOUR_API_KEY_HERE with your actual API key from OpenWeatherMap
|
5 |
api_key = "1aafc3163909c1493596da9340e00aee"
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
-
data = json.loads(response.content)
|
17 |
-
temperature = data['main']['temp']
|
18 |
-
description = data['weather'][0]['description']
|
19 |
-
wind_speed = data['wind']['speed']
|
20 |
-
print(f"Temperature: {temperature - 273.15:.1f} °C")
|
21 |
-
print(f"Weather Description: {description.title()}")
|
22 |
-
print(f"Wind Speed: {wind_speed} m/s")
|
23 |
-
else:
|
24 |
-
print(response.json()['message'])
|
|
|
4 |
# Replace YOUR_API_KEY_HERE with your actual API key from OpenWeatherMap
|
5 |
api_key = "1aafc3163909c1493596da9340e00aee"
|
6 |
|
7 |
+
def get_weather(city_name):
|
8 |
+
# Make the API call to get the current weather details
|
9 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city_name}&appid={api_key}"
|
10 |
+
response = requests.get(url)
|
11 |
|
12 |
+
# Extract the relevant information from the response
|
13 |
+
if response.status_code == 200:
|
14 |
+
data = json.loads(response.content)
|
15 |
+
temperature = data['main']['temp']
|
16 |
+
description = data['weather'][0]['description']
|
17 |
+
wind_speed = data['wind']['speed']
|
18 |
+
print(f"Temperature: {temperature - 273.15:.1f} °C")
|
19 |
+
print(f"Weather Description: {description.title()}")
|
20 |
+
print(f"Wind Speed: {wind_speed} m/s")
|
21 |
+
else:
|
22 |
+
print(response.json()['message'])
|
23 |
|
24 |
+
# Call the get_weather() function with a city name argument
|
25 |
+
get_weather("London")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|