tqdm printing to newline

PythonProgress BarTqdm

Python Problem Overview


I'm working on a small command-line game in python where I am showing a progress bar using the tqdm module. I listen for user input using the msvcrt module to interrupt the progress. Once interrupted, the user can restart by entering 'restart' into the command line prompt. The second time the progress bar is shown, instead of updating the same line with the progress, it creates a new line each time.

How would I get it to show the progress on the same line?

Progress bar issue

This code snippet illustrates my use of the progress bar.

def transfer():
    for i in tqdm.tqdm(range(1000), desc="Transfer progress", ncols=100, bar_format='{l_bar}{bar}|'):
        sleep(.1)
        if msvcrt.kbhit():
            if msvcrt.getwche() == ' ':
                interrupt()
                break

def interrupt():
    type("File transfer interrupted, to restart the transfer, type 'restart'")

Python Solutions


Solution 1 - Python

Try with position=0 and leave=True

(Solution working in Google Colab to avoid printing to a newline)

from tqdm import tqdm 
import time

def foo_():
    time.sleep(0.3)
range_ = range(0, 10)
total = len(range_)

with tqdm(total=total, position=0, leave=True) as pbar:
   for i in tqdm((foo_, range_ ), position=0, leave=True):
    pbar.update()

Solution 2 - Python

tqdm_notebook is deprecated. You must use tq.notebook.tqdm instead.

import tqdm.notebook as tq
for i in tq.tqdm(...):

Furthermore, tqdm_notebook was really miserable in terms of performances. That's fully corrected with the new library.

Solution 3 - Python

I have realized that closing tqdm instances before using tqdm again fixes the problem of printing status bar in a new line on Jupyter Lab:

while len(tqdm._instances) > 0:
    tqdm._instances.pop().close()

Or even better, thanks to Nirmal for the suggestion:

tqdm._instances.clear()

Solution 4 - Python

from tqdm import tqdm_notebook

this command works in google colab.

Solution 5 - Python

I faced this problem a lot and sometimes position = 0 & leave = True does not works. So, I found one alternate way.

Instead of tqdm.tqdm you can use tqdm.auto.tqdm
or
instead of

from tqdm import tqdm

try using

from tqdm.auto import tqdm

Solution 6 - Python

Try using tqdm.tqdm.write in place of the standard print()

This will print above the progress bar and move the progress bar one row below.

I tested this using below code, pressing space will print into stdout but not break the loop. It was not 100% clear what you are trying to achieve, since the interrupt() function of yours only checks the type of the provided string. type() built-in function

import tqdm
import msvcrt
from time import sleep

def transfer():
    for i in tqdm.tqdm(range(1000), desc="Transfer progress", ncols=100, bar_format='{l_bar}{bar}|'):
        sleep(.1)
        if msvcrt.kbhit():
            if msvcrt.getwche() == ' ':
                interrupt()
                # break

def interrupt():
    tqdm.tqdm.write("File transfer interrupted, to restart the transfer, type 'restart'", end="")

transfer()

EDIT: to include end parameter of tqdm.write() as noted by Paul Netherwood tqdm.tqdm.write()

Solution 7 - Python

from tqdm import notebook

Instead of tqdm(looping)
Use notebook.tqdm(looping)

Solution 8 - Python

You might have imported tqdm twice. Restart the whole notebook kernel and run again. It will solve the issue. It might also be showing because of any print statements inside the tqdm

Solution 9 - Python

Besides the aforementioned position=0, leave=True parameters, in my case the tqdm's default ascii=False parameter was also printing on new lines after a few iterations. You easily identify if this is the case by looking at the progress bar: if there are any weirdly formatted symbols (e.g. question marks) in your progress bar, you should try using ascii=True.

So this worked for me:

from tqdm.auto import tqdm
...

with tqdm(data, position=0, leave=True, ascii=True) as iterator:
   for x in iterator:
      # do stuff
      ...

      iterator.set_postfix_str(msg)

Solution 10 - Python

The following is hacky, but seems to work reasonably well to reset tqdm:

from tqdm import tqdm as tqdm_base
def tqdm(*args, **kwargs):
    if hasattr(tqdm_base, '_instances'):
        for instance in list(tqdm_base._instances):
            tqdm_base._decr_instances(instance)
    return tqdm_base(*args, **kwargs)

Sometimes previous output is printed at the start (which I am not sure how to remove), but I find it much less annoying than newlines (especially in long loops).

Solution 11 - Python

Import tqdm.

from tqdm import tqdm

First start the code, where you use tqdm, stop it because of multiple lines output.

Then do:

list(getattr(tqdm, '_instances'))

for instance in list(tqdm._instances):
    tqdm._decr_instances(instance)

If you get an error:

> AttributeError: type object 'tqdm' has no attribute '_instances'

You need first to start your code, where you use tqdm and only after that start code which mention.

And after all this manipulations your tqdm will work fine.

Solution 12 - Python

leave=False for the inner loop worked in my case.

for j in tqdm(outer_list):
    for i in tqdm(inner_list, leave=False):

Evnironment with tqdm==4.38.0 and Python 3.6.7

Solution 13 - Python

Try using tqdm.tnrange()

for i in tqdm.tnrange(len(df)):

Ongoing image finished image

Solution 14 - Python

I've tried tqdm solution but as I'm using Spyder (Anaconda) it doesn't work in my case as supposed due to mentioned in other answers conflict between write and print commands. I came up with simple and working although not fanciest solution.

def ybar(progr, total, step=50):
    #starts with 1
    l2=(progr/total)//(1/step)
    if progr==1: print(f'[{total}]: '+'|'*int(l2), end = '') 
    else:
        l1=((progr-1)/total)//(1/step) 
        
        ll=int(l2-l1)
        if l1 < l2: 

            for j in range(1,ll+1):
                if (int(l1)+j)%5==0:
                    print('*', end = '')
                else:
                    print('|', end = '')
        if progr==total: print("  DONE")

And as result you'll get simple: [100]: ||||||

for i in range(1,101):
    ybar(i,len(range(1,101)),50)
    #something

There are plenty of solutions here: https://stackoverflow.com/questions/3160699/python-progress-bar

Solution 15 - Python

https://github.com/tqdm/tqdm#parameters I think sometimes tqdm cannot catch the screen width use ncols=xx to restrict line width ex: tqdm(iter,ncols=80): # restrict line width=80

Solution 16 - Python

The issue I'm having might not be common, but in case it's useful to anyone, I was printing another variable to the console. Disabling that print fixes the issue.

Solution 17 - Python

If you encounter this problem while using tqdm on colab or jupyter notebook, then ...

Use notebook/colab version of tqdm

>>> from tqdm.notebook import trange, tqdm
>>> for i in trange(1000):
...     ...

Solution 18 - Python

Try from tqdm import tqdm_notebook as tqdm instead of from tqdm import tqdm.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPieter HelsenView Question on Stackoverflow
Solution 1 - PythonSciPyView Answer on Stackoverflow
Solution 2 - PythonLaurent GRENIERView Answer on Stackoverflow
Solution 3 - PythonJosé VicenteView Answer on Stackoverflow
Solution 4 - PythonAptha GowdaView Answer on Stackoverflow
Solution 5 - PythonRAHUL TIWARIView Answer on Stackoverflow
Solution 6 - Pythonuser10417531View Answer on Stackoverflow
Solution 7 - Pythoncode-freezeView Answer on Stackoverflow
Solution 8 - PythonVijeth RaiView Answer on Stackoverflow
Solution 9 - PythonmjkvaakView Answer on Stackoverflow
Solution 10 - PythonStan KriventsovView Answer on Stackoverflow
Solution 11 - PythonGusev SlavaView Answer on Stackoverflow
Solution 12 - PythonDaniil MashkinView Answer on Stackoverflow
Solution 13 - PythonASHu2View Answer on Stackoverflow
Solution 14 - PythonYury WalletView Answer on Stackoverflow
Solution 15 - Pythonx8569View Answer on Stackoverflow
Solution 16 - PythonYupeng TangView Answer on Stackoverflow
Solution 17 - PythonShaida MuhammadView Answer on Stackoverflow
Solution 18 - PythonImranView Answer on Stackoverflow