How I used Python to Automate my Daily Routine with Desktop Notifications

Project Description

Today while working on my Python code, I suddenly realized that it’s been two hours since I have been trying to solve the bug. I was constantly looking at the screen for two hours without even realizing to take a break or drink a glass drink water. That is when I thought of creating a script that would automate my daily routine with desktop notifications. I thought of creating a script that will generate a notification after certain time intervals for the following:

  • Drink 1 glass of water ( notify after every 1 hr)
  • Take a break from work (after every 1 hr )
  • Restart work ( after 10 mins of 2nd notification)
  • Medicine Notification at 10 PM every day
  • Take backup and end work ( at 8 PM ) every day

Therefore, in this project, we will be learning how to automate the daily routine using desktop popup notifications. So, without further delay, let’s dive into the steps to complete our project.

Step 1: Install and Import the Necessary Libraries

The two important libraries we need in this project are win10toast and schedule. The win10toast module is used to create the desktop notifications. It notifies us when any event occurs. Since it is not a built-in Python library, therefore you need to install it using PIP. Open your terminal and type the following command –

pip install win10toast

Once you have installed the win10toast module, go ahead and install the schedule library as follows:

pip install schedule

Schedule Library helps us to schedule a task at a particular time every day or even on a particular day. It matches our system’s time to the scheduled time set and then calls the command function. You will also need the help of the time module while scheduling the tasks. So, make sure that you also import the time module in your script.

import win10toast
import schedule 
import time 

πŸ–₯️ Recommended: Best Ultra-Wide Monitors for Programming: Top Picks for 2023

Step 2: Creating Notifications

I used the ToastNotifier class from the win10toast module to create an object and the method show_toast to create a notification. The following code will generate a notification that will stay on the screen for 10 seconds.

noti = win10toast.ToastNotifier()
noti.show_toast("Demo", "You will get a notification", duration = 10)

Explanation: The first argument in the show_toast method is the header of the notification and the second argument is the message that you want to display within the notification window. The duration (mentioned in seconds) specifies how long the notification will remain on the desktop screen before disappearing. 

Step 3: Scheduling the Tasks

Code:

schedule.every().hour.do(water)
schedule.every().hour.do(take_break)
schedule.every(10).to(15).minutes.do(work)
schedule.every().day.at("22:00").do(medicine)
schedule.every().day.at("20:00").do(backup)
while True:
    schedule.run_pending()
    time.sleep(1)

Explanation: As we need to generate the notifications after certain time intervals we would use the schedule module that will schedule the tasks as per the given/required intervals. We will also use the sleep method from the time module, to let the next line of code execute after some time specified in seconds.

Step 4: Creating the Task Functions

Next, I created different functions that will perform different tasks when called. The function to remind me to:

  • Drink a glass of water every 1 hour is as follows:
def water():
    noti.show_toast('Time to drink water!', duration= 10)
  • Take a break from work (after every 1 hr )
def take_break():
    noti.show_toast('You have been working since one hour. Time to take a break', duration= 10)
  • Restart work ( after 10 mins of 2nd notification)
def work():
    noti.show_toast('Restart your work', duration= 10)
  • Medicine Notification at 10 PM every day
def medicine():
    noti.show_toast('Time to take the medicine', duration= 15)
  • Take backup and end work ( at 8 PM ) every day
def backup():
    noti.show_toast('Take backup and end work', duration= 15)

Putting it All Together

Now let’s put it all together to visualize how the entire script works :

import win10toast
import schedule 
import time 
 
noti = win10toast.ToastNotifier()
 
def water():
    noti.show_toast('Time to drink water!', duration= 10)
 
def take_break():
    noti.show_toast('You have been working since one hour. Time to take a break', duration= 10)
 
def work():
    noti.show_toast('Restart your work', duration= 10)
 
def medicine():
    noti.show_toast('Time to take the medicine', duration= 15)
 
def backup():
    noti.show_toast('Take backup and end work', duration= 15)
 
schedule.every().hour.do(water)
schedule.every().hour.do(take_break)
schedule.every(10).to(15).minutes.do(work)
schedule.every().day.at("22:00").do(medicine)
schedule.every().day.at("20:00").do(backup)
 
while True:
  schedule.run_pending()
  time.sleep(1)

Conclusion

There we go! We have successfully created a wonderful automated script that will generate notifications for us, giving us prompts at certain intervals to organize our work and daily routine. I hope this project added some value and helped you in your coding quest. Stay tuned and subscribe for more interesting projects and tutorials.

Do you love automating tasks with Python? Well! I do. And if you are someone who likes automation then here’s a list of few mini projects that will get you going –