|
|
|
|
|
define([ |
|
'base/js/namespace', |
|
'notebook/js/outputarea', |
|
'notebook/js/codecell', |
|
], function( |
|
Jupyter, |
|
oa, |
|
cc |
|
) { |
|
"use strict"; |
|
|
|
|
|
var params = { |
|
|
|
limit_output : 10000, |
|
limit_stream : true, |
|
limit_execute_result : true, |
|
limit_display_data : false, |
|
|
|
limit_output_message : '<b>limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters</b>' |
|
}; |
|
|
|
|
|
|
|
var update_params = function() { |
|
var config = Jupyter.notebook.config; |
|
for (var key in params) { |
|
if (config.data.hasOwnProperty(key) ){ |
|
params[key] = config.data[key]; |
|
} |
|
} |
|
}; |
|
|
|
function is_finite_number (n) { |
|
n = parseFloat(n); |
|
return !isNaN(n) && isFinite(n); |
|
} |
|
|
|
var initialize = function () { |
|
update_params(); |
|
|
|
params.limit_output = parseFloat(params.limit_output); |
|
var old_handle_output = oa.OutputArea.prototype.handle_output; |
|
oa.OutputArea.prototype.handle_output = function (msg) { |
|
var handled_msg_types = ['stream', 'execute_result', 'display_data']; |
|
if (handled_msg_types.indexOf(msg.header.msg_type) < 0) { |
|
return old_handle_output.apply(this, arguments); |
|
} |
|
else { |
|
|
|
|
|
var MAX_CHARACTERS = params.limit_output; |
|
var cell_metadata = this.element.closest('.cell').data('cell').metadata; |
|
if (is_finite_number(cell_metadata.limit_output)) { |
|
MAX_CHARACTERS = parseFloat(cell_metadata.limit_output); |
|
} |
|
|
|
|
|
var count = this.element.data('limit_output_count') || 0; |
|
|
|
var old_count = count; |
|
if (msg.header.msg_type === "stream" && params.limit_stream) { |
|
count += String(msg.content.text).length; |
|
} |
|
else { |
|
if ((msg.header.msg_type === "execute_result" && params.limit_execute_result) || |
|
(msg.header.msg_type === "display_data" && params.limit_display_data)) { |
|
count += Math.max( |
|
(msg.content.data['text/plain'] === undefined) ? 0 : String(msg.content.data['text/plain']).length, |
|
(msg.content.data['text/html'] === undefined) ? 0 : String(msg.content.data['text/html']).length |
|
); |
|
} |
|
|
|
} |
|
|
|
this.element.data('limit_output_count', count); |
|
|
|
if (count <= MAX_CHARACTERS) { |
|
return old_handle_output.apply(this, arguments); |
|
} |
|
|
|
if (old_count <= MAX_CHARACTERS) { |
|
|
|
var to_add = MAX_CHARACTERS - old_count; |
|
if (msg.header.msg_type === "stream") { |
|
msg.content.text = msg.content.text.substr(0, to_add); |
|
} |
|
else { |
|
if (msg.content.data['text/plain'] !== undefined) { |
|
msg.content.data['text/plain'] = msg.content.data['text/plain'].substr(0, to_add); |
|
} |
|
if (msg.content.data['text/html'] !== undefined) { |
|
msg.content.data['text/html'] = msg.content.data['text/html'].substr(0, to_add); |
|
} |
|
} |
|
old_handle_output.apply(this, arguments); |
|
|
|
|
|
console.log( |
|
"limit_output: Maximum message size of", MAX_CHARACTERS, |
|
"exceeded with", count, "characters. Further output muted." |
|
); |
|
|
|
var limitmsg = params.limit_output_message.replace("{message_type}", msg.header.msg_type) |
|
.replace("{limit_output_length}", MAX_CHARACTERS) |
|
.replace("{output_length}", count); |
|
this.append_output({ |
|
"output_type": "display_data", |
|
"metadata": {}, |
|
"data": {"text/html": limitmsg} |
|
}); |
|
} |
|
} |
|
}; |
|
|
|
var old_clear_output = oa.OutputArea.prototype.clear_output; |
|
oa.OutputArea.prototype.clear_output = function () { |
|
|
|
this.element.data('limit_output_count', 0); |
|
return old_clear_output.apply(this, arguments); |
|
}; |
|
}; |
|
|
|
var load_ipython_extension = function() { |
|
return Jupyter.notebook.config.loaded.then(initialize); |
|
}; |
|
|
|
return { |
|
load_ipython_extension : load_ipython_extension |
|
}; |
|
}); |
|
|