Project Description

We will build the following weather dashboard web app:

Users can search for a city in the text box and the current weather information for that city will be displayed below the button:

You can use the Open Weather API to get current weather data. You need to sign up for a free account and get an API key. Then, build a Flask web app that makes API requests to the http://api.openweathermap.org/data/2.5/weather endpoint and display the received JSON data to the webpage.

Objectives

  1. Learn how to interact with a public API.
  2. Work with JSON data and extract relevant information.
  3. Display dynamic data on a web page using Flask.
  4. Understand how to handle API keys and environment variables for security.
  5. Improve your ability to build interactive web applications with external data.

Prerequisites:

Required Libraries: requests, flask
pip install flask requests
Required Files: No files are required.
IDE: Use any IDE.

Solution:

How to run the app:

  • Save the Python script as app.pySave the index.html file inside a folder named templates.
  • Run the Flask app from the command line: python app.py
  • Open a browser and navigate to: http://127.0.0.1:5000/
  • Add tasks, mark them as completed, and delete them using the app.

app.py:

from flask import Flask, render_template, request
import requests


app = Flask(__name__)

# Get API key from environment variables
API_KEY = "141710af2113bab9f55ef73e1bcd33d5"
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"

# Function to get weather data
def get_weather(city):
    params = {
        'q': city,
        'appid': API_KEY,
        'units': 'metric'
    }
    response = requests.get(BASE_URL, params=params)
    return response.json()

@app.route('/', methods=['GET', 'POST'])
def index():
    weather_data = None
    error = None
    if request.method == 'POST':
        city = request.form['city']
        weather_data = get_weather(city)
        if weather_data.get('cod') != 200:
            error = "City not found. Please try again."
            weather_data = None
    return render_template('index.html', weather_data=weather_data, error=error)

if __name__ == '__main__':
    app.run(debug=True)

templates/index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather Dashboard</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f0f8ff;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 0 auto;
            padding: 20px;
            background-color: white;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            margin-top: 50px;
            text-align: center;
        }
        h1 {
            color: #007bff;
        }
        .form-container {
            margin-bottom: 20px;
        }
        input[type="text"] {
            padding: 10px;
            width: 70%;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        input[type="submit"] {
            padding: 10px;
            margin-left: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        .weather-info {
            margin-top: 20px;
        }
        .error {
            color: red;
        }
        .weather-icon {
            width: 50px;
            height: 50px;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>Weather Dashboard</h1>

    <div class="form-container">
        <form method="POST">
            <input type="text" name="city" placeholder="Enter city name" required>
            <input type="submit" value="Get Weather">
        </form>
    </div>

    {% if error %}
        <p class="error">{{ error }}</p>
    {% elif weather_data %}
        <div class="weather-info">
            <h2>{{ weather_data.name }}</h2>
            <img src="http://openweathermap.org/img/wn/{{ weather_data.weather[0].icon }}@2x.png" alt="Weather icon" class="weather-icon">
            <p><strong>Temperature:</strong> {{ weather_data.main.temp }}°C</p>
            <p><strong>Humidity:</strong> {{ weather_data.main.humidity }}%</p>
            <p><strong>Weather:</strong> {{ weather_data.weather[0].description }}</p>
            <p><strong>Wind Speed:</strong> {{ weather_data.wind.speed }} m/s</p>
        </div>
    {% endif %}
</div>

</body>
</html>

Happy Coding