Hi there! Let’s create cool progress bars using Python. We will be using Python 3.8.10 on Windows 10. Let’s go! ✨⚡🔥
First, let’s install the alive-progress library with this simple command:
pip install alive-progress
Now that we have installed the library, let’s write our code:
from alive_progress import alive_bar
import time
steps = [1,2,3,4,5,6,7]
with alive_bar(total=len(steps), title="Operation X", theme='smooth') as bar:
for s in steps:
bar.text = f"Working on Step {s}..."
time.sleep(2)
bar()
Let’s explain what is happening here:
- We import our alive-progress library and the time library obviously.
- We define a list steps which contains 7 numbers, 1 to 7 inclusive. This list represents a list of steps of some arbitrary operation. We will use our progress bar to show the status of the completion of each step. In practice, this will be a typical use case for a progress bar.
- We initialize our bar with the method alive_bar() which was imported earlier.
- parameter total is the total length of progress bar, equal to the number of steps in our operation
- parameter title is the name of our progress bar, it will be displayed to the left of the progress bar
- parameter theme is the look of the progress bar, can be one of 4 values: smooth, classic, scuba, musical
- We define a for loop that will iterate through the steps list and increment the progress bar as each step s is completed. In this instance we are simply calling sleep() to pause the program for 2 seconds for each step. We are also displaying a line of text indicating what step we are currently working on.
- The function bar() will update the displayed progress bar with the new state after each iteration of the inner for loop.
When the program executes, we should the see the following if all goes well:
Let’s do another example:
from alive_progress import alive_bar
import time
steps = [1,2,3,4,5,6,7]
with alive_bar(total=len(steps), title="Operation X", bar='halloween',spinner='elements',dual_line=True) as bar:
for s in steps:
bar.text = f"Working on Step {s}..."
time.sleep(2)
bar()
This is a more fancy example where we added two new parameters to the alive_bar() method: bar and spinner. These parameters add styles to the progress bar. The bar is the actual progress bar itself and the spinner is the little animation to the right of the progress bar that indicates work is taking place in between the ticks of the bar. When the above code executes we will get the following:
If we want a demo of all the potential themes, bars or spinners available we can execute the following code:
from alive_progress.styles import show_bars, show_spinners, show_themes
#Hit Ctrl-C to continue
show_bars()
show_spinners()
show_themes()
When the above code executes you will see an impressive display of all the bars, spinners and themes the library has to offer. You will need to hit Ctrl-C on your keyboard to continue.
So there you have it! A cool progress bar in Python for your projects. Thanks for reading! 👌👌👌