Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Display a Progress Bar in Python
1
How to Display a Progress Bar in Python

5/5 – (1 vote)

[[Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png]] 100%

Problem Formulation and Solution Overview

In this article, you’ll learn how to configure and display a progress bar.

A progress bar is commonly used in Python or, for that matter, any other programming language to show the user an application’s progress. For example, an installation, a transferring of files, or any other commands.

The Finxter Academy recommends implementing this to visually display to the user what is happening behind the scenes.

[Image: 1f4ac.png] Question: How would we write code to display a progress bar?

We can accomplish this task by one of the following options:

Method 1: Use a For Loop

[Image: progress_bar.gif]

This method imports the
Code:
time
and
Code:
sys
libraries combined with a
Code:
for
loop to display a custom progress bar output on a single line.

Code:
from time import sleep import sys for x in range(0,21): sys.stdout.write('\r') sys.stdout.write("[%-20s] %d%%" % (''*x, 5*x)) sys.stdout.flush() sleep(0.75)

Above imports, the
Code:
time
library to call the sleep command and the
Code:
sys
library to write the contents to the terminal.

Next, a
Code:
for
loop is instantiated. This loop uses the
Code:
range
function to set the start position (0 by default) and the stop position (21-1). Inside this loop, the following occurs:
  • The first line uses a carriage return (
    Code:
    \r
    ) to start the code on a new line.

  • The following line determines how the progress bar appears by:
    • Left-aligning the emoji(s) inside the square brackets
      Code:
      [%-20s]
      .

    • Configuring the progress percentage
      Code:
      %d%%" %
      (example
      Code:
      15%
      ).

    • Determining the visual emoji progress
      Code:
      (':beaming_face_with_smiling_eyes:'*x, 5*x)
      .

  • Then
    Code:
    sys.stdout.flush()
    writes everything in the buffer to the terminal.

  • The code pauses for the stated period (
    Code:
    0.75
    ).

The loop continues until 100% has been attained. Below is the end result.

[[Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png][Image: 1f601.png]] 100%

[Image: 1f4a1.png]Note: Any emoji or another symbol can be substituted for the above selection.

Method 2: Use apytl Library

This method imports the
Code:
time
and
Code:
apytl
libraries to generate a custom progress bar.

To run this code error-free, install the required library. Click here for installation instructions.

Code:
import time import apytl total_iterations = 10 for index, value in enumerate(range(total_iterations)): apytl.Bar().drawbar(value, total_iterations, fill='*') time.sleep(0.75)

Above imports the
Code:
time
library to call the sleep command and the
Code:
apytl
library to display the progress bar.

Next, the total number of iterations to carry out is declared as
Code:
10
and saved to
Code:
total_iterations
.

To understand what is going on in the
Code:
for
loop, let’s write some test code to see what the values of
Code:
index
,
Code:
value
and
Code:
total_iterations
are doing:

Code:
total_iterations = 10 for index, value in enumerate(range(total_iterations)): print(index, value, total_iterations)

As you can see from the output, the
Code:
index
and
Code:
value
variables count from 0 (
Code:
range
default start position) to the stop position of 9 (10-1). Notice the value of
Code:
total_iterations
does not change.

Code:
0 0 101 1 102 2 103 3 104 4 105 5 106 6 107 7 108 8 109 9 10

Referring back to the code directly below the Method 2 heading, the next line is:
Code:
apytl.Bar().drawbar(value, total_iterations, fill='*')
.

This function is passed three (3) arguments:
  • The variable
    Code:
    value
    , which you can see from the above, increments by one (1) each iteration.

  • The variable
    Code:
    total_iterations
    , which remains constant at
    Code:
    10
    .

  • The fill value. This example uses the asterisk (
    Code:
    *
    ) character. However, feel free to use an emoji or other symbol.

Progress |***********************************| 100.0% Complete

Method 3: Use alive-progress

The
Code:
alive-progress
library moves progress bars to the next level! With its many display options, it’s a must-a-try!

To run this code error-free, install the required library. Click here for installation instructions.

Code:
from alive_progress import alive_bar from time import sleep for x in range(10): with alive_bar(x, bar='solid') as bar: for i in range(x): sleep(.001) bar()

Above imports the
Code:
alive-progress
library to use the progress bar and the
Code:
time
library to call the
Code:
sleep
command.

Next, a
Code:
for
loop is instantiated. This loop uses the
Code:
range
function to set the stop position at 9 (10-1). Inside this loop, the following occurs:
  • The
    Code:
    alive_bar
    is called and passed the argument
    Code:
    x
    and the bar type. For this example,
    Code:
    solid
    was selected.
    • A new
      Code:
      for
      loop is instantiated and loops through
      Code:
      range
      passing
      Code:
      x
      as an argument.

    • The code halts (
      Code:
      sleep
      ) for the stated period (
      Code:
      .001
      ).

    • The bar is output to the terminal each iteration.

The loop continues until 100% has been attained.

<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 0 in 0.0s (0.00/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 1/1 [100%] in 0.0s (333.28/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 2/2 [100%] in 0.0s (62.48/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 3/3 [100%] in 0.0s (63.96/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 4/4 [100%] in 0.1s (62.39/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 5/5 [100%] in 0.1s (63.32/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 6/6 [100%] in 0.1s (64.84/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 7/7 [100%] in 0.1s (63.79/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 8/8 [100%] in 0.1s (63.53/s)
<■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■> 9/9 [100%] in 0.1s (64.09/s)

[Image: 1f4a1.png]Note: To view a list of all available progress bars, click here.

Method 4: Use tqdm

A simple yet elegant progress bar,
Code:
tqdm
is a must-see!

To run this code error-free, install the required library. Clickhttps://pypi.org/project/alive-progress/1.0/here for installation instructions.

Code:
from tqdm import tqdm from time import sleep for i in tqdm(range(100)): sleep(0.02)

Above imports the
Code:
tqdm
library for the progress bar and the
Code:
time
library to call the
Code:
sleep()
command.

Next, a
Code:
for
loop is instantiated. This loop uses the
Code:
tqdm
library with the
Code:
range
function to set the stop position at 99 (100-1). Inside this loop, the following occurs:

The progress bar displays on one line. Each iteration increases the progress by a percentage.

[Image: bbbb.png]

Method 5: Use progress Library

This method uses the
Code:
progress
library to display a progress bar. This library has many fun options to select from.

To run this code error-free, install the required library. Clickhttps://pypi.org/project/tqdm/here for installation instructions.

Code:
from time import sleep from progress.spinner import MoonSpinner with MoonSpinner('Processing…') as bar: for i in range(100): sleep(0.02) bar.next()

Above imports the
Code:
time
library to call the sleep command and thehttps://pypi.org/project/apytl/
Code:
progress
library to display a progress bar, specifically the
Code:
MoonSpinner
.

The following code displays the word
Code:
Processing...
at the terminal, and the
Code:
MoonSpinner
rotates, sleeps, and continues until the value of the
Code:
range
stop position (100-1) is attained.

Processing…◑

[Image: 1f4a1.png]Note: Spend some time on this library page to test the different types of progress bars.

Summary

There is so much more to Python Progress Bars than was covered in this article. May we suggest you take time to delve into each method outlined above to determine the best one for your requirements.

Happy Coding!

Programming Humor – Python

[Image: image-65.png][i]“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.” — xkcd[/i]
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: