Skip to main content

Weather Forecast App in Python

Building a Weather Forecast App in Python

Building a Weather Forecast App in Python

Weather forecasting is an essential part of our daily lives, helping us plan our activities. In this article, we'll walk through the process of building a simple weather forecast app using Python. We’ll use the OpenWeatherMap API to get real-time weather data and display it in a user-friendly format.



Prerequisites

Before we start, make sure you have the following:

  • Python installed on your machine.
  • An API key from OpenWeatherMap (you can sign up for a free account to get one).
  • Flask or Tkinter (depending on whether you want a web-based or GUI-based app).

Step 1: Setting Up the Project

First, let's set up a basic Python project. Create a new directory for your project and navigate into it:

mkdir weather_app
cd weather_app

Next, create a new Python file named app.py where we will write our code:

touch app.py

Step 2: Installing Required Packages

We need to install some Python packages to work with APIs and handle HTTP requests. If you're using Flask, you'll also need to install it:

pip install requests Flask

Step 3: Writing the Python Code

In your app.py file, start by importing the necessary libraries:

import requests
from flask import Flask, render_template, request

Now, let's define the basic structure of our Flask app and set up a route to handle the weather data:

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
    weather_data = None
    if request.method == 'POST':
        city = request.form.get('city')
        api_key = 'your_api_key_here'
        url = f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric'
        response = requests.get(url)
        weather_data = response.json()
    return render_template('index.html', weather=weather_data)

Step 4: Creating the HTML Template

Create an index.html file in a new directory named templates. This file will display the weather information:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Weather Forecast</title>
</head>
<body>
    <h1>Weather Forecast</h1>
    <form method="post">
        <input type="text" name="city" placeholder="Enter city name" required>
        <button type="submit">Get Weather</button>
    </form>

    {% if weather %}
    <h2>Weather in {{ weather['name'] }}</h2>
    <p>Temperature: {{ weather['main']['temp'] }}°C</p>
    <p>Description: {{ weather['weather'][0]['description'] }}</p>
    {% endif %}
</body>
</html>

Step 5: Running the App

Finally, run your Flask app by executing the following command in your terminal:

python app.py

Visit http://localhost:5000 in your browser, and you'll be able to see your weather forecast app in action!

Conclusion

In this article, we built a simple weather forecast app using Python and Flask. This project demonstrates how easy it is to interact with APIs in Python and create user-friendly web applications. You can extend this project by adding more features, such as a weather forecast for multiple days, or even integrating it into a larger project.

Comments

Popular posts from this blog

Getting Started with Python

A Beginner's Guide to Python A Beginner's Guide to Python Python is one of the most popular programming languages today, loved for its simplicity and versatility. No matter if you're new to programming or looking to add another language to your skill set, Python is always an excellent choice to start with. Why Learn Python? Python is a high-level, interpreted language known for its easy-to-read syntax. It's widely used in various fields, including web development, data analysis, artificial intelligence, scientific computing, and automation. This guide will help you take your first steps in learning Python. Setting Up Your Environment Before you start coding, you'll need to set up Python on your computer: Install Python: Download the latest version of Python from the official website. The installation process is straightforward, and Python comes with...

Python to Transform Your Business

How Python is Transforming Business Operations How Python is Transforming Business Operations Curious about how Python can revolutionize your business? This post delves into the practical uses of Python in the business realm, particularly in data analysis, and discusses why adopting Python tools could be a wise decision for your organization. The Rise of Python in Business Python is undoubtedly having its day in the sun, and for good reason. Utilizing Python in business, especially for data analysis, is changing the way companies manage their data, analytics, and automation. By leveraging Python's flexible capabilities, businesses can enhance efficiency, extract valuable insights, and gain a competitive edge in their industries. The Value of Data in Business Data is immensely valuable, but to truly tap into its potential, companies need to know how to use it effectively. Python, with its vast array of libraries for data analysis, artificial intelligence, and ma...