Day 1: Mastering Notifications with Python

🎄 Welcome to the Python Advent Calendar! 🎄

Every day till Xmas, we'll open a new door to Python secrets.

Today, let's explore how to send notifications using Python.
Whilst your code is running, you could use your newfound downtime to make a cup of tea reply to that email 📧 share this newsletter on LinkedIn or Twitter 📣 and make someone’s day ❤️

1️⃣ jupyterlab_notify: Desktop alerts from Jupyter

📆 Last updated: May 2023
⬇️ Downloads: 106/week
🐍 PyPI | ⭐ GitHub Stars: 5

🔍 What is it?

Trigger desktop notifications using a Jupyter cell magic to alert you when a cell has completed execution. Inspired by the earlier jupyter-notify (notebook only, and last updated April 2021) and adapted to work with Jupyter Lab.

📦 Install

pip install jupyterlab-notify

🛠️ Use

First register the magic:

%load_ext jupyterlab_notify

Then trigger alerts using the %%notify magic:

%%notify
import time
time.sleep(3)

2️⃣ pync: macOS alerts from Python

 📆 Last updated: Jan 2022
⬇️ Downloads: 455/week
🐍 PyPI | ⭐ GitHub Stars: 353

❓ What about Linux & Windows?

Check out plyer - it’s actively maintained and exposes a huge array of native APIs on Windows, Linux, macOS, Android and iOS including notifications, audio recordings, camera access, GPS, native file chooser, keystores, and more.

🔍 What is it?

Triggers macOS notifications with customisable callback actions by wrapping the terminal-notifier command-line tool.

📦 Install

pip install pync

🛠️ Use

import pync

# Notification from "Terminal"
pync.notify("Hello from Terminal")

# Notification from "Python"
pync.notify("Hello from Python", title='Python')

# Click the notification to open Safari
pync.notify("Click me!", activate='com.apple.Safari')

# Click the notification to open a URL
url = "https://github.com/CoefficientSystems/python-advent-calendar"
pync.notify("Check out our GitHub repo!", open=url)

# Make macOS talk
joke = "Knock knock. Race condition. Who's there?"
pync.notify("Want to hear a joke?", execute=f'say "{joke}"')

3️⃣ Pushover: Send notifications to your mobile

📆 Last updated: June 2023
⬇️ Downloads: 91/week
🐍 PyPI | ⭐ GitHub Stars: 23

🔍 What is it?

Use the pushover.net service to deliver notifications in real-time to the Pushover app on iOS, Android, or desktop client. The Pushover service has a one-off cost of $5 per platform to cover service costs. Their website states: “We don't show ads in any of our apps or notifications, and we don't sell any of your data.”

📦 Install

  1. Install the Pushover app on your mobile device.

  2. Sign up to try it free for 30 days and go to https://pushover.net/apps/build to get your account details.

  3. Verify your email address to create an API token.

  4. pip install pushover

🛠️ Use

First create a file called credentials.pushover

[pushover]
app_key = ###############################
user_key = ##############################

Then you can start firing notifications to your mobile device:

from pushover import PushoverClient

client = PushoverClient('credentials.pushover')
client.send_message("☕ Your tea is ready.")

📺 Stay Tuned for More! 👀

Every day is a new surprise in our Python Advent Calendar. Tomorrow, we'll delve into another exciting Python topic. Until then, check out our python-advent-calendar GitHub repo full of code examples from every newsletter.

Happy Coding! 🐍

Your Python Advent Calendar Team 😃 

🤖 Python Advent Calendar is brought to you by Coefficient, a data consultancy with expertise in data science, software engineering, devops, machine learning and other AI-related services. We code, we teach, we speak, we’re part of the PyData London Meetup team, and we love giving back to the community. If you’d like to work with us, just email [email protected] and we’ll set up a call to say hello. ☎️

P.S. We love feedback! Did you like today's content? Did we miss a good Python package? Is there a package or tool or top tip you think we should feature? Whatever it is, reach out to us. Here’s a link to our privacy policy.