Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding clear_output to kernel and HTML notebook. #789

Closed
wants to merge 1 commit into from

3 participants

Brian E. Granger Fernando Perez Min RK
Brian E. Granger
Owner

This enables the clearing of output during the execution of a cell. It can be used for simple forms of animation in the notebook.

Fernando Perez
Owner

I like the idea of the clear_output message, because I think it does have a lot of valid use cases. A few comments though on this PR before merging:

  • this is a new message type, so it needs to be documented in the spec.
  • there should be at least a test about the message

We were terrible in adding the entire messaging spec without a simple test suite for all the message types that would check compliance and should match our docs. While we may not have time right now to retrofit the whole spec with full test coverage, let's not compound the problem by adding more messages without any tests. This is an easy one to start with, and our test_messages can for now just consist of a test for this one. Over time, we can extend it to complete coverage of the entire specification.

We're starting to have other users of the messaging spec and more than one client, so it's becoming very important to have that specification really validated.

Once these two points are addressed, this one can go in as far as I'm concerned. Thanks for doing it quickly, I think it's a very good idea and will be super-useful in practice.

Min RK
Owner

Since this is a new message type, it should probably be added to the message spec doc.

(quote from #788 comment):

Yep, that is basically what I implemented.

No, it isn't. You implemented a new message type, for which the behavior is really quite different. I'm not saying what you did is better or worse, but it's certainly not adding hold-like function to existing display messages, since it's a new message altogether. If you did want to avoid adding a new message to the spec, this could certainly have been added tot he regular display-message spec, which would also result in a reduction in the number of messages to accomplish a 'display this and this alone' action from two to one.

Brian E. Granger
Owner
Brian E. Granger
Owner
Min RK
Owner
Brian E. Granger
Owner
Fernando Perez
Owner
Brian E. Granger
Owner
Fernando Perez
Owner
Min RK
Owner

I like the channels idea. I wouldn't breakdown by mimetype, because that doesn't make a lot of sense to me. For instance, if you are doing repeated plots of a value inplace, changing the figure type from png to svg shouldn't change the behavior.

The way we have channel options elsewhere is in the KernelManager, which has:

KM.start_channels(self, shell=True, stdin=True, iopub=True, hb=True)

so, should we have:

clear_output(stdout=True, stderr=True, pyout=True, pyerr=True, payloads=True)

?

Note that I don't think pyerr and pyout can get output at any point other than the end of execution, which is necessarily after any call to clear_output(), so they may not make sense to include. I'm pretty sure about that on pyerr, but not pyout.

Min RK
Owner

I made the channel support changes described in my branch.

Fernando Perez
Owner

Did you make a PR for @ellisonbg to merge those in here, or do you want to move this PR over to your branch?

Min RK
Owner

I can do a new PR if Brian doesn't have time to pull from mine.

Fernando Perez
Owner

Closing here since it will be done in #893.

Fernando Perez fperez closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 14, 2011
  1. Brian E. Granger
This page is out of date. Refresh to see the latest.
6 IPython/core/display.py
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()
4 IPython/core/displaypub.py
View
@@ -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.
4 IPython/frontend/html/notebook/static/js/notebook.js
View
@@ -679,7 +679,9 @@ var IPython = (function (IPython) {
} else if (content.execution_state === 'dead') {
this.handle_status_dead();
};
- }
+ } else if (msg_type === 'clear_output') {
+ cell.clear_output();
+ };
};
5 IPython/zmq/zmqshell.py
View
@@ -74,6 +74,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."""
56 docs/examples/notebooks/clear_output.ipynb
View
@@ -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
+}
Something went wrong with that request. Please try again.