Desktop notifier app using python

Published on . Written by

Desktop notifier app using python

Notifications help people to remember things. It is a small piece of text which appears on the desktop or mobile screen to inform the user about the updates or any other important pieces of information. This allows the user to focus on important things and ignore the non-important ones. The notifications are stored in the notification bar which you can refer after finishing your works. In this python project, you will learn to develop an application which will notify the user.


Skyfi Labs Projects
We will include the URL in the program from where the necessary information should be fetched. Before starting the project ensure you have installed the newest version of Python and install the libraries - notify2 and BeautifulSoup.

Read more..

SLNOTE
notify2 - Install this library using the following command

pip install notify2

BeautifulSoup - It is a library used to fetch data from the HTML and XML files. It helps the programmers to save a lot of time. BeautifulSoup is widely used to fetch information from the webpages.

In this python project, we are going to display the rate of bitcoins in INR. Following is the process followed to build the project:

Step1: Fetch the content to display

Step2: Set up the notifier

Step3: Display the content in the form of a notification

Let’s start with the scrapping process,


SLLATEST
Fetching the content

The first step is to import bs4-

from bs4 import BeautifulSoup

import requests

def fetch_bitcoin():

URL that we want to fetch the information is included.

url = "https://www.coingecko.com/en/price_charts/bitcoin/inr"

    headers = {'User-Agent': 'Mozilla/5.0'}

    bitcoin_file = requests.get(url)

The soup object is formatted.

    soup = BeautifulSoup(bitcoin_file.text, "html.parser")

    bitcoin_li = []

Now, we will get the necessary information from the website after observing the site structure.

    for table in soup.find_all("table", attrs={"class" : "table"}):

        for td in table.find_all("td"):

            bitcoin_li.append(td.text)

    del bitcoin_li[3:]

Let’s remove the unwanted characters from the list items.

    bitcoin_li = map(lambda s : s.strip(), bitcoin_li)

    return bitcoin_li

So far, we have created a program to fetch the bitcoin information from the given URL in the form of a list.

Setting up the notifier

We have fetched the required information to display let’s set up the notifier.

First, import the libraries.

import notify2

import Rates

Define the function

def notify():

    icon_path = "full path to the icon image"

Now fetch the bitcoin rate   

bitcoin = Rates.fetch_bitcoin()

Initialise the D-BUS connection. It is a message system which connects two applications to talk to each other. D-BUS connection for notify2 is initialized:

notify2.init("Cryptocurrency rates notifier")

Let’s create a notification object

n = notify2.Notification("Crypto Notifier", icon = icon_path)

Following is the structure of the notification object:

notify2.Notification(summary, message='body text', icon='icon path')

Set the importance level of the notification like - High, normal and low.

n.set_urgency(notify2.URGENCY_NORMAL)

Finally, set the timeout for the notification

n.set_timeout(1000)

Displaying the content

Now the notifier is ready, we will create the code to display the content.

result = ""

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

# Update the content

n.update("Current Rates", result)

# Show the notification

n.show()

First, The content is displayed with the help of list returned from the fetch_bitcoin function then we update the notifier with the required content and finally the content is displayed with the help of n.show().


SLDYK
Kit required to develop Desktop notifier app using python:
Technologies you will learn by working on Desktop notifier app using python:


Any Questions?


Subscribe for more project ideas