Skip to content

Commit

Permalink
add channel-selection to clear_output
Browse files Browse the repository at this point in the history
example notebook updated accordingly
  • Loading branch information
minrk committed Oct 18, 2011
1 parent 1ea85f6 commit d2f8e5f
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 19 deletions.
24 changes: 21 additions & 3 deletions IPython/core/display.py
Expand Up @@ -383,7 +383,25 @@ def _find_ext(self, s):
return unicode(s.split('.')[-1].lower())


def clear_output():
"""Clear the output of the current cell receiving output."""
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()
InteractiveShell.instance().display_pub.clear_output(
stdout=stdout, stderr=stderr, other=other,
)
2 changes: 1 addition & 1 deletion IPython/core/displaypub.py
Expand Up @@ -104,7 +104,7 @@ def publish(self, source, data, metadata=None):
if data.has_key('text/plain'):
print(data['text/plain'], file=io.stdout)

def clear_output(self):
def clear_output(self, stdout=True, stderr=True, other=True):
"""Clear the output of the cell receiving output."""
pass

Expand Down
38 changes: 35 additions & 3 deletions IPython/frontend/html/notebook/static/js/codecell.js
Expand Up @@ -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);
}
}
}
};


Expand Down
6 changes: 3 additions & 3 deletions IPython/frontend/html/notebook/static/js/notebook.js
Expand Up @@ -627,7 +627,7 @@ var IPython = (function (IPython) {
var cells = this.cells();
for (var i=0; i<ncells; i++) {
if (cells[i] instanceof IPython.CodeCell) {
cells[i].clear_output();
cells[i].clear_output(true,true,true);
}
};
this.dirty = true;
Expand Down Expand Up @@ -734,7 +734,7 @@ var IPython = (function (IPython) {
this.handle_status_dead();
};
} else if (msg_type === 'clear_output') {
cell.clear_output();
cell.clear_output(content.stdout, content.stderr, content.other);
};
};

Expand Down Expand Up @@ -825,7 +825,7 @@ var IPython = (function (IPython) {
var cell = that.selected_cell();
var cell_index = that.find_cell_index(cell);
if (cell instanceof IPython.CodeCell) {
cell.clear_output();
cell.clear_output(true, true, true);
var code = cell.get_code();
var msg_id = that.kernel.execute(cell.get_code());
that.msg_cell_map[msg_id] = cell.cell_id;
Expand Down
5 changes: 3 additions & 2 deletions IPython/zmq/zmqshell.py
Expand Up @@ -80,9 +80,10 @@ def publish(self, source, data, metadata=None):
parent=self.parent_header
)

def clear_output(self):
def clear_output(self, stdout=True, stderr=True, other=True):
content = dict(stdout=stdout, stderr=stderr, other=other)
self.session.send(
self.pub_socket, u'clear_output', {},
self.pub_socket, u'clear_output', content,
parent=self.parent_header
)

Expand Down
67 changes: 60 additions & 7 deletions docs/examples/notebooks/clear_output.ipynb

Large diffs are not rendered by default.

0 comments on commit d2f8e5f

Please sign in to comment.