personal_site/_posts/2023-06-22-tqdm_and_printing_in_notebooks.md
2023-10-13 15:50:00 +02:00

1.2 KiB

title, excerpt, layout, image, thumbnail, image_class, alt
title excerpt layout image thumbnail image_class alt
Progress bars and log output in Jupyter notebooks How to get an updatable message printing a tqdm progress bar. post /assets/blog/progress_bars/bar.png /assets/blog/progress_bars/thumbnail.png invertable An image of a nice animated progress bar in a jupyter notebook output cell.

I wanted to have just one updatable line of output that would play nicely with a tqdm progress bar. After playing around with print(s, end="\r") I settled on using Ipython.display with a handle. The problem with the print approach is that it doesn't work when the output is shorter than the previous line.

import time
import random
from tqdm.auto import tqdm
from IPython.display import display, Markdown

info_line = display(Markdown(''), display_id=True)

for x in tqdm(range(0,5), position = 0):  
    for y in tqdm(range(0,5), position = 1, leave=False):  
        x = random.randint(1, 10)
        b = "Loading" + "." * x
        info_line.update(Markdown(b))
        time.sleep(0.5)
What it looks like in the end.