Get City Weather
Tags: #openweathermap #opendata #snippet

Input

Import library

1
import requests
Copied!

Variables

1
OPENWEATHER_KEY = '**********' # get your key from here https://home.openweathermap.org/api_keys (it takes couples of minutes)
2
CITY = "Paris"
Copied!

Model

Fonctions

1
def get_weather_info(city):
2
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_KEY}"
3
response = requests.get(url)
4
return response.json()
5
6
def format_weather_data(data):
7
return {
8
"temp": f'{round(int(data["main"]["temp"]) - 273.15, 1)}°',
9
"city": data["name"],
10
}
11
12
def run(city):
13
data = get_weather_info(city)
14
return format_weather_data(data)
Copied!

Output

Display result

1
run(CITY)
Copied!
Copy link
Edit on GitHub