weather app

Building a weather app in Python is a great way for beginners to practice coding, we also discussed some simple python projects in our guide. Moreover this projects allows you to learn how to use APIs, work with JSON data, and make HTTP requests using Python’s requests library. By following this guide, you’ll be able to create a basic app that fetches real-time weather data for any city.

Focused Keyword: weather app in Python

What You’ll Need:

  • Python installed on your machine
  • An API key from OpenWeatherMap
  • Basic knowledge of Python

Step 1: Set Up Python

First, ensure that you have Python installed on your system. To check this, open your terminal or command prompt and type:

python --version

If you don’t have Python installed, download it from the official website. After installation, you’ll also need the requests library. You can install it by running:

pip install requests

Step 2: Get an API Key

To get weather data, you’ll need access to a weather API. OpenWeatherMap offers free access, though it has some limits on daily requests.

  1. Go to the OpenWeatherMap website.
  2. Sign up for a free account.
  3. After creating your account, you will receive an API key. Which you’ll use key to make requests to the weather API.

Step 3: Write the Python Script

You can now start writing the Python script to fetch weather data. Here’s a basic example of how you can make a request:

import requests

def get_weather(city):
api_key = "your_api_key_here" # Replace with your actual API key
base_url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
response = requests.get(base_url)

if response.status_code == 200:
data = response.json()
temperature = data['main']['temp']
weather_desc = data['weather'][0]['description']
return f"The weather in {city} is {weather_desc} with a temperature of {temperature}°C."
else:
return "City not found."

city = input("Enter city name: ")
print(get_weather(city))

Explanation:

  • The requests.get() function makes an HTTP request to the weather API.
  • The API responds with data in JSON format, which we convert into a Python dictionary using response.json().
  • We extract information like temperature and weather description from the data and print it in a readable format.

Step 4: Improve the App

While the app works, we can add more features to make it better. You can display more weather details such as humidity or wind speed, and you can also handle errors better. Here’s an improved version:

import requests

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

if response.status_code == 200:
data = response.json()
main = data['main']
weather_desc = data['weather'][0]['description']
temperature = main['temp']
humidity = main['humidity']
wind_speed = data['wind']['speed']

return (f"City: {city}\n"
f"Weather: {weather_desc}\n"
f"Temperature: {temperature}°C\n"
f"Humidity: {humidity}%\n"
f"Wind Speed: {wind_speed} m/s\n")
else:
return "City not found, please check the city name."

while True:
city = input("Enter city name (or type 'exit' to quit): ")
if city.lower() == 'exit':
break
print(get_weather(city))

This version gives more details about the weather and handles multiple cities.


Step 5: Add Error Handling

You can make the app more robust by adding error handling for when the city name is not found, or when the API request fails. This can improve the user experience:

if response.status_code == 404:
return "City not found. Please try again."
elif response.status_code != 200:
return "An error occurred while fetching the data."
weather app in python

Step 6: Expand the App

To make your weather app more advanced, you can add features like:

  • Forecast: Display a 5-day weather forecast.
  • Graphical Interface: Build a GUI using Tkinter for better user interaction.
  • Save Data: Save weather information to a file for later use.

Leave a Reply

Your email address will not be published. Required fields are marked *

Instagram

This error message is only visible to WordPress admins

Error: No feed found.

Please go to the Instagram Feed settings page to create a feed.