Building a desktop notification tool for Linux using python

Published Aug 27, 2017Last updated Feb 23, 2018
Building a desktop notification tool for Linux using python

What is a desktop notification ?

The term desktop notifications refer to a graphical control element that communicates certain events to the user without forcing them to react to this notification immediately. In other words, it is a simple application which produces a notification message in form of a pop-up message on desktop.

Why use desktop notifications ?

There are many reasons that a computer may want to notify you of things not directly related to what you are doing right now. Software updates may be available; you may have received a new instant message; your 300-page print job may have finally finished; you may have only ten minutes of battery left. Sometimes these events need a response or at least acknowledgement, or don’t need to be read immediately, in which case they should be presented in an alert box or some other window.

For cases where a notification is best read immediately but there is no applicable response, however, the most appropriate mechanism is a non-interactive notification bubble.

For more information regarding the use cases and structural details please refer to this link.


Now, in order to create a desktop notifier, you need to install a third party Python module, notify2.

You can install notify2 using a simple pip command:

pip install notify2

Example - Crypto-Notifier

Here , we are going to build a notification tool which displays the current rate of bitcoins in INR.

The entire implementation for the notification tool has been divided into the following phases:

  1. Getting the content to be displayed.
  2. Setting up the notifier.
  3. Displaying the content in the form of a notification bubble.

Getting the content

For getting the current rate of bitcoins in INR, we can scrape its price from the web. Here we will scrape the result using BeautifulSoup.

The page structure is shown in the following snippet:

Screenshot from 2017-08-27 11-57-51.png

The following code snippet gives the details of the entire scraping process:

from bs4 import BeautifulSoup
import requests

def fetch_bitcoin():
  # URL to fetch the latest price in INR
    url = ""
    headers = {'User-Agent': 'Mozilla/5.0'}
    bitcoin_file = requests.get(url)
    # Forming the soup object
    soup = BeautifulSoup(bitcoin_file.text, "html.parser")

    bitcoin_li = []

  # Getting the necessary details from the page by observing the structure
    for table in soup.find_all("table", attrs={"class" : "table"}):
        for td in table.find_all("td"):

    del bitcoin_li[3:]
    # Removing unnecessary characters from the list items
    bitcoin_li = map(lambda s : s.strip(), bitcoin_li)
    return bitcoin_li

Here we have simply implemented a function which returns the bitcoin details in INR in the form of a list after scraping the given url.

Setting up the notifier

After getting the neccesary information to be displayed, we initialise the notification tool. The following code snippet explains this process:

import notify2
import Rates

def notify():

    icon_path = "full path to the icon image"
    # Fetch the current bitcoin rate
    bitcoin = Rates.fetch_bitcoin()
    # initialise the d-bus connection
    notify2.init("Cryptocurrency rates notifier")
    # create Notification object
    n = notify2.Notification("Crypto Notifier", icon = ICON_PATH)
    # Set the urgency level
    # Set the timeout

Let us try to analyse the code step by step:

  • Before we can send any notifications, we need to initialize a D-Bus connection. D-Bus is a message bus system, a simple way for applications to talk to one another. So, D-Bus connection for notify2 in current Python script is initialized using:
notify2.init("Cryptocurrency rates notifier")
  • Now, we create a notification object using:
n = notify2.Notification("Crypto Notifier", icon = icon_path)

The general structure of a notification object is:

notify2.Notification(summary, message='body text', icon='icon path')
  • We can optionally set the urgency level of notifications using set_urgency method:

Other available constants are notify2.URGENCY_LOW and notify2.URGENCY_HIGH

  • We can also set the display duration using the timeout utility:

Displaying the content

Once the notifier has been setup, we can use it to display the notifcations to the user. The following code gives a brief overview of this:

result = ""
result = result + str(bitcoin[0]) + "  -  " + str(bitcoin[2].encode('utf-8')) + "\n"

# Update the content
n.update("Current Rates", result)

# Show the notification

Let us analyse the code step by step:

  • In the first step, we basically form the content to be displayed by using the list returned from the fetch_bitcoin function.
  • In the next step, we update the notifier with the required content.
  • Finally, we display the notification using utility.

The output of the entire process is :

Screenshot from 2017-08-27 12-05-41.png

Github repository for this desktop notifier application: Crypto-Notifier.

Feel free to fork, suggest changes, or point out bugs!

Hope you found this helpful 😄

Discover and read more posts from Dushyant Rathore
get started