PYTHON

Create Amazing Progress Bars in Python with alive-progress

Have you ever needed a progress bar in your Python command-line application? One great way of creating a progress bar is to use the alive-progress package created by Rogério Sampaio de Almeida! Alive progress provides multiple different types of progress bars in your terminal or IPython REPL session. The alive progress package will work with any iterable, from lists to querysets, and more.

Let’s spend a little time learning how the alive-progress package works!

Installation

Installing the alive-progress package is easy using the pip installer utility. Here is the command you should use in your terminal:

python -m pip install alive-progress

Pip will install the package and any dependencies it needs. The pip tool shouldn’t take very long to install alive-progress.

Example Usage

The alive-progress package comes with a great demo that you can use to see all the different types of progress bars that the package supports. Open up a Python REPL and run the following code:

from alive_progress.styles import showtime

showtime()

When you run this code, you will see something similar to the following:

There is another alive-progress demo that is a little different from the one above. You don’t need to use a Python REPL to run it though. Instead, you can open up your terminal application and run the following command:

python -m alive_progress.tools.demo

When you run this command, you will see something like this:

The alive-progress GitHub page also shows several different code examples that demonstrate how to use alive-progress in your code. Here is one of the examples:

from alive_progress import alive_bar
import time

for x in 1000, 1500, 700, 0:
   with alive_bar(x) as bar:
       for i in range(1000):
           time.sleep(.005)
           bar()

Here you loop over four different integer values and create a progress bar for each of them. Then you loop over a range of one thousand and the progress bars will run through to completion.

When you run this code in your terminal, you will see this output:

Check out the GitHub repository for more fun examples!

Wrapping Up

The alive-progress package is lots of fun. You can add progress bars to any of your regular Python scripts and see them visually in your applications. This can be especially useful for command-line utilities that you create as they will show the user how far along they are in processing the data.

Download the package and start tinkering today!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button