Search code examples
pythonpycharmjupyter-notebooktqdm

Double Progress Bars for Jupyter/PyCharm


I've some code that needs to run over two loops of not insignificant length. So what I'd like to be able to do is have two progress bars; one for each loop.

for _ in tqdm(range(10)):
    for _ in tqdm(range(100)):
        time.sleep(0.01)

I thought that tqdm supported this, and it appears that it does if I run in IPython. However, if I run inside a Jupyter notebook, or in PyCharm, instead of updating the bar(s) after each loop it prints each update on a new line.

I assume that this is something specific to the way that printing works. Has anyone figured out a way to make multiple progress bars work inside a notebook, or in PyCharm.


Solution

  • So apparently there's a leave argument.

    For Jupyter noteboooks:

    import time
    
    from tqdm import tqdm_notebook as tqdm
    
    for _ in tqdm(range(10)):
        for _ in tqdm(range(1000), leave=False):
            time.sleep(0.01)
    

    Otherwise:

    import time
    
    from tqdm import tqdm
    
    for _ in tqdm(range(10)):
        for _ in tqdm(range(1000), leave=False):
            time.sleep(0.01)