As an Amazon Associate I earn from qualifying purchases.
Working long hours on your computer? Need a little bit of wisdom or inspiration to get you through the day? The Motivational Desktop Notifier can help! Today, I will be taking you through how to set up a desktop notifier that notifies you every hour with a new motivational quote.
To start, you will need to install Plyer. Plyer is a platform-independent API to use features commonly found on various platforms, notably mobile ones, in Python. This tutorial works on Windows. macOS and Linux!
This can be installed using pip. If you need any help installing this package, check out this quick tutorial.
Now that Plyer is installed, you will need some quotes to display throughout the day. These quotes can be downloaded in JSON format with the link below:
Writing the code
Let’s dive into some code! Start by creating a new Python file and name it
Note: you will need to place
motivator.py in the same directory as the
Start by importing notification from
plyer – this is what we will use to send notifications to the desktop:
from plyer import notification
Since we are pulling our quotes from a JSON file, you will need to import the
import time import os import sys import random
Your Python code should now look like the following:
from plyer import notification import json import time import os import sys import random
Getting the quotes
Create a variable named
filename and set it equal to ‘
filename = 'quotes.json'
The next step is to open the file and read a random quote. Open the file using the following line:
with open(filename, encoding="utf8") as json_file:
Now, convert the JSON to a dictionary and assign it to a variable named
data (this will make it easier to randomly pick a quote):
data = json.load(json_file)
As we will want to continuously display motivational notifications, add an endless while loop:
Create a variable named
r and assign it to
random.randint using args
0 and the length of the
data-1 dictionary. This will pick a random number between zero and the length of the data dictionary:
r = random.randint(0,len(data) - 1)
A notification will need to have a title and a message. Let’s extract this information from the
data dictionary using
r as the random index:
title = data[r]['from'] message = data[r]['text']
Display the notification to the desktop using the
notification.notify method passing the title, message, and a timeout (the length of time you wish to display the notification for):
notification.notify( title = title, message= message, # displaying time timeout=10 )
Finally, add a sleep statement to make the notifier run once every hour:
Running the code as a background task
If you run the Python script we just created, you will see that notifications are displayed on screen every hour. You will want to run this Python script as a background process so that you can close your command line terminal without killing the Python script. To do this run the following command:
If you’re interested in learning Python I highly recommend this book. In the first half of the book, you”ll learn basic programming concepts, such as variables, lists, classes, and loops, and practice writing clean code with exercises for each topic. In the second half, you”ll put your new knowledge into practice with three substantial projects: a Space Invaders-inspired arcade game, a set of data visualizations with Python”s handy libraries, and a simple web app you can deploy online. Get it here.
Interested in creating your own algo trading bot? Check out my free series on algo trading here: https://www.conorjohanlon.com/category/algotrader/
As always, if you have any questions or comments please feel free to post them below. Additionally, if you run into any issues please let me know.