Building a Weather reports Dashboard with python, S3 and Openweather API

Introduction Have you ever wanted to build an application that fetches real-time weather data and securely stores it for later use? In this article, I'll walk you through how I developed a weather reporting app using Python, the OpenWeather API, and AWS S3. This app: Fetches current weather information for selected cities. Displays the weather data on the console. Stores the data in JSON format in an S3 bucket for archival purposes. This is my first project in the 30-days DevOps challenge series. Setting Up the Project Requirements Python 3.x AWS CLI configured with credentials and permissions aws configure aws s3 mb s3://your_s3_bucket_name OpenWeather API key A .env file to store environment variables: OPENWEATHER_API_KEY=your_openweather_api_key AWS_BUCKET_NAME=your_s3_bucket_name Installing Required Libraries Install the necessary libraries: pip install python-dotenv requests boto3 Alternatively, use a requirements.txt file: touch requirements.txt echo "python-dotenv" >> requirements.txt echo "requests" >> requirements.txt echo "boto3" >> requirements.txt pip install -r requirements.txt The App 1. Importing Packages import os import json import boto3 import requests from datetime import datetime from dotenv import load_dotenv 2. Environment Setup Initialize the app to interact with the API and AWS S3: load_dotenv() class WeatherDashboard: def __init__(self): self.api_key = os.getenv('OPENWEATHER_API_KEY') self.bucket_name = os.getenv('AWS_BUCKET_NAME') self.s3_client = boto3.client('s3') def create_bucket_if_not_exists(self): try: self.s3_client.head_bucket(Bucket=self.bucket_name) print(f"Bucket {self.bucket_name} exists") except: print(f"Creating bucket {self.bucket_name}") try: self.s3_client.create_bucket(Bucket=self.bucket_name) print(f"Successfully created bucket {self.bucket_name}") except Exception as e: print(f"Error creating bucket: {e}") 3. Fetching Weather Data def fetch_weather(self, city): base_url = "http://api.openweathermap.org/data/2.5/weather" params = { "q": city, "appid": self.api_key, "units": "imperial" } try: response = requests.get(base_url, params=params) response.raise_for_status() return response.json() except requests.exceptions.RequestException as e: print(f"Error fetching weather data: {e}") return None 4. Saving Data to S3 def save_to_s3(self, weather_data, city): if not weather_data: return False timestamp = datetime.now().strftime('%Y%m%d-%H%M%S') file_name = f"weather-data/{city}-{timestamp}.json" try: weather_data['timestamp'] = timestamp self.s3_client.put_object( Bucket=self.bucket_name, Key=file_name, Body=json.dumps(weather_data), ContentType='application/json' ) print(f"Successfully saved data for {city} to S3") return True except Exception as e: print(f"Error saving to S3: {e}") return False 5. Main Function def main(): dashboard = WeatherDashboard() # Create bucket if needed dashboard.create_bucket_if_not_exists() cities = ["Philadelphia", "Seattle", "New York"] for city in cities: print(f"\nFetching weather for {city}...") weather_data = dashboard.fetch_weather(city) if weather_data: temp = weather_data['main']['temp'] feels_like = weather_data['main']['feels_like'] humidity = weather_data['main']['humidity'] description = weather_data['weather'][0]['description'] print(f"Temperature: {temp}°F") print(f"Feels like: {feels_like}°F") print(f"Humidity: {humidity}%") print(f"Conditions: {description}") # Save to S3 success = dashboard.save_to_s3(weather_data, city) if success: print(f"Weather data for {city} saved to S3!") else: print(f"Failed to fetch weather data for {city}") if __name__ == "__main__": main() Check your s3 bucket aws s3 ls s3://your_s3_bucket_name alternatively, you could use the console to the see the content of your bucket Conclusion Building a weather reporting app that integrates with AWS S3 demonstrates the power of combining APIs, cloud storage, and Python automation. This project retrieves live weather data using the O

Jan 19, 2025 - 13:23
Building a Weather reports Dashboard with python, S3 and Openweather API

Introduction

Have you ever wanted to build an application that fetches real-time weather data and securely stores it for later use? In this article, I'll walk you through how I developed a weather reporting app using Python, the OpenWeather API, and AWS S3.

This app:

  • Fetches current weather information for selected cities.
  • Displays the weather data on the console.
  • Stores the data in JSON format in an S3 bucket for archival purposes.

This is my first project in the 30-days DevOps challenge series.

Setting Up the Project

Requirements

  1. Python 3.x
  2. AWS CLI configured with credentials and permissions
   aws configure
   aws s3 mb s3://your_s3_bucket_name
  1. OpenWeather API key
  2. A .env file to store environment variables:
   OPENWEATHER_API_KEY=your_openweather_api_key  
   AWS_BUCKET_NAME=your_s3_bucket_name  

Installing Required Libraries

Install the necessary libraries:

pip install python-dotenv requests boto3

Alternatively, use a requirements.txt file:

touch requirements.txt  
echo "python-dotenv" >> requirements.txt  
echo "requests" >> requirements.txt  
echo "boto3" >> requirements.txt  
pip install -r requirements.txt  

The App

1. Importing Packages

import os  
import json  
import boto3  
import requests  
from datetime import datetime  
from dotenv import load_dotenv  

2. Environment Setup

Initialize the app to interact with the API and AWS S3:

load_dotenv()  

class WeatherDashboard:  
    def __init__(self):  
        self.api_key = os.getenv('OPENWEATHER_API_KEY')  
        self.bucket_name = os.getenv('AWS_BUCKET_NAME')  
        self.s3_client = boto3.client('s3')  

    def create_bucket_if_not_exists(self):  
        try:  
            self.s3_client.head_bucket(Bucket=self.bucket_name)  
            print(f"Bucket {self.bucket_name} exists")  
        except:  
            print(f"Creating bucket {self.bucket_name}")  
            try:  
                self.s3_client.create_bucket(Bucket=self.bucket_name)  
                print(f"Successfully created bucket {self.bucket_name}")  
            except Exception as e:  
                print(f"Error creating bucket: {e}")  

3. Fetching Weather Data

def fetch_weather(self, city):  
    base_url = "http://api.openweathermap.org/data/2.5/weather"  
    params = {  
        "q": city,  
        "appid": self.api_key,  
        "units": "imperial"  
    }  

    try:  
        response = requests.get(base_url, params=params)  
        response.raise_for_status()  
        return response.json()  
    except requests.exceptions.RequestException as e:  
        print(f"Error fetching weather data: {e}")  
        return None  

4. Saving Data to S3

def save_to_s3(self, weather_data, city):  
    if not weather_data:  
        return False  

    timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')  
    file_name = f"weather-data/{city}-{timestamp}.json"  

    try:  
        weather_data['timestamp'] = timestamp  
        self.s3_client.put_object(  
            Bucket=self.bucket_name,  
            Key=file_name,  
            Body=json.dumps(weather_data),  
            ContentType='application/json'  
        )  
        print(f"Successfully saved data for {city} to S3")  
        return True  
    except Exception as e:  
        print(f"Error saving to S3: {e}")  
        return False  

5. Main Function

def main():  
    dashboard = WeatherDashboard()  

    # Create bucket if needed  
    dashboard.create_bucket_if_not_exists()  

    cities = ["Philadelphia", "Seattle", "New York"]  

    for city in cities:  
        print(f"\nFetching weather for {city}...")  
        weather_data = dashboard.fetch_weather(city)  
        if weather_data:  
            temp = weather_data['main']['temp']  
            feels_like = weather_data['main']['feels_like']  
            humidity = weather_data['main']['humidity']  
            description = weather_data['weather'][0]['description']  

            print(f"Temperature: {temp}°F")  
            print(f"Feels like: {feels_like}°F")  
            print(f"Humidity: {humidity}%")  
            print(f"Conditions: {description}")  

            # Save to S3  
            success = dashboard.save_to_s3(weather_data, city)  
            if success:  
                print(f"Weather data for {city} saved to S3!")  
        else:  
            print(f"Failed to fetch weather data for {city}")  

if __name__ == "__main__":  
    main()  

Check your s3 bucket

aws s3 ls s3://your_s3_bucket_name 

alternatively, you could use the console to the see the content of your bucket

Conclusion

Building a weather reporting app that integrates with AWS S3 demonstrates the power of combining APIs, cloud storage, and Python automation. This project retrieves live weather data using the OpenWeather API, processes it, and securely stores it in S3 for future use.

Further Enhancements:

  • Add a web interface.
  • Integrate data visualization tools.
  • Set up scheduled updates with AWS Lambda.

This project is a great way to sharpen your Python skills while exploring cloud technologies. Happy coding!

GitHub Repository