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

Community
  • 1
  • 1
Delta_Fore
  • 3,079
  • 4
  • 26
  • 46

1 Answers1

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>'))
Taar
  • 808
  • 8
  • 8