Tooling Tuesday: Would you like some toast?

Howdy Doodly Doo! This time we take a look at a Python 3 library to create pop up notifications on your computer....And not an annoying talking toaster, honest.

If you like what you read...



Sorry to interrupt! But would you like to buy me a cup of coffee? It helps me to pay for hosting this blog, and to buy stuff to hack from Poundshops / Dollar Stores which are used in free projects on this blog. Thanks!

So what is it?



ptoaster is a Python 3 library to easily create pop up notifications for your projects. The project at the time of writing is only 5 days old and uses PySimpleGUI as the backbone to produce GUI elements on your desktop.

So how do I install it?

Via pip!

Linux / Mac

sudo pip3 install ptoaster

Windows

pip3.exe install ptoaster

So I've installed it, now what?

Ok lets do a quick hello world project to understand how it works.

In your favourite Python editor (I'm partial to VS Code these days) create a new file called ptoaster-test.py and use the following code.

import ptoaster
ptoaster.notify("Title: Hello World","Message: Visit bigl.es for hacks")



So in the above example we import the ptoaster library and then call a function called notify which takes two basic parameters. First is the title of the pop up, the second is the message. So in our example we have the title"Title: Hello World" and the message "Message: Visit bigl.es for hacks" which gave us the pop up in the image above. Nice and simple!

More options!



We can also add more parameters. For example here is the code to show an icon in the popup and set the opacity (enabling us to see through the pop up) alpha level. The alpha level 1 means that the pop up cannot be seen through, but 0.5 offers a translucent pop up, but is hard to read so use it with care.

import ptoaster
ptoaster.notify("Visit bigl.es for hacks","Lots of cool hacks with biglesp",icon="bigles.png",alpha=0.5)



This is a 40px icon image, the same used in my examples

For icons we need to use a GIF or PNG file around 40 x 40 pixels in size. Anything larger will make the text hard to read. Your code will need to be in the same location as the image, or you must use an absolute file path otherwise there will be an error.



We can set the location / position of the pop up too! The location uses a tuple to store the x and y co-ordinates for where we would like the pop up to appear. In the animation above you can see the image appear in the middle of an Amazon page on one of my two 1080p monitors. I used (960,540) to give me a rough centre position, but it looked a little off.

import ptoaster
ptoaster.notify("Visit bigl.es for hacks","Lots of cool hacks with biglesp",icon="bigles.png",alpha=1, location=(960,540))

Darth Fader...



If you would like to change the speed at which pop ups fade into view, then another parameter, fade_in_duration is used to set the time in seconds. For this example I wanted the pop up to immediately appear. No gentle fade in!

import ptoaster
ptoaster.notify("Visit bigl.es for hacks","Lots of cool hacks with biglesp",icon="bigles.png",alpha=1, fade_in_duration=0)

So what did I build with it?



Using ptoaster and fortune, a Python version of the fortune command for BSD/UNIX, I made a pop up fortune teller project.

I used an open source icon from Material Design to add a professional look to the pop up.

Here is the code

import ptoaster
import fortune
image = "face.png"
wisdom = fortune.get_random_fortune("/usr/share/games/fortunes/fortunes")
ptoaster.notify("Your fortune today is",wisdom,icon=image, alpha=1)

What could this be used for?

ptoaster would be useful for remote projects which need to send a message to the user on the main machine. For example an alarm system, temperature monitoring a greenhouse, server status alert.

Happy Hacking!