How to Build a Weather Notifier App Using Python and a Public API
Whether you're a developer trying to stay dry or just looking for a fun Python project, a Weather Notifier App is a great choice. In this blog, we’ll build a simple weather notification tool using Python and the OpenWeatherMap API. You’ll learn about API integration, notifications, and simple scheduling — all in under 100 lines of code.
What You’ll Need:
Python 3 installed
requestslibrary (for API calls)plyerlibrary (for desktop notifications)A free API key from OpenWeatherMap
Step 1: Get an API Key from OpenWeatherMap
Sign up and log in.
Go to the "API keys" section in your account and generate a new key.
Use the “Current Weather Data” API.
Step 2: Install Required Libraries
Open your terminal and install the necessary Python libraries:pip install requests plyer
Step 3: Fetch Weather Data Using Python
Create a new file called weather_notifier.py and start by writing a function to fetch weather data:
import requests
API_KEY = 'your_api_key_here'
CITY = 'London'
BASE_URL = "http://api.openweathermap.org/data/2.5/weather?"
def get_weather(city):
url = f"{BASE_URL}appid={API_KEY}&q={city}&units=metric"
response = requests.get(url)
data = response.json()
if data["cod"] != 200:
return f"Error: {data['message']}"
main = data["main"]
weather_desc = data["weather"][0]["description"]
temp = main["temp"]
return f"Weather in {city}:\n{weather_desc.capitalize()}, {temp}°C"
Step 4: Show the Notification
Now let’s display the weather using a desktop notification with the plyer library:
from plyer import notification
def notify_user(message):
notification.notify(
title="Weather Update",
message=message,
timeout=10 # seconds )
Step 5: Combine Everything
Put everything together in your main block:
if name == "main":
weather_report = get_weather(CITY)
notify_user(weather_report)
Now run the script:python weather_notifier.py
You’ll see a system notification pop up with the current weather.
Optional: Add Automatic Scheduling
If you want the app to check the weather every few hours, install the schedule library:pip install schedule
Then add this to your script:
import schedule
import time
schedule.every(3).hours.do(lambda: notify_user(get_weather(CITY)))
while True: schedule.run_pending()
time.sleep(1)
Bonus Ideas:
Add a GUI using Tkinter or PyQt
Fetch weather for multiple cities
Use geolocation to get weather automatically
Send weather via email or SMS using Twilio or SMTP
Conclusion:
You’ve just built a simple, useful Weather Notifier App in Python using real-time data from OpenWeatherMap. This project is a great introduction to working with APIs and automation in Python. It's easily extendable and can be personalized in countless ways.
Now you’ll never be caught without an umbrella again!
Comments
Join the discussionNo comments yet. Be the first to comment.