Day 1 - Building a Simple Weather Dashboard with Python, Google Cloud Storage, and OpenWeather API
In this article, we'll explore how to build a basic weather dashboard using Python. The complete code for this project is available on GitHub. This dashboard will:
Fetch real-time weather data for a specific city using the OpenWeather API.
Store the fetched data in a Google Cloud Storage (GCS) bucket for later use or analysis.
Prerequisites
Before we dive into the code, ensure you have the following:
Python 3.x installed on your system.
A Google Cloud project with a Service Account having Storage Admin privileges.
An OpenWeather API account and your API key.
The requests
, google-cloud-storage
, google-auth
, and dotenv
libraries installed:
pip install requests google-cloud-storage google-auth python-dotenv
Project Setup
Create a Google Cloud Project and enable the Google Cloud Storage API.
Create a Service Account with Storage Admin privileges and download the JSON key file.
Create a GCS bucket where you'll store the weather data.
Obtain an OpenWeather API key by signing up for an account.
Create a
.env
file in the same directory as your script. Add the following environment variables:OPENWEATHER_API_KEY=<your_api_key> GCP_STORAGE_BUCKET_NAME=<your_bucket_name>
Code Implementation
Here's the Python code for the weather dashboard:
import os
import json
import requests
from google.cloud import storage
from google.oauth2 import service_account
from datetime import datetime
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
file_name = '<your-credentials-file>.json'
the_path = os.getcwd() # Get the current working directory
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.join(the_path, file_name)
class WeatherDashboard:
def __init__(self):
self.api_key = os.getenv('OPENWEATHER_API_KEY')
self.bucket_name = os.getenv('GCP_STORAGE_BUCKET_NAME')
self.storage_client = storage.Client()
def create_bucket_if_not_exists(self):
"""Create GCS bucket if it doesn't exist"""
try:
self.storage_client.get_bucket(self.bucket_name)
print(f"Bucket {self.bucket_name} exists")
except:
try:
bucket = self.storage_client.create_bucket(
self.bucket_name,
location='us-east1'
)
print(f"Successfully created bucket {bucket.name}")
except Exception as e:
print(f"Error creating bucket: {e}")
def fetch_weather(self, city):
"""Fetch weather data from OpenWeather API"""
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
def save_to_gcs_bucket(self, weather_data, city):
"""Save weather data to GCS bucket"""
if not weather_data:
return False
timestamp = datetime.now().strftime('%Y%m%d-%H%M%S')
file_name = f"weather-data/{city}-{timestamp}.json"
bucket = self.storage_client.bucket(self.bucket_name)
blob = bucket.blob(file_name)
blob.upload_from_string(json.dumps(weather_data), content_type='application/json')
print(f"Successfully saved data for {city} to GCS Bucket")
def main():
dashboard = WeatherDashboard()
# Create bucket if needed
dashboard.create_bucket_if_not_exists()
cities = ["Cairo"]
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 GCS
success = dashboard.save_to_gcs_bucket(weather_data, city)
if success:
print(f"Weather data for {city} saved to GCS!")
else:
print(f"Failed to fetch weather data for {city}")
if __name__ == "__main__":
main()
Explanation
Import necessary libraries:
os
: For interacting with the operating system.json
: For working with JSON data.requests
: For making HTTP requests to the OpenWeather API.google.cloud.storage
: For interacting with Google Cloud Storage.datetime
: For generating timestamps for filenames.dotenv
: For loading environment variables from the.env
file.
Define the
WeatherDashboard
class:__init__
: Initializes the class with the API key, bucket name, and creates astorage.Client
object.create_bucket_if_not_exists
: Checks if the specified GCS bucket exists. If not, it creates the bucket in theus-east1
location.fetch_weather
: Makes an API call to OpenWeather to get weather data for a given city.save_to_gcs_bucket
: Saves the fetched weather data as a JSON file to the GCS bucket.
Define the
main
function:Creates an instance of the
WeatherDashboard
class.Creates the GCS bucket if it doesn't exist.
Iterates through a list of cities (currently only "Cairo").
Fetches weather data for each city.
Prints the weather information to the console.
Saves the weather data to the GCS bucket.
Running the Script:
Save the code as a Python file (e.g.,
weather_dashboard.py
).Replace the placeholder values in the
.env
file with your actual API key and bucket name.Run the script:
python weather_dashboard.py
Conclusion
This article demonstrated a basic example of building a weather dashboard using Python, Google Cloud Storage, and the OpenWeather API. This approach can be extended to:
Fetch weather data for multiple cities at once.
Process and visualize the collected weather data.
Schedule automatic updates to the weather data.
I hope this article provides a helpful starting point for your weather dashboard project!
Note: This article provides a basic framework. You can enhance it further by adding features like error handling, data validation, and a more user-friendly interface.