Tqdm in Jupyter Notebook Prints New Progress Bars Repeatedly
I Am Using Tqdm to Print Progress in a Script I'm Running in a Jupyter Notebook. I Am Printing All Messages to the Console via Tqdm. Write(). However, This...
I am using tqdm to print progress in a script I'm running in a Jupyter notebook. I am printing all messages to the console via tqdm.write(). However, this still gives me a skewed output like so:
That is, each time a new line has to be printed, a new progress bar is printed on the next line. This does not happen when I run the script via terminal. How can I solve this?
11 Answers
Try using tqdm.notebook.tqdm instead of tqdm, as outlined here.
This could be as simple as changing your import to:
from tqdm.notebook import tqdm
Good luck!
EDIT: After testing, it seems that tqdm actually works fine in 'text mode' in Jupyter notebook. It's hard to tell because you haven't provided a minimal example, but it looks like your problem is caused by a print statement in each iteration. The print statement is outputting a number (~0.89) in between each status bar update, which is messing up the output. Try removing the print statement.
This is an alternative answer for the case where tqdm_notebook doesn't work for you.
Given the following example:
from time import sleep
from tqdm import tqdm
values = range(3)
with tqdm(total=len(values)) as pbar:
for i in values:
pbar.write('processed: %d' %i)
pbar.update(1)
sleep(1)
The output would look something like this (progress would show up red):
0%| | 0/3 [00:00<?, ?it/s]
processed: 1
67%|██████▋ | 2/3 [00:01<00:00, 1.99it/s]
processed: 2
100%|██████████| 3/3 [00:02<00:00, 1.53it/s]
processed: 3
The problem is that the output to stdout and stderr are processed asynchronously and separately in terms of new lines.
If say Jupyter receives on stderr the first line and then the "processed" output on stdout. Then once it receives an output on stderr to update the progress, it wouldn't go back and update the first line as it would only update the last line. Instead it will have to write a new line.