Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How I used Python to Automate my Daily Routine with Desktop Notifications

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

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1058079&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<figure class="wp-block-image size-large is-resized is-style-default"><img loading="lazy" decoding="async" src="https://blog.finxter.com/wp-content/uploads/2023/01/pexels-meruyert-gonullu-6589149-683x1024.jpg" alt="" class="wp-image-1058264" width="498" height="746" srcset="https://blog.finxter.com/wp-content/uploads/2023/01/pexels-meruyert-gonullu-6589149-683x1024.jpg 683w, https://blog.finxter.com/wp-content/uplo...00x300.jpg 200w, https://blog.finxter.com/wp-content/uplo...8x1152.jpg 768w, https://blog.finxter.com/wp-content/uplo...4x1536.jpg 1024w, https://blog.finxter.com/wp-content/uplo...5x2048.jpg 1365w, https://blog.finxter.com/wp-content/uplo...scaled.jpg 1707w" sizes="(max-width: 498px) 100vw, 498px" /></figure>
<h2><strong>Project Description</strong></h2>
<p>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:</p>
<ul>
<li>Drink 1 glass of water ( notify after every 1 hr)</li>
<li>Take a break from work (after every 1 hr )</li>
<li>Restart work ( after 10 mins of 2nd notification)</li>
<li>Medicine Notification at 10 PM every day</li>
<li>Take backup and end work ( at 8 PM ) every day</li>
</ul>
<p>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.</p>
<h2><strong>Step 1: Install and Import the Necessary Libraries</strong></h2>
<p>The two important libraries we need in this project are <code>win10toast</code> and <code>schedule</code>. The <code>win10toast</code> 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 <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-install-pip-on-windows/" target="_blank">PIP</a>. Open your terminal and type the following command – </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install win10toast</pre>
<p>Once you have installed the win10toast module, go ahead and install the <code>schedule</code> library as follows:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install schedule</pre>
<p><code>Schedule</code> 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 <code>command</code> function. You will also need the help of the <code>time</code> module while scheduling the tasks. So, make sure that you also import the <code>time</code> module in your script.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import win10toast
import schedule import time </pre>
<h2><strong>Step 2: Creating Notifications</strong></h2>
<p>I used the ToastNotifier class from the <code>win10toast</code> module to create an object and the method <code>show_toast</code> to create a notification. The following code will generate a notification that will stay on the screen for 10 seconds.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">noti = win10toast.ToastNotifier()
noti.show_toast("Demo", "You will get a notification", duration = 10)</pre>
<p><strong>Explanation: </strong>The first argument in the <code>show_toast</code> 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. </p>
<h2><strong>Step 3: Scheduling the Tasks</strong></h2>
<p><strong>Code:</strong></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p><strong>Explanation:</strong> As we need to generate the notifications after certain time intervals we would use the <code>schedule</code> module that will schedule the tasks as per the given/required intervals. We will also use the <code>sleep</code> method from the <code>time</code> module, to let the next line of code execute after some time specified in seconds.</p>
<h2><strong>Step 4: Creating the Task Functions</strong></h2>
<p>Next, I created different functions that will perform different tasks when called. The function to remind me to:</p>
<ul>
<li><strong>Drink a glass of water every 1 hour is as follows:</strong></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def water(): noti.show_toast('Time to drink water!', duration= 10)</pre>
<ul>
<li><strong>Take a break from work (after every 1 hr )</strong></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def take_break(): noti.show_toast('You have been working since one hour. Time to take a break', duration= 10)</pre>
<ul>
<li><strong>Restart work ( after 10 mins of 2nd notification)</strong></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def work(): noti.show_toast('Restart your work', duration= 10)</pre>
<ul>
<li><strong>Medicine Notification at 10 PM every day</strong></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def medicine(): noti.show_toast('Time to take the medicine', duration= 15)</pre>
<ul>
<li><strong>Take backup and end work ( at 8 PM ) every day</strong></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">def backup(): noti.show_toast('Take backup and end work', duration= 15)</pre>
<h2><strong>Putting it All Together</strong></h2>
<p>Now let’s put it all together to visualize how the entire script works :</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)
</pre>
<h2><strong>Conclusion</strong></h2>
<p>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 <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">subscribe</a></strong> for more interesting projects and tutorials.</p>
<p>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 –</p>
<ul>
<li><a href="https://blog.finxter.com/create-your-own-youtube-video-downloader/"><strong>Create Your Own YouTube Video Downloader</strong></a></li>
<li> <strong><a href="https://blog.finxter.com/the-power-of-automation-using-python-segregating-images-based-on-dimensions/" target="_blank" rel="noreferrer noopener">The Power of Automation Using Python – Segregating Images Based on Dimensions</a></strong></li>
<li><strong><a href="https://blog.finxter.com/automate-backup-to-google-drive-with-python/" target="_blank" rel="noreferrer noopener">Automate Backup to Google Drive with Python</a></strong></li>
<li><strong><a href="https://blog.finxter.com/receive-automated-email-notification-on-website-update/" target="_blank" rel="noreferrer noopener">Receive Automated Email Notification on Website Update</a></strong></li>
<li><strong><a href="https://blog.finxter.com/the-art-of-automating-whatsapp-messages-using-python/" target="_blank" rel="noreferrer noopener">The Art of Automating Whatsapp Messages Using Python</a></strong></li>
<li><strong><a href="https://blog.finxter.com/create-your-own-youtube-video-downloader/" target="_blank" rel="noreferrer noopener">Create Your Own YouTube Video Downloader</a></strong></li>
</ul>
<hr class="wp-block-separator has-alpha-channel-opacity" />
</div>


https://www.sickgaming.net/blog/2023/01/...fications/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016