From bdf1ecd4d35dc497322a992d0b0bff2008f8c307 2011-10-17 23:19:58
From: Brian Granger <ellisonbg@gmail.com>
Date: 2011-10-17 23:19:58
Subject: [PATCH] Adding clear_output to kernel and HTML notebook.

---

diff --git a/IPython/core/display.py b/IPython/core/display.py
index 3fd2206..7b08d12 100644
--- a/IPython/core/display.py
+++ b/IPython/core/display.py
@@ -381,3 +381,9 @@ class Image(DisplayObject):
 
     def _find_ext(self, s):
         return unicode(s.split('.')[-1].lower())
+
+
+def clear_output():
+    """Clear the output of the current cell receiving output."""
+    from IPython.core.interactiveshell import InteractiveShell
+    InteractiveShell.instance().display_pub.clear_output()
diff --git a/IPython/core/displaypub.py b/IPython/core/displaypub.py
index d214d1b..4763f50 100644
--- a/IPython/core/displaypub.py
+++ b/IPython/core/displaypub.py
@@ -104,6 +104,10 @@ class DisplayPublisher(Configurable):
         if data.has_key('text/plain'):
             print(data['text/plain'], file=io.stdout)
 
+        def clear_output(self):
+            """Clear the output of the cell receiving output."""
+            pass
+
 
 def publish_display_data(source, data, metadata=None):
     """Publish data and metadata to all frontends.
diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js
index d149ac3..29b7487 100644
--- a/IPython/frontend/html/notebook/static/js/notebook.js
+++ b/IPython/frontend/html/notebook/static/js/notebook.js
@@ -733,7 +733,9 @@ var IPython = (function (IPython) {
             } else if (content.execution_state === 'dead') {
                 this.handle_status_dead();
             };
-        }
+        } else if (msg_type === 'clear_output') {
+            cell.clear_output();
+        };
     };
 
 
diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py
index e1bb0d8..c5f7d9f 100644
--- a/IPython/zmq/zmqshell.py
+++ b/IPython/zmq/zmqshell.py
@@ -80,6 +80,11 @@ class ZMQDisplayPublisher(DisplayPublisher):
             parent=self.parent_header
         )
 
+    def clear_output(self):
+        self.session.send(
+            self.pub_socket, u'clear_output', {},
+            parent=self.parent_header
+        )
 
 class ZMQInteractiveShell(InteractiveShell):
     """A subclass of InteractiveShell for ZMQ."""
diff --git a/docs/examples/notebooks/clear_output.ipynb b/docs/examples/notebooks/clear_output.ipynb
new file mode 100644
index 0000000..778ef27
--- /dev/null
+++ b/docs/examples/notebooks/clear_output.ipynb
@@ -0,0 +1,56 @@
+{
+    "worksheets": [
+        {
+            "cells": [
+                {
+                    "source": "A demonstration of the ability to clear the output of a cell during execution.", 
+                    "cell_type": "markdown"
+                }, 
+                {
+                    "cell_type": "code", 
+                    "language": "python", 
+                    "outputs": [], 
+                    "collapsed": true, 
+                    "prompt_number": 8, 
+                    "input": "from IPython.core.display import clear_output, display"
+                }, 
+                {
+                    "cell_type": "code", 
+                    "language": "python", 
+                    "outputs": [], 
+                    "collapsed": false, 
+                    "prompt_number": 4, 
+                    "input": "import time"
+                }, 
+                {
+                    "source": "First we show how this works with ``display``:", 
+                    "cell_type": "markdown"
+                }, 
+                {
+                    "cell_type": "code", 
+                    "language": "python", 
+                    "outputs": [], 
+                    "collapsed": false, 
+                    "prompt_number": 17, 
+                    "input": "for i in range(10):\n    display(\"Time step: %i\" % i)\n    time.sleep(0.5)\n    clear_output()"
+                }, 
+                {
+                    "source": "Next, we show that ``clear_output`` can also be used to create a primitive form of animation using\nmatplotlib:", 
+                    "cell_type": "markdown"
+                }, 
+                {
+                    "cell_type": "code", 
+                    "language": "python", 
+                    "outputs": [], 
+                    "collapsed": false, 
+                    "prompt_number": 20, 
+                    "input": "for i in range(10):\n    figure()\n    plot(rand(100))\n    show()\n    time.sleep(0.1)\n    clear_output()"
+                }
+            ]
+        }
+    ], 
+    "metadata": {
+        "name": "clear_output"
+    }, 
+    "nbformat": 2
+}
\ No newline at end of file