Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a nested tqdm bars on jupyter notebook

Here is an example for nested tqdm bar

import time
import tqdm
for i in tqdm.tqdm(range(3)):
    for j in tqdm.tqdm(range(5)):
        print(i," : ", j)

I try it on jupyter notebook but it does not show any thing without error! However, it works coorectly on ipython console!

like image 348
Jeanne Avatar asked Oct 23 '17 18:10

Jeanne


1 Answers

I just found the solution that I'm going to use. The solution has plenty of examples here.

I've modified the example in the original post with both the solution as well as a time delay for visualization purposes (the final output is the same with or without the delay).

from time import sleep
from tqdm import tqdm_notebook
for i in tqdm_notebook(range(3)):
    for j in tqdm_notebook(range(5)):
        sleep(0.1)
        print(i," : ", j)

print("Done!")

The final output looks like this. While it's processing, it's pleasant to watch at (no jumping around or anything crazy).

Image of the final output after completion


One little hack that I'm now doing to make this a super easy drop-in replacement is to pull in tqdm like this, so I don't have to change any other code:

from time import sleep
from tqdm import tqdm_notebook as tqdm
for i in tqdm(range(3)):
    for j in tqdm(range(5)):
        sleep(0.1)
        print(i," : ", j)

print("Done!")
like image 58
Jaxidian Avatar answered Oct 04 '22 20:10

Jaxidian