0

I am trying to display two TQDM progress bar in Jupyter Notebook. I'm using VSC with Jupyter Notebook and Python 3.9.

I have the following code:

from pathlib import Path
from tqdm.autonotebook import tqdm

outerdir = Path('some/path')

dirs = list(outerdir.iterdir())

for dir in tqdm(dirs, desc='outer'):
    files = dir.iterdir()

    for file in tqdm(files, desc='inner'):
        **do something**

However, my output is as follows:

outer:   0%|          | 0/217 [00:00<?, ?it/s]

Meaning, it only shows the outer progress bar, not the inner. In addition, it does not show the ETA and the speed.

Does anyone know why that is?

NB: It was working when I still used os instead of pathlib to manage paths and folder iteration.

[Edit]: The code inside the inner loop is taking at least 1 min per run. There are approximately 50 runs of the inner loop for each iteration of the outer loop, so the outer loop is taking at least 50 min per run. This means that the duration of the loop should not be the problem.

Neele22
  • 373
  • 2
  • 18
  • as I know it uses special char `"\r"` to move to beginning of line and draw progress bar with new values - but this can't move to previous line so it can't draw two progress bars in two separated rows - so maybe it draw both progress bars in one place but finally you see only `output` which is displayed in the same place as `inner`. And maybe inner for-loop runs so fast that you don't even see progress bar. You could try with some `time.sleep()` to slow down code. – furas Feb 17 '22 at 17:20

1 Answers1

0

Here is a solution I created not using TQDM, it calculates ETA based on time took for 1 iterationenter image description here

import time
import sys
from colorama import init, Fore, Back, Style, ansi

init(autoreset=True)

def progress(count, total, eta, status=''):
    bar_len = 50
    filled_len = int(round(bar_len * count / float(total)))
    tip = '>' if count < total else ''
    n = Fore.GREEN +"Complete"+ Fore.WHITE if filled_len == bar_len else Fore.RED + status + Fore.WHITE
    percents = round(100.0 * count / float(total), 1)
    bar = Fore.MAGENTA+'=' * filled_len + tip + Fore.CYAN +'-' * (bar_len - filled_len)+Fore.WHITE
    sys.stdout.write('(%s) [%s] %s%s ~%s\r' % (n, bar, percents, '%', eta))
    sys.stdout.flush()

x = time.perf_counter()
k = 50
m, m_ = 0, 0
tm = 1
for i in range(1, k+1):
    b = " "
    #estimated time of arrival ETA
    m_1 = Back.WHITE +Fore.BLUE+ " ETA {:.2f} sec ".format(abs(m-m_))
    progress(i, k, eta=f' {m_1}', status = 'Running')
    time.sleep(tm)
    y = time.perf_counter() 
    benchmark = y-x
    m = benchmark * (k/i)
    m_ = (m/k) * i+ tm
Anonimy
  • 11
  • 2