From fa157db84c00e88b3ec0b40384d42914574d6d32 2012-04-09 22:11:30 From: MinRK Date: 2012-04-09 22:11:30 Subject: [PATCH] clear_output implies '\r' for terminal frontends --- diff --git a/IPython/core/display.py b/IPython/core/display.py index c44f3d0..37c6021 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -17,6 +17,8 @@ Authors: # Imports #----------------------------------------------------------------------------- +from __future__ import print_function + from xml.dom import minidom from .displaypub import ( @@ -496,6 +498,16 @@ def clear_output(stdout=True, stderr=True, other=True): (e.g. figures,images,HTML, any result of display()). """ from IPython.core.interactiveshell import InteractiveShell - InteractiveShell.instance().display_pub.clear_output( - stdout=stdout, stderr=stderr, other=other, - ) + if InteractiveShell.initialized(): + InteractiveShell.instance().display_pub.clear_output( + stdout=stdout, stderr=stderr, other=other, + ) + else: + from IPython.utils import io + if stdout: + print('\033[2K\r', file=io.stdout, end='') + io.stdout.flush() + if stderr: + print('\033[2K\r', file=io.stderr, end='') + io.stderr.flush() + diff --git a/IPython/core/displaypub.py b/IPython/core/displaypub.py index cfcf939..388757e 100644 --- a/IPython/core/displaypub.py +++ b/IPython/core/displaypub.py @@ -30,6 +30,7 @@ Authors: from __future__ import print_function from IPython.config.configurable import Configurable +from IPython.utils import io #----------------------------------------------------------------------------- # Main payload class @@ -99,14 +100,20 @@ class DisplayPublisher(Configurable): arbitrary key, value pairs that frontends can use to interpret the data. """ - from IPython.utils import io + # The default is to simply write the plain text data using io.stdout. if data.has_key('text/plain'): print(data['text/plain'], file=io.stdout) - def clear_output(self, stdout=True, stderr=True, other=True): - """Clear the output of the cell receiving output.""" - pass + def clear_output(self, stdout=True, stderr=True, other=True): + """Clear the output of the cell receiving output.""" + if stdout: + print('\033[2K\r', file=io.stdout, end='') + io.stdout.flush() + if stderr: + print('\033[2K\r', file=io.stderr, end='') + io.stderr.flush() + def publish_display_data(source, data, metadata=None): diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 21baabf..7c42f88 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -81,8 +81,15 @@ class ZMQDisplayPublisher(DisplayPublisher): ) def clear_output(self, stdout=True, stderr=True, other=True): - self._flush_streams() content = dict(stdout=stdout, stderr=stderr, other=other) + + if stdout: + print('\r', file=sys.stdout, end='') + if stderr: + print('\r', file=sys.stderr, end='') + + self._flush_streams() + self.session.send( self.pub_socket, u'clear_output', content, parent=self.parent_header