typesdigital commited on
Commit
c6df024
·
1 Parent(s): 6e0aed9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -19
app.py CHANGED
@@ -1,25 +1,23 @@
1
  import requests
2
  import json
3
 
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")
 
1
  import requests
2
  import json
3
 
4
+ # Get an API key from https://openweathermap.org/api
5
  api_key = "1aafc3163909c1493596da9340e00aee"
6
 
7
+ # Make a request to the OpenWeatherMap API
8
+ url = "https://api.openweathermap.org/data/2.5/weather?q=London&appid={}".format(api_key)
9
+ response = requests.get(url)
 
10
 
11
+ # Parse the response
12
+ if response.status_code == 200:
13
+ # The request was successful
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("Temperature: {} °C".format(temperature))
19
+ print("Weather Description: {}".format(description))
20
+ print("Wind Speed: {} m/s".format(wind_speed))
21
+ else:
22
+ # The request failed
23
+ print("Error: {}".format(response.json()['message']))