Skip to content

Latest commit

 

History

History
57 lines (35 loc) · 1.87 KB

OpenWeatherMap_Get_City_Weather.md

File metadata and controls

57 lines (35 loc) · 1.87 KB



Template request | Bug report | Generate Data Product

Tags: #openweathermap #opendata #snippet #dataframe

Author: Christophe Blefari

Description: This notebook provides an easy way to access current weather data for any city using the OpenWeatherMap API.

Input

Import library

import requests

Variables

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

Model

Fonctions

def get_weather_info(city):
    url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_KEY}"
    response = requests.get(url)
    return response.json()


def format_weather_data(data):
    return {
        "temp": f'{round(int(data["main"]["temp"]) - 273.15, 1)}°',
        "city": data["name"],
    }


def run(city):
    data = get_weather_info(city)
    return format_weather_data(data)

Output

Display result

run(CITY)