Yuri Almeida
Web Developement

Automating Desktop Notifications in Python with auto-notifier

Yuri Almeida
<!DOCTYPE html>

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

InterfaceBest For
@notify_when_doneMonitoring 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=&quot;Backup complete&quot;,
    message=&quot;Nightly backup finished successfully.&quot;,
    error_title=&quot;Backup failed&quot;
)
def run_backup():
    time.sleep(5)  # simulate work
    # raise Exception(&quot;Disk error&quot;)  # test failure
    return &quot;ok&quot;

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=&quot;Data import finished&quot;):
    time.sleep(3)
    print(&quot;Import complete&quot;)

Wraps any block of code — ideal for scripts or notebooks.


🎛️ Customizing Notifications

ParameterDescription
titleSuccess notification title
messageBody text on success
error_titleFailure notification title
error_messageBody text on error
icon_pathCustom icon for notification
app_nameName shown in notification center

Example with icon:

@notify_when_done(
    title=&quot;Report generated&quot;,
    message=&quot;Monthly metrics ready.&quot;,
    icon_path=&quot;icons/chart.png&quot;
)
def build_report():
    ...

🛠 How It Uses Decorators &amp; 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=&quot;Data cleaned&quot;, error_title=&quot;Cleaning error&quot;)
def clean_data(path):
    df = pd.read_csv(path)
    time.sleep(4)
    return df.dropna()

with notify_on_complete(title=&quot;ETL pipeline complete&quot;):
    cleaned = clean_data(&quot;raw.csv&quot;)
    cleaned.to_csv(&quot;cleaned.csv&quot;, 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.