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.