Search code examples
pythonjupytertqdm

tqdm failure with Jupyter Notebook


tqdm is one of my favorite Python packages, but I have an annoying issue with it and trying to figure out if this is my fault or not.

While running a loop with tqdm using Jupyter Notebook and encoutering a RunTime Error the recovery is really hard - fixing the bug and reruning the loop ofter result in multiline prints, instead of tqdm original single line. The only way to get back is to restart the Kernel which is not very useful.

Are the any other solutions?

I can't reproduce the issue intentionally but here is an example code that might cause it:

from tqdm import trange
s=0
for i in trange(100):
    s+=i
    if i==10:
        raise ValueError

and then reruning the cell.


Solution

  • To overcome this issue, I use the following conditional import which works in code that might run in Jupyter Notebook or in the console (and do nothing if not run interactively):

    # progress.py
    import sys
    
    
    def tqdm(iterable, **kwargs):
        """Fake progress function."""
    
        return iterable
    
    
    # check if running notebook and use notebook backend for tqdm progress bar
    if 'IPython' in sys.modules:
    
        from IPython import get_ipython
        from IPython.display import display_javascript, Javascript
    
        from tqdm import tqdm
    
        ip = get_ipython()
    
        if 'IPKernelApp' in ip.config:
            monkey_patch = f"""
                from tqdm import tqdm_notebook as tqdm
                import {__name__} as module
                module.tqdm = tqdm
            """
            monkey_patch = ';'.join(x.strip() for x in monkey_patch.strip().split('\n'))
            display_javascript(Javascript(f"""IPython.notebook.kernel.execute("{monkey_patch}");"""))
    

    Then use it:

    $ jupyter console  # or ipython3
    In [1]: import progress
    
    In [2]: list(progress.tqdm(range(10)))
    100%|██████████| 10/10 [00:00<00:00, 120873.31it/s]
    Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]