Automating Desktop Notifications in Python with auto-notifier
Automating Desktop Notifications in Python with auto-notifier
When you work with multiple monitors, it’s easy to start a long-running Python task on one screen and switch to something else. I often run data processing or training scripts in a terminal while focusing on another task. The problem is simple: unless I keep checking back, I don’t know if the script finished or crashed.
auto-notifier was built for that case. It wraps your function or code block, detects whether it completed or failed with an exception, and sends a desktop notification. It relies on two familiar Python constructs — decorators and context managers — and stays out of your way.
[ Placeholder for Screenshot of Notification ]\ Example: Desktop notification popup after script completion
🚀 Installation
pip install auto-notifier
auto-notifier uses notify-py internally to send native system notifications (Windows, macOS, Linux).
🧭 How It Works
| Interface | Best For |
|---|---|
@notify_when_done | Monitoring a function |
notify_on_complete() | Monitoring a code block |
Both detect:
- ✅ Successful completion
- ❌ Exceptions (with failure alert)
- ⏱️ Execution time (included internally)
Exceptions are not suppressed — you still see tracebacks in the terminal.
🧩 Using the Decorator
from auto_notifier import notify_when_done
import time
@notify_when_done(
title="Backup complete",
message="Nightly backup finished successfully.",
error_title="Backup failed"
)
def run_backup():
time.sleep(5) # simulate work
# raise Exception("Disk error") # test failure
return "ok"
run_backup()
What happens:
- Success → "Backup complete" notification
- Error → "Backup failed" notification, then exception re-raised
📎 Using the Context Manager
from auto_notifier import notify_on_complete
import time
with notify_on_complete(title="Data import finished"):
time.sleep(3)
print("Import complete")
Wraps any block of code — ideal for scripts or notebooks.
🎛️ Customizing Notifications
| Parameter | Description |
|---|---|
title | Success notification title |
message | Body text on success |
error_title | Failure notification title |
error_message | Body text on error |
icon_path | Custom icon for notification |
app_name | Name shown in notification center |
Example with icon:
@notify_when_done(
title="Report generated",
message="Monthly metrics ready.",
icon_path="icons/chart.png"
)
def build_report():
...
🛠 How It Uses Decorators & Context Managers
- Decorator: Wraps the function, measures runtime, catches exceptions, triggers notification.
- Context Manager: Uses
__enter__/__exit__, ensures notification even if an exception occurs.
No configuration files, threads, or services — just Python control flow.
🔁 Combined Example (Pipeline Style)
from auto_notifier import notify_when_done, notify_on_complete
import pandas as pd
import time
@notify_when_done(title="Data cleaned", error_title="Cleaning error")
def clean_data(path):
df = pd.read_csv(path)
time.sleep(4)
return df.dropna()
with notify_on_complete(title="ETL pipeline complete"):
cleaned = clean_data("raw.csv")
cleaned.to_csv("cleaned.csv", index=False)
🧭 When It’s Useful
- Long-running terminal scripts
- Data processing / scraping tasks
- Training loops
- Backups or exports
It’s built for local desktop usage, where system notifications are visible.
📌 Summary
auto-notifier adds notification hooks to Python code using decorators or context managers.\
It lets you step away from the terminal and still know exactly when a script finished or failed.
PyPI: https://pypi.org/project/auto-notifier/\ GitHub: https://github.com/yalmeidarj/auto-notifier
If you launch scripts and move on to other tasks, this tool keeps you informed without manual checks.