Search code examples
tqdm

tqdm_notebook description is truncated


I am using tqdm_notebook to show progress, and update the description to track the loss with the number of iterations. How can I make the description not be truncated (an image attached below).

I define ebar = tqdm_notebook(range(NUM_EPOCHS)) and update the description using:

ebar.set_description('c_loss: {:.5f}, r_loss: {:.5f}, v_loss: {:.5F}'.format(av_class_loss,
                                                                             av_recon_loss,
                                                                             av_valid_loss))

enter image description here


Solution

  • I've been searching on this problem recently, and luckily it has actually been solved in tqdm since version 4.28.1, which is available on Pypi

    See this commit for reference

    Namely, the progress bar is a widget from ipywidgets that has a dedicated progress bar widget which cannot officially change size. See here for more information about widget description

    Now, it seems that your description is quite long, and setting description length to 'initial' might make you end up with a 1px wide progress bar.

    If you need to get a bigger progress bar, the easiest way it display it in two lines, with a Label widget, and change its value directly

    from ipywidgets import HBox, Label, IntProgress
    import time
    from IPython.display import display
    from tqdm import tqdm_notebook as tqdm
    a = Label('A too long description')
    display(a)
    b = tqdm(range(100))
    for i in b:
        time.sleep(0.1)
        a.value = 'a too long descriptio' + 'o'*i +'n'
    

    If you really need to get everything in one line, you can modify tqdm's code here so that tqdm widgets is a Hbox with text, IntProgres, text again.

    ptext = HTML()
    pdesc = HTML()
    container = HBox(children=[pdesc, pbar, ptext])
    

    Once it's done, you can update the description in the same manner as here but here for description