Simple Progress Bar-Integrate with any python project in a few seconds

Tarun Kumar
Analytics Vidhya
Published in
5 min readJun 19, 2020

--

Photo by Pankaj Patel on Unsplash

Why do you need a progress bar? Yeah, sometimes you need to monitor the progress of the task. Most of the time I find issues building the progress bar from scratch. Sometimes a very basic one can do the job, but not always. Have you ever used Keras (Machine Learning API for Python)? It has its own custom progress bar to give a good idea of the training. When I started working with PyTorch, I feel something is missing, because every time I need to build a progress monitor. So, I decided to build a package a simple one but can do a lot very easily.

Today, I will guide you on how to use my progress bar. You can find the documentation and package on my GitHub repository. I have tried to keep things simple and very intuitive.

You can use this for many different usages but I have built this for progress monitoring with PyTorch.

Installation

Just need a file progress.py into your working directory. Get the file from GitHub.

Note: I will soon make the python package available through pip install.

How To Use

I am going to explain through two simple examples. Further documentation can be found on my GitHub.

  1. EXAMPLE 1: Suppose you need to process the images and you have 2478 images to process. Let’s see how you can make a simple progress monitor as in the figure below.
Option 1
Option 2
Code for Option 1

Setting up a progress bar involved a few steps. Create a progress bar object, create different Element instances as per requirement, compile all the elements, and use the compiled progress bar where you want.

Line 6: Create an instance of a progress bar. Mode can be barand no-bar .

Line 9: Create a single Element as required in this example. The first parameter of the element is the name to show for the progress monitor and the second parameter takes the initial value of the Element.

For option 2: you just need to add the Element parameters like this. counter = P.Element("Images Processed", max_value=images, value_display_mode=1)

Read Documentation for more parameters on GitHub.

Compiling is very easy just like stacking the Elements in order. One more thing to note, you can add a custom string in between the element stack. You can choose from 2 different Syntax as shown below:

p = p(Element1)(Element2)...("String is also allowed")...(ElementN)

OR

p = p + Element1 + Element2 + ...+ "String is also allowed" + ...+ (ElementN)

Line12: Compile the single element.

Now you are ready to plug the custom made progress monitor into your application to track the progress. Just you need to initialize the progress bar before using it and then update it at each step.

Updating values of the Elements also very easy just follow the syntax Element1(updated_value) . After updating the values of all the elements you need to render the progress bar to show up with the updated value that can be done by calling p.update(step =1) . That’s it.

2. EXAMPLE 2: Suppose you want to create a progress bar for training progress in PyTorch. So, let’s create a progress bar like below in this example.

First thing first, decide what elements you need.

Elements:

  1. Epoch (To track each epochs)
  2. Bar (Predefined element)
  3. Batch (Progress of batches in each epoch)
  4. ProgressTime (Predefined element) (Time for each epoch)
  5. Loss (To track loss during training)
  6. Acc (To track accuracy during training)

See the code and try to understand it. Most of the things are the same as before but few things I will cover which I have not discussed yet.

Creating Elements and compiling progress bar.

See the mode in line 1 has been changed to bar. Here I have defined 6 Elements (4 custom elements and 2 predefined elements). Bar and ProgressTime elements are predefined elements as shown in the example code. Bar elements can take a few parameters as well, check the documentation on GitHub. ProgressTime takes only one parameter postfix any string you want to add after the time otherwise leave it blank.

The compiling part is the same as the previous example. You may check the final format of the progress bar after compiling by calling p.get_format() .

Initializing and updating progress bar (Progress bar in use)
Final Result

In this part, everything works the same except line 23. If you don't set the new cursor position the progress for the new epoch will be overwritten on the old one. If you want to save the progress for each epoch then you need to call p.set_cursor_postion() after the completion of each epoch.

That’s it 😎. I hope this post helps you. Please share if you find my work helpful.

Full Code and Documentation

Find full code on my GitHub: https://github.com/tarunk04/progress-bar-python/

Feedback

That’s all for Today’s Post, if you have any questions please let me know in the comments. Also, if you found any error in the post please write to me at tarun12.tarunkr@gmail.com.

If you want me to cover some specific topics in the upcoming posts please let me know in the comments.

Follow:

Thank You for your the time to read this article. Please share it if you like.

I am looking for people with different skill sets to build a closed community so that we can build together awesome products. If you are interested reach me out on Linkedin or mail me tarun12.tarunkr@gmail.com

--

--