/ python

Tuesday Tooling: Custom Python Spinners

Tooling Tuesday is a new series of short, to the point blog posts that highlight a fun and useful tool for developers, coders, makers, hackers and of course system administrators.

For the first in the series of Tueday Tooling blog posts, I have chosen to highlight a rather fun and useful Python library that can provide meaningful and expressive interaction to the user.

So tell me more

Spinners, no not those fidget spinners, but a way in which a process or application can show you that it is working on something.

Created by Manraj Singh, Halo is nothing to do with Master Chief, and more about giving the user useful output while a script is running.

Halo?alt

Requirements

  • A *nix system, Halo doesn't work with Windows...yet.
  • It will work on a Raspberry Pi, and a big powerful *nix server

I rather like Halo it is really easy to install.

How do I install it?

From a terminal type...

sudo pip3 install halo

And it can be dropped into your Python script rather easily.

from halo import Halo

spinner = Halo(text='Loading', spinner='dots')
spinner.start()

# Run time consuming work here
# You can also change properties for spinner as and when you want

spinner.stop()

Each "spinner" can be configured to deliver a set message and this is set by adding text='something'. The spinner animation can then be selected from a rather long JSON file of options, try monkey its fun but a bit useless. The spinner is then started, and it runs while your Python sequence is running. Then all you need to do is stop the spinner when you are ready.

Lets take it for a spin.

I wanted to test how Halo could be used so I wrote a quick project that can be run from the terminal. It is really a wrapper for ping used to check if a site or IP address is up. The project just shows how Halo can be used to pep up a project.
The code can be run from the terminal, and from there you can pass an IP address of URL as an argument. This is then used to run a ping test on the URL/IP address and a result is displayed.
The running project looks like this.
alt
And as you can see the project runs and shows a quick "progress bar" that illustrates the application is loading, then the project advises the user on the status of a URL/IP using a green tick for an active site, and a red cross if the site is down. Simple things but handy for fast confirmnation.

Here is all of the code for the project.

#!/usr/bin/env python3
from halo import Halo
from time import sleep
import os
import sys

spinner = Halo(text='Starting the ping application', spinner='growHorizontal')
spinner.start()
sleep(2)

spinner.stop()
spinner.succeed("Pinger initialisation complete")
response = os.system("ping -c 1 " + sys.argv[1])
if response == 0:
    print("============")
    spinner.succeed(sys.argv[1]+" is up")
    print("============")
else:
    print("============")
    spinner.fail(sys.argv[1]+" is down")
    print("============")

Conclusion

Halo is a great tool for sysadmins needing a quick and easy method to create informative and appealing outputs to the user. It is also a fun way to provide visual flair to your code, and could be used for a visual debugging tool in your next Python project.

Code for this project

All of the code that I have written for this post can be found on my GitHub page.