Skip to content

Lawliet7129/weatherForecast

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Code Structure and Architecture

The WeatherForecast application utilizes the Flask framework to provide weather forecasts for cities using the OpenWeatherMap API.

Prerequisites

  • Python 3.6 or higher
  • Flask
  • Requests library

Setting Up

  1. Clone the Repository: Clone the source code from your repository or download the zip file and extract it into your local machine.

    git clone <repository-url>
    cd WeatherForecast
    
  2. Install Dependencies Install the necessary Python dependencies using pip. It's recommended to use a virtual environment to avoid conflicts with other packages.

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    pip install flask requests
    
  3. API Key Configuration You need an API key from OpenWeatherMap to fetch weather data: Sign up or log in at OpenWeatherMap.. Replace 'Enter_API_KEY' in app.py with your actual API key.

Once the setup is complete, you can run the application locally:

python app.py

This command starts the Flask server on localhost on port 5000. You can access the application by navigating to http://localhost:5000 in your web browser.

Usage

  • Home Page: The home page allows you to enter a city name and select units (Metric or Imperial) and language for the weather report.
  • Submit: After entering the city name and preferences, click the 'Get Forecast' button to view the weather forecast.

Key Components

  • app.py: The core script of the application. It sets up the Flask application, defines routes, and includes all the main functional operations for handling API interactions.

Functions and Routes

The application is designed around Flask routes and specific helper functions, detailed as follows:

  1. home() Function:

    • Functionality: Serves the homepage using the index.html template, allowing users to input the city name for which they seek weather data.
  2. get_coordinates(city_name) Function:

    • Functionality: Fetches geographic coordinates (latitude and longitude) from the OpenWeatherMap Geocoding API by city name.
    • Rationale: Isolating this functionality enhances testability and maintainability. It also supports potential scalability, for example, integrating additional APIs or features in the future.
  3. weather() Function:

    • Route: /weather (POST method)
    • Functionality: Handles form submissions from the homepage. It retrieves the city name, units, and language from the form, obtains geographic coordinates, fetches weather data based on these coordinates, and handles any errors.
    • Error Handling: Displays an error message on the homepage if the city cannot be located or if the weather data cannot be fetched.
  4. fetch_weather_data(lat, lon, units, lang) Function:

    • Functionality: Requests detailed weather information based on latitude and longitude from OpenWeatherMap and formats this data for the frontend.
    • Rationale: Centralizing API interactions within this function simplifies the application's error handling and data formatting, leading to more maintainable and robust code.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published