From 5c6c247b653b5528669d868c7a2c102bc3cc22b3 Mon Sep 17 00:00:00 2001 From: MinRK Date: Tue, 3 Apr 2012 22:52:43 -0600 Subject: [PATCH 1/3] [notebook] clear_output is handled after a delay This reduces flicker during common loops like: for step in stuff: clear_output() print something the timeout is flushed *immediately* on any subsequent output. --- .../html/notebook/static/js/codecell.js | 39 +++++++++++++++++++ .../frontend/html/notebook/static/js/utils.js | 5 ++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 82da3f40a..ecc563577 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -21,6 +21,7 @@ var IPython = (function (IPython) { this.outputs = []; this.collapsed = false; this.tooltip_timeout = null; + this.clear_out_timeout = null; IPython.Cell.apply(this, arguments); }; @@ -566,6 +567,7 @@ var IPython = (function (IPython) { CodeCell.prototype.append_output = function (json, dynamic) { // If dynamic is true, javascript output will be eval'd. this.expand(); + this.flush_clear_timeout(); if (json.output_type === 'pyout') { this.append_pyout(json, dynamic); } else if (json.output_type === 'pyerr') { @@ -621,6 +623,11 @@ var IPython = (function (IPython) { if (json.stream == undefined){ json.stream = 'stdout'; } + if (!utils.fixConsole(json.text)){ + // fixConsole gives nothing (empty string, \r, etc.) + // so don't append any elements, which might add undesirable space + return; + } var subclass = "output_"+json.stream; if (this.outputs.length > 0){ // have at least one output to consider @@ -730,7 +737,30 @@ var IPython = (function (IPython) { CodeCell.prototype.clear_output = function (stdout, stderr, other) { + var that = this; + if (this.clear_out_timeout != null){ + // fire previous pending clear *immediately* + clearTimeout(this.clear_out_timeout); + this.clear_out_timeout = null; + this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); + } + // store flags for flushing the timeout + this._clear_stdout = stdout; + this._clear_stderr = stderr; + this._clear_other = other; + this.clear_out_timeout = setTimeout(function(){ + // really clear timeout only after a short delay + // this reduces flicker in 'clear_output; print' cases + that.clear_out_timeout = null; + that._clear_stdout = that._clear_stderr = that._clear_other = null; + that.clear_output_callback(stdout, stderr, other); + }, 500 + ); + }; + + CodeCell.prototype.clear_output_callback = 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(""); @@ -770,6 +800,15 @@ var IPython = (function (IPython) { CodeCell.prototype.clear_input = function () { this.code_mirror.setValue(''); }; + + CodeCell.prototype.flush_clear_timeout = function() { + var output_div = this.element.find('div.output'); + if (this.clear_out_timeout){ + clearTimeout(this.clear_out_timeout); + this.clear_out_timeout = null; + this.clear_output_callback(this._clear_stdout, this._clear_stderr, this._clear_other); + }; + } CodeCell.prototype.collapse = function () { diff --git a/IPython/frontend/html/notebook/static/js/utils.js b/IPython/frontend/html/notebook/static/js/utils.js index bce44b367..5b1022b7b 100644 --- a/IPython/frontend/html/notebook/static/js/utils.js +++ b/IPython/frontend/html/notebook/static/js/utils.js @@ -52,12 +52,13 @@ IPython.utils = (function (IPython) { // are set in the css file. function fixConsole(txt) { txt = xmlencode(txt); - var re = /\033\[([\d;]*?)m/; + var re = /\033\[([\dA-Fa-f;]*?)m/; var opened = false; var cmds = []; var opener = ""; var closer = ""; - + // \r does nothing, so shouldn't be included + txt = txt.replace('\r', ''); while (re.test(txt)) { var cmds = txt.match(re)[1].split(";"); closer = opened?"":""; From c21be2f386ee78385ce2207bd2baaad9c98a97cd Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 9 Apr 2012 15:10:43 -0700 Subject: [PATCH 2/3] hide output_area for js prevents growing vertical space from adding empty output_areas. --- IPython/frontend/html/notebook/static/js/codecell.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index ecc563577..84055b7b0 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -690,6 +690,9 @@ var IPython = (function (IPython) { // We just eval the JS code, element appears in the local scope. var element = $("
").addClass("box_flex1 output_subarea"); e.append(element); + // Div for js shouldn't be drawn, as it will add empty height to the area. + e.hide(); + eval(js); } From 9cb1559d5e24967ebfc628e4ec52c3935fc144cb Mon Sep 17 00:00:00 2001 From: MinRK Date: Mon, 9 Apr 2012 15:34:54 -0700 Subject: [PATCH 3/3] document initially hidden javascript container --- IPython/frontend/html/notebook/static/js/codecell.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/IPython/frontend/html/notebook/static/js/codecell.js b/IPython/frontend/html/notebook/static/js/codecell.js index 84055b7b0..5119461d8 100644 --- a/IPython/frontend/html/notebook/static/js/codecell.js +++ b/IPython/frontend/html/notebook/static/js/codecell.js @@ -686,13 +686,14 @@ var IPython = (function (IPython) { }; - CodeCell.prototype.append_javascript = function (js, e) { + CodeCell.prototype.append_javascript = function (js, container) { // We just eval the JS code, element appears in the local scope. var element = $("
").addClass("box_flex1 output_subarea"); - e.append(element); + container.append(element); // Div for js shouldn't be drawn, as it will add empty height to the area. - e.hide(); - + container.hide(); + // If the Javascript appends content to `element` that should be drawn, then + // it must also call `container.show()`. eval(js); }