117

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'")
Pieter Helsen
  • 1,311
  • 2
  • 9
  • 9

18 Answers18

136

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()
SciPy
  • 4,301
  • 4
  • 16
  • 17
26

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.

Laurent GRENIER
  • 462
  • 1
  • 5
  • 9
23

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()
José Vicente
  • 231
  • 2
  • 4
19
from tqdm import tqdm_notebook

this command works in google colab.

Aptha Gowda
  • 850
  • 2
  • 10
  • 20
11

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
RAHUL TIWARI
  • 145
  • 1
  • 5
7

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()

7

from tqdm import notebook

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

Hamman Samuel
  • 2,080
  • 4
  • 27
  • 39
code-freeze
  • 425
  • 6
  • 7
4

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

Vijeth Rai
  • 301
  • 2
  • 9
3

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)
mjkvaak
  • 371
  • 3
  • 6
2

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).

2

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.

Gusev Slava
  • 1,928
  • 2
  • 19
  • 24
2

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

Daniil Mashkin
  • 4,110
  • 1
  • 32
  • 42
1

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.

Yupeng Tang
  • 653
  • 6
  • 7
0

Try using tqdm.tnrange()

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

Ongoing image finished image

ASHu2
  • 1,958
  • 15
  • 27
  • 3
    Please include code as codeblocks instead of images so as to make it easier to copy/paste (and also reduce Stack Overflow's hosting cost) – Nino Filiu Mar 21 '19 at 16:01
  • 1
    Ah sorry, but the code just `for i in tqdm.tnrange(len(df)):` rest is for jupyter as printing in newline also happens inside notebook. – ASHu2 Mar 21 '19 at 16:50
0

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: Python Progress Bar

Yury Wallet
  • 1,206
  • 12
  • 21
0

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

x8569
  • 1
0

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):
...     ...
Shaida Muhammad
  • 899
  • 8
  • 23
-8

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

Imran
  • 550
  • 7
  • 17