DEV Community

Teniola
Teniola

Posted on

Building a Weather Dashboard with Python and AWS S3

Weather Dashboard

Overview

The Weather Dashboard is a Python-based application designed to fetch real-time weather data for specified cities and store this data in an AWS S3 bucket. It leverages the OpenWeather API for retrieving weather information and uses the Boto3 library to interact with AWS S3, making it a handy tool for anyone who wants to organize and store weather data efficiently.


Features

  • Fetches real-time weather data for multiple cities
  • Displays temperature (°F), humidity, and weather conditions
  • Automatically stores weather data in AWS S3

- Supports multiple cities tracking

Prerequisites

Before you begin, ensure you have the following:

  • Python 3.6 or higher installed on your system.
  • An AWS account with S3 access configured.
  • An OpenWeather API key to fetch weather data.

Installation

1. Clone the Repository

Run the following commands to clone the repository and navigate to its directory:

git clone https://github.com/Teni1023/weather-dashboard.git
cd weather-dashboard
Enter fullscreen mode Exit fullscreen mode

2. Create and Activate a Virtual Environment

Create a virtual environment to manage dependencies:

python -m venv venv
source venv/bin/activate  # On Windows, use `venv\Scripts\activate`
Enter fullscreen mode Exit fullscreen mode

3. Install Required Packages

Install the dependencies listed in the requirements.txt file:

pip install -r requirements.txt
Enter fullscreen mode Exit fullscreen mode

4. Configure Environment Variables

Create a .env file in the root directory of the project and add the following environment variables:

OPENWEATHER_API_KEY=your_openweather_api_key
AWS_ACCESS_KEY=your_aws_access_key
AWS_SECRET_KEY=your_aws_secret_key
AWS_REGION=your_aws_region
S3_BUCKET_NAME=your_s3_bucket_name
Enter fullscreen mode Exit fullscreen mode

Replace the placeholders with your actual API keys and AWS details.


Usage

Run the application with the following command:

python src/weather_dashboard.py
Enter fullscreen mode Exit fullscreen mode
  • The application will fetch weather data for the cities specified in the code.
  • It will display the weather data in a readable format on the console.
  • Additionally, the weather data will be saved to your configured AWS S3 bucket.

Code Structure

Here's an overview of the key components of the application:

weather_dashboard.py

The main script containing the logic for:

  • Fetching weather data using the OpenWeather API.
  • Displaying the data in a user-friendly format.
  • Saving the data to AWS S3.

Methods

__init__()

  • Initializes the WeatherDashboard class.
  • Loads environment variables and sets up configurations.

create_bucket_if_not_exists()

  • Checks if the specified S3 bucket exists.
  • Creates the bucket if it doesn't exist.

fetch_weather(city)

  • Fetches real-time weather data for the specified city from the OpenWeather API.

display_weather_data(city, weather_data)

  • Displays weather details such as temperature, humidity, wind speed, and weather description in a readable format.

save_weather_data_to_s3(city, weather_data)

  • Saves the fetched weather data to the specified AWS S3 bucket for future reference.

Example Output

Here’s what the output of the application might look like:

Bucket my-weather-bucket exists
Fetching weather for Philadelphia...
Weather data for Philadelphia:
Temperature: 75°F
Weather: clear sky
Humidity: 50%
Wind Speed: 5 mph
Timestamp: 2023-10-01 12:00:00
----------------------------------------
Successfully saved data for Philadelphia to S3
Enter fullscreen mode Exit fullscreen mode

Conclusion

The Weather Dashboard is a simple yet powerful tool for fetching, displaying, and storing weather data. By integrating Python, AWS S3, and the OpenWeather API, this project demonstrates how you can combine various technologies to build a functional and scalable application.

Feel free to clone the repository, explore the code, and customize it to suit your needs.

Top comments (0)