display(...display_id=anything)
returns a handle
with .display(obj)
and .update(obj)
methods.from IPython.display import display, update_display
handle = display('x', display_id=True)
handle
'z'
<DisplayHandle display_id=bea6030c53e8a7fe2034b9d9f3efc6ca>
handle.display('y')
'z'
handle.update('z')
display('x', display_id='here');
'z'
display('y', display_id='here');
'z'
update_display('z', display_id='here')
An example ProgressBar using these messages:
import os
from binascii import hexlify
class ProgressBar(object):
def __init__(self, capacity):
self.progress = 0
self.capacity = capacity
self.width = 48
self._display_id = hexlify(os.urandom(8)).decode('ascii')
def __repr__(self):
fraction = self.progress / self.capacity
filled = '=' * int(fraction * self.width)
rest = ' ' * (self.width - len(filled))
return '[{}{}] {}/{}'.format(
filled, rest,
self.progress, self.capacity,
)
def display(self):
display(self, display_id=self._display_id)
def update(self):
update_display(self, display_id=self._display_id)
bar = ProgressBar(10)
bar.display()
[================================================] 10/10
Update the progress bar:
import time
bar.display()
for i in range(11):
bar.progress = i
bar.update()
time.sleep(0.25)
[================================================] 10/10