Permalink
Browse files

Adding clear_output to kernel and HTML notebook.

  • Loading branch information...
1 parent 4332951 commit bdf1ecd4d35dc497322a992d0b0bff2008f8c307 @ellisonbg ellisonbg committed with Sep 14, 2011
View
@@ -381,3 +381,9 @@ def _repr_jpeg_(self):
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()
@@ -104,6 +104,10 @@ def publish(self, source, data, metadata=None):
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.
@@ -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();
+ };
};
View
@@ -80,6 +80,11 @@ def publish(self, source, data, metadata=None):
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."""
@@ -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
+}

0 comments on commit bdf1ecd

Please sign in to comment.