From c99b9fd7d3e48000ef54148ff11e142b2aee0028 2011-10-19 00:55:13 From: Fernando Perez Date: 2011-10-19 00:55:13 Subject: [PATCH] Merge pull request #893 from minrk/clearoutput Adding clear_output to kernel and HTML notebook. This enables the clearing of output during the execution of a cell. It can be used for simple forms of animation in the notebook. --- diff --git a/IPython/core/display.py b/IPython/core/display.py index 3fd2206..91c8ae9 100644 --- a/IPython/core/display.py +++ b/IPython/core/display.py @@ -381,3 +381,27 @@ class Image(DisplayObject): def _find_ext(self, s): return unicode(s.split('.')[-1].lower()) + + +def clear_output(stdout=True, stderr=True, other=True): + """Clear the output of the current cell receiving output. + + Optionally, each of stdout/stderr or other non-stream data (e.g. anything + produced by display()) can be excluded from the clear event. + + By default, everything is cleared. + + Parameters + ---------- + stdout : bool [default: True] + Whether to clear stdout. + stderr : bool [default: True] + Whether to clear stderr. + other : bool [default: True] + Whether to clear everything else that is not stdout/stderr + (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, + ) diff --git a/IPython/core/displaypub.py b/IPython/core/displaypub.py index d214d1b..7ff3f07 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, stdout=True, stderr=True, other=True): + """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/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 83ebd37..b557e29 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -364,9 +364,41 @@ var IPython = (function (IPython) { } - CodeCell.prototype.clear_output = function () { - this.element.find("div.output").html(""); - this.outputs = []; + CodeCell.prototype.clear_output = function (stdout, stderr, other) { + var output_div = this.element.find("div.output"); + if (stdout && stderr && other){ + // clear all, no need for logic + output_div.html(""); + this.outputs = []; + return; + } + // remove html output + // each output_subarea that has an identifying class is in an output_area + // which is the element to be removed. + if (stdout){ + output_div.find("div.output_stdout").parent().remove(); + } + if (stderr){ + output_div.find("div.output_stderr").parent().remove(); + } + if (other){ + output_div.find("div.output_subarea").not("div.output_stderr").not("div.output_stdout").parent().remove(); + } + + // remove cleared outputs from JSON list: + for (var i = this.outputs.length - 1; i >= 0; i--){ + var out = this.outputs[i]; + var output_type = out.output_type; + if (output_type == "display_data" && other){ + this.outputs.splice(i,1); + }else if (output_type == "stream"){ + if (stdout && out.stream == "stdout"){ + this.outputs.splice(i,1); + }else if (stderr && out.stream == "stderr"){ + this.outputs.splice(i,1); + } + } + } }; diff --git a/IPython/frontend/html/notebook/static/js/notebook.js b/IPython/frontend/html/notebook/static/js/notebook.js index d149ac3..0cc2e20 100644 --- a/IPython/frontend/html/notebook/static/js/notebook.js +++ b/IPython/frontend/html/notebook/static/js/notebook.js @@ -627,7 +627,7 @@ var IPython = (function (IPython) { var cells = this.cells(); for (var i=0; i