Skip to content
This repository

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 October 18, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Sep 13, 2011
Brian E. Granger Adding clear_output to kernel and HTML notebook. 22e6ffc
This page is out of date. Refresh to see the latest.
6  IPython/core/display.py
@@ -381,3 +381,9 @@ def _repr_jpeg_(self):
381 381
 
382 382
     def _find_ext(self, s):
383 383
         return unicode(s.split('.')[-1].lower())
  384
+
  385
+
  386
+def clear_output():
  387
+    """Clear the output of the current cell receiving output."""
  388
+    from IPython.core.interactiveshell import InteractiveShell
  389
+    InteractiveShell.instance().display_pub.clear_output()
4  IPython/core/displaypub.py
@@ -104,6 +104,10 @@ def publish(self, source, data, metadata=None):
104 104
         if data.has_key('text/plain'):
105 105
             print(data['text/plain'], file=io.stdout)
106 106
 
  107
+        def clear_output(self):
  108
+            """Clear the output of the cell receiving output."""
  109
+            pass
  110
+
107 111
 
108 112
 def publish_display_data(source, data, metadata=None):
109 113
     """Publish data and metadata to all frontends.
4  IPython/frontend/html/notebook/static/js/notebook.js
@@ -679,7 +679,9 @@ var IPython = (function (IPython) {
679 679
             } else if (content.execution_state === 'dead') {
680 680
                 this.handle_status_dead();
681 681
             };
682  
-        }
  682
+        } else if (msg_type === 'clear_output') {
  683
+            cell.clear_output();
  684
+        };
683 685
     };
684 686
 
685 687
 
5  IPython/zmq/zmqshell.py
@@ -74,6 +74,11 @@ def publish(self, source, data, metadata=None):
74 74
             parent=self.parent_header
75 75
         )
76 76
 
  77
+    def clear_output(self):
  78
+        self.session.send(
  79
+            self.pub_socket, u'clear_output', {},
  80
+            parent=self.parent_header
  81
+        )
77 82
 
78 83
 class ZMQInteractiveShell(InteractiveShell):
79 84
     """A subclass of InteractiveShell for ZMQ."""
56  docs/examples/notebooks/clear_output.ipynb
... ...
@@ -0,0 +1,56 @@
  1
+{
  2
+    "worksheets": [
  3
+        {
  4
+            "cells": [
  5
+                {
  6
+                    "source": "A demonstration of the ability to clear the output of a cell during execution.", 
  7
+                    "cell_type": "markdown"
  8
+                }, 
  9
+                {
  10
+                    "cell_type": "code", 
  11
+                    "language": "python", 
  12
+                    "outputs": [], 
  13
+                    "collapsed": true, 
  14
+                    "prompt_number": 8, 
  15
+                    "input": "from IPython.core.display import clear_output, display"
  16
+                }, 
  17
+                {
  18
+                    "cell_type": "code", 
  19
+                    "language": "python", 
  20
+                    "outputs": [], 
  21
+                    "collapsed": false, 
  22
+                    "prompt_number": 4, 
  23
+                    "input": "import time"
  24
+                }, 
  25
+                {
  26
+                    "source": "First we show how this works with ``display``:", 
  27
+                    "cell_type": "markdown"
  28
+                }, 
  29
+                {
  30
+                    "cell_type": "code", 
  31
+                    "language": "python", 
  32
+                    "outputs": [], 
  33
+                    "collapsed": false, 
  34
+                    "prompt_number": 17, 
  35
+                    "input": "for i in range(10):\n    display(\"Time step: %i\" % i)\n    time.sleep(0.5)\n    clear_output()"
  36
+                }, 
  37
+                {
  38
+                    "source": "Next, we show that ``clear_output`` can also be used to create a primitive form of animation using\nmatplotlib:", 
  39
+                    "cell_type": "markdown"
  40
+                }, 
  41
+                {
  42
+                    "cell_type": "code", 
  43
+                    "language": "python", 
  44
+                    "outputs": [], 
  45
+                    "collapsed": false, 
  46
+                    "prompt_number": 20, 
  47
+                    "input": "for i in range(10):\n    figure()\n    plot(rand(100))\n    show()\n    time.sleep(0.1)\n    clear_output()"
  48
+                }
  49
+            ]
  50
+        }
  51
+    ], 
  52
+    "metadata": {
  53
+        "name": "clear_output"
  54
+    }, 
  55
+    "nbformat": 2
  56
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.