Desktop notifications are used to inform the user about an event or display some form of information without getting in the user’s way. Notifications are usually small pop up windows that disappear after a short while. There are a number of ways to display notifications using Python, but most of these depend on some GUI library. The focus of this article is on how to send notifications to the desktop from a commandline application or script without using a GUI toolkit in Ubuntu.
notify-send is a program used to display notifications sent from the command line. It’s usually installed by default but if it isn’t you can get it by installing the “libnotify” package from your repositories. Sending a notification with notify-send is really easy, type this into your terminal to send a notification:
notify-send "Title" "Message"
As you can see from the screenshot, “Title” represents the notification’s summary or title and the “Message” is the information you want the user to see. It is also possible to add pretty icons to the notifications. The icons can be emotes, system images or your own stock images. Here are a few examples:
notify-send -i face-crying "System Malfunction" "Something bad happened."
notify-send -i weather-storm "Storm Alert" "There's a storm coming your way."
notify-send -i lock-screen "Password Required" "You need a password to access this device."
-i flag allows you to specify what icon to use in a notification.
notify-send supports more options for notifications such as urgency levels and setting how long a notification should be displayed for.
Using Python to send the notifications.
To send notifications from Python, call the
notify-send program and pass the options as a list.
import subprocess as s s.call(["notify-send", "-i","face-laugh","hello"])
This page maintains a list of icons you can use in your applications.
That’s it! If there is an easy way to send notifications in Windows and Mac, let me know in the comments below so that I write articles about how to send notifications in those OSes too.