Sai A Sai A
Updated date Mar 05, 2024
In this blog, we will learn how to build a simple weather app in Python using two different methods. We are fetching and displaying current weather information for any city using the OpenWeatherMap API and the pyowm library.

Introduction:

In today's world, accessing weather information is easier than ever. Whether you are planning a weekend getaway or just want to know if you need an umbrella tomorrow, having a simple weather app can be incredibly handy. In this blog, we will learn the process of building a basic weather app in Python

Method 1: Using OpenWeatherMap API

To get weather data in Python is by using an API (Application Programming Interface). We will be using the OpenWeatherMap API, which provides current weather data, forecasts, and historical data. First, you will need to sign up for a free account on the OpenWeatherMap website to obtain an API key.

import requests

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

city = input("Enter city name: ")
api_key = "YOUR_API_KEY"  # Replace with your actual API key from OpenWeatherMap
weather_data = get_weather(city, api_key)
print("Current Weather in", city)
print("Temperature:", weather_data['main']['temp'], "°C")
print("Description:", weather_data['weather'][0]['description'])

Output:

Enter city name: London
Current Weather in London
Temperature: 9.12 °C
Description: overcast clouds

In this method, we defined a function get_weather that takes a city name and an API key as parameters. It constructs a URL with the city name and API key to make a GET request to the OpenWeatherMap API. We specify the units as metric to get temperature in Celsius. The response is then converted to JSON format, and relevant weather information such as temperature and description is extracted and printed.

Method 2: Using pyowm Library

Another approach to fetch weather data is by using a Python library like pyowm, which provides a wrapper around various weather APIs including OpenWeatherMap.

from pyowm.owm import OWM

def get_weather(city, api_key):
    owm = OWM(api_key)
    mgr = owm.weather_manager()
    observation = mgr.weather_at_place(city)
    w = observation.weather
    return w

city = input("Enter city name: ")
api_key = "YOUR_API_KEY"  # Replace with your actual API key from OpenWeatherMap
weather = get_weather(city, api_key)
print("Current Weather in", city)
print("Temperature:", weather.temperature('celsius')['temp'], "°C")
print("Description:", weather.detailed_status)

Output:

Enter city name: Paris
Current Weather in Paris
Temperature: 10.0 °C
Description: broken clouds

In this method, we utilize the pyowm library to simplify the process of fetching weather data. We create an instance of the OWM class with our API key, then use the weather_manager() method to get a reference to the weather manager. We fetch the current weather observation for the specified city and extract relevant information such as temperature and description.

Conclusion:

In this blog, we have discussed two methods for building a simple weather app in Python. The first method uses the OpenWeatherMap API directly with the requests library, while the second method uses the pyowm library to abstract away some of the complexity. 

Comments (0)

There are no comments. Be the first to comment!!!