0

I'm trying to replicate this How can I print over the current line in a command line application?

but using html so same example but

import sys
import time
from IPython.display import display,clear_output
for i in range(10):
    time.sleep(1)
    clear_output(wait=True)
    HTML('<b> Hello  ' + str(i) + '</b>')

But it doesn't update. Is there a way to make it work?

In a Ipython Notebook

1 Answer 1

2

Each call to HTML() just creates an object of class IPython.display.HTML. Every object from this class, when displayed by IPython in a notebook, is shown as an html element that is included in the page. See Custom Display for details.

IPython default behavior is to display the last object computed in a cell, if it wasn't used or assigned, which means that a cell with the following code works as intended:

from IPython.display import display,HTML,clear_output
HTML('<b>Hello world</b>')

What you want here is to instruct IPython to display all your objects in sequence, not just the last one created. The IPython.display.display() function does the trick:

import sys
import time
from IPython.display import display,HTML,clear_output
for i in range(10):
    time.sleep(1)
    clear_output(wait=True)
    display(HTML('<b> Hello  ' + str(i) + '</b>'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.