Signup/Sign In

Desktop Notifier Application Python Project

Desktop Notifier Application Python Project

Have you ever attempted to design a desktop notification program that meets your specific requirements? Achieve you know that you can do this using Python in just a few steps?

Don't worry, we'll start from the beginning; in this post, we'll create a desktop notification application for tracking the coronavirus's daily statistics.

What You'll Discover In This Article

  • Installing the Python packages that are necessary.
  • Obtaining coronavirus info via the internet.
  • Making a desktop notification program.
  • Making your software run in the background is a great way to save time.

Let's get going.

We need to obtain two vital python packages for this application while installing needed python packages.

Note: If you're using Windows, execute these two instructions at the command prompt; if you're using Linux, type them in the terminal (for fetching data from the web)

pip install requests
pip install plyer

We may get coronavirus data from the web by using the URL supplied below; you are free to substitute the nation name with your own, but we will be utilizing India's coronavirus data for this application.

https://corona-rest-api.herokuapp.com/Api/india

Now that we have all of the tools we need to construct this app, let's get to work coding it.

Note: It will be easier if you code this in offline compiler rather than online compiler because we will be making this application run as a background process in your PC in the later stages of this article; if you code in online compiler, you will need to download the file, which is not required in offline compiler. I recommend that you use Visual Studio.

Step 1: Importing Libraries

import datetime #for reading present date
import time 
import requests #for retreiving coronavirus data from web
from plyer import notification #for getting notification on your PC

Step 2: Retrieving the Data From The Web

#let there is no data initially
covidData = None
try:
    covidData = requests.get("https://corona-rest-api.herokuapp.com/Api/india")
except:
    #if the data is not fetched due to lack of internet
    print("Please! Check your internet connection")

Step 3: Creating Custom Notification

#if we fetched data
if (covidData != None):
    #converting data into JSON format
    data = covidData.json()['Success']
    
    #repeating the loop for multiple times
    while(True):
        notification.notify(
            #title of the notification,
            title = "COVID19 Stats on {}".format(datetime.date.today()),
            #the body of the notification
            message = "Total cases : {totalcases}\nToday cases : {todaycases}\nToday deaths :{todaydeaths}\nTotal active :{active}".format(
                        totalcases = data['cases'],
                        todaycases = data['todayCases'],
                        todaydeaths = data['todayDeaths'],
                        active = data["active"]),  
            #creating icon for the notification
            #we need to download a icon of ico file format
            app_icon = "Paomedia-Small-N-Flat-Bell.ico",
            # the notification stays for 50sec
            timeout  = 50
        )
        #sleep for 4 hrs => 60*60*4 sec
        #notification repeats after every 4hrs
        time.sleep(60*60*4)

That's it; we're ready to launch our program. Before we do, however, you should be aware of several adjustments you may make to tailor your application to your specific requirements.

timeout — specifies how long a notice should appear on the desktop.
sleep ()— specifies the time period after which the notice should appear.

After you've ran your app, this is how you'll receive your notice.

Having your application operate in the background is a great way to save time.

Source Code for Desktop Notifier Application

import datetime #for reading present date
import time 
import requests #for retreiving coronavirus data from web
from plyer import notification #for getting notification on your PC
#let there is no data initially
covidData = None
try:
    covidData = requests.get("https://corona-rest-api.herokuapp.com/Api/india")
except:
    #if the data is not fetched due to lack of internet
    print("Please! Check your internet connection")
#if we fetched data
if (covidData != None):
    #converting data into JSON format
    data = covidData.json()['Success']
    
    #repeating the loop for multiple times
    while(True):
        notification.notify(
            #title of the notification,
            title = "COVID19 Stats on {}".format(datetime.date.today()),
            #the body of the notification
            message = "Total cases : {totalcases}\nToday cases : {todaycases}\nToday deaths :{todaydeaths}\nTotal active :{active}".format(
                        totalcases = data['cases'],
                        todaycases = data['todayCases'],
                        todaydeaths = data['todayDeaths'],
                        active = data["active"]),  
            #creating icon for the notification
            #we need to download a icon of ico file format
            app_icon = "Paomedia-Small-N-Flat-Bell.ico",
            # the notification stays for 50sec
            timeout  = 50
        )
        #sleep for 4 hrs => 60*60*4 sec
        #notification repeats after every 4hrs
        time.sleep(60*60*4)

Output:

Desktop

What is the best way to have a Python program run in the background?

Simply execute this command in command prompt in Windows or terminal in Linux to have your program operate in the background. Note that you must type this command in command prompt in Windows and terminal in Linux.

Note: your-file-name-here> should be replaced with pythonw.exe.your-file-name-here>.

pythonw.exe .\<your-file-name-here>
example
pythonw.exe .\desktopNotifier.py

That's all there is to it; your program will now operate in the background.

How Can I Turn Off Notifications?

It's as easy as killing the python process in Task Manager. If you have any trouble stopping the notice, please share your experience in the comments area of this page.
There are a variety of other situations in which you may use this strategy.

  • Every day, you will be reminded to take your medication.
  • The need to drink water is reminded every hour.

and many more; how you utilize this program is entirely up to you.

Conclusion

This program is compatible with any operating system, including Windows, Linux, and Mac. Please feel free to ask in the comments area of this post if you desire a simpler desktop notification program.