Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using tqdm progress bar in a while loop

People also ask

Can you use tqdm with while loop?

tqdm does not require any dependencies and works across multiple python environments. Integrating tqdm can be done effortlessly in loops, on iterable, with Pandas or even with machine learning libraries— just wrap any iterable with tqdm(iterable) , and you're done!

How do you track a loop progress in Python?

Instead of printing out indices or other info at each iteration of your Python loops to see the progress, you can easily add a progress bar. wrap the object on which you iterate with pbar() . and it will display a progress that automatically updates itself after each iteration of the loop.

What is tqdm progress bar?

tqdm derives from the Arabic word taqaddum (تقدّم) which can mean "progress," and is an abbreviation for "I love you so much" in Spanish (te quiero demasiado). Instantly make your loops show a smart progress meter - just wrap any iterable with tqdm(iterable) , and you're done!


You can use manual control in tqdm by specifying a total argument in the constructor. Verbatim from the manual:

with tqdm(total=100) as pbar:
    for i in range(10):
        sleep(0.1)
        pbar.update(10)

UPDATE

To manually control the tqdm without the context manager (aka with statement), you will need to close the progress bar after you are done using it. Here is another example from the manual:

pbar = tqdm(total=100)
for i in range(10):
    sleep(0.1)
    pbar.update(10)
pbar.close()

For this to work you need to know the total number of expected runs. In your code it could look something like

...
pbar = tqdm(total = runs+1)
while currentData[0] <= runs:

    ### ROLLING THE DICES PROCESS ###
    dices = twinDiceRoll()
    currentData[1] += dices[2]  # Updating the current tile

    ### SURPASSING THE NUMBER OF TILES ONBOARD ###
    if currentData[1] > 37:   # If more than a table turn is achieved,
        currentData[0] += 1   # One more turn is registered
        currentData[1] -= 38  # Update the tile to one coresponding to a board tile.
        pbar.update(1)
    else:
        pass
...
pbar.close()

However, this code isn't perfect: consider if the currentData[1] is always less than 37 -- the progress bar will just stop and not update. If you try to update it in the else:... part, you might violate the total upper bound. This is a start tho :)


Because of the attention, this post is attracting I thought it would be good to point out how this can be achieved with an infinite while loop as well.

To use an infinite loop with tqdm you need to change your while loop into an infinite for loop by utilizing a generator.

Infinite loop (no progress bar)

while True:
  # Do stuff here

Infinite loop (with progress bar)

def generator():
  while True:
    yield

for _ in tqdm(generator()):
  # Do stuff here

The code above would create an indefinite progress bar that would look similar to this

16it [01:38,  6.18s/it]

Note that the generator could also be modified to work with a condition

def generator():
  while condition:
    yield