Search code examples
ipythonjupytergoogle-colaboratory

How do I use updatable displays on colab?


On the Jupyter Notebook I can create named outputs that are able to be updated like this:

from IPython.display import HTML, display
import time

def progress(value, max=100):
    return HTML("""
        <progress
            value='{value}'
            max='{max}',
            style='width: 100%'
        >
            {value}
        </progress>
    """.format(value=value, max=max))

out = display(progress(0, 100), display_id=True)
for ii in range(101):
    time.sleep(0.02)
    out.update(progress(ii, 100))

progress bar

Whereas in colab it doesn't update the progress bar.

colab no updatey

How do you do this in colab?


Solution

  • Good news! This now works in Colab. :)

    (Previously, we didn't support update_display_data messages.)

    Pasting the code for anyone with sharing issues:

    from IPython.display import HTML, display
    import time
    
    def progress(value, max=100):
        return HTML("""
            <progress
                value='{value}'
                max='{max}',
                style='width: 100%'
            >
                {value}
            </progress>
        """.format(value=value, max=max))
    
    out = display(progress(0, 100), display_id=True)
    for ii in range(101):
        time.sleep(0.02)
        out.update(progress(ii, 100))