Building a Weather Forecast Dashboard with Python and API Integration

Learn how to create a beginner-friendly weather forecast dashboard using Python and an API to fetch live weather data.

In this tutorial, we will build a simple weather forecast dashboard using Python. We'll integrate a weather API to fetch real-time weather data for any city and display it in a clear format. This beginner-friendly guide will cover getting API data, parsing JSON, and displaying the information.

First, you need to sign up for a free API key from OpenWeatherMap (https://openweathermap.org/api). The API will provide the weather details for the city you request.

Make sure you have Python installed on your system. You'll also need the 'requests' library to make HTTP requests. If you don't have it installed, run this command in your terminal or command prompt:

python
pip install requests

Now, let's write the Python code to fetch and display the weather data. We'll create a function to call the API and print the temperature, weather description, humidity, and wind speed.

python
import requests

API_KEY = 'your_api_key_here'  # Replace with your OpenWeatherMap API key
BASE_URL = 'http://api.openweathermap.org/data/2.5/weather'


def get_weather(city):
    params = {
        'q': city,
        'appid': API_KEY,
        'units': 'metric'  # Use 'imperial' for Fahrenheit
    }
    response = requests.get(BASE_URL, params=params)
    if response.status_code == 200:
        data = response.json()
        weather = data['weather'][0]['description']
        temp = data['main']['temp']
        humidity = data['main']['humidity']
        wind_speed = data['wind']['speed']

        print(f"Weather in {city.title()}:")
        print(f"Description: {weather}")
        print(f"Temperature: {temp}°C")
        print(f"Humidity: {humidity}%")
        print(f"Wind Speed: {wind_speed} m/s")
    else:
        print(f"Error fetching weather data for {city}. Please check the city name.")


if __name__ == '__main__':
    city = input("Enter city name: ")
    get_weather(city)

Let's break down what this code does: - It asks the user to enter a city name. - It sends a request to the OpenWeatherMap API with the city and your API key. - If the request is successful, it parses the JSON response to extract weather details. - It prints the weather description, temperature in Celsius, humidity percentage, and wind speed. - If the city is not found or there is an error, it prints an appropriate error message.

To run the script, save it as weather_dashboard.py and execute it from the command line with: python weather_dashboard.py Then enter the city when prompted. You'll see the current weather conditions displayed.

This basic dashboard can be expanded with GUI libraries like Tkinter or even converted into a web app using frameworks like Flask or Django. But this script is a great starting point to understand how API integration and data parsing works in Python.