#!/usr/bin/env python # coding: utf-8 # # Updatable displays proposal # # - `display(...display_id=True) generates a new, random display_id # - `display(...display_id=anything)` returns a `handle` with `.display(obj)` and `.update(obj)` methods. # In[1]: from IPython.display import display, update_display # In[2]: handle = display('x', display_id=True) handle # In[3]: handle.display('y') # In[4]: handle.update('z') # In[5]: display('x', display_id='here'); # In[6]: display('y', display_id='here'); # In[7]: update_display('z', display_id='here') # An example ProgressBar using these messages: # In[8]: 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() # Update the progress bar: # In[9]: import time bar.display() for i in range(11): bar.progress = i bar.update() time.sleep(0.25)