Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/limit output2 #882

Merged
merged 3 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ Parameters:
default: 10000
step: 1
min: 0
- name: limit_stream
description: Enable limiting stream messages
input_type: checkbox
default: true
- name: limit_execute_result
description: Enable limiting execute_result messages
input_type: checkbox
default: true
- name: limit_display_data
description: Enable limiting display_data messages
input_type: checkbox
default: false
- name: limit_output_message
description: Message to append when output is limited
input_type: text
default: '<b>limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters</b>'
default: '<b>limit_output extension: Maximum message size for {message_type} of {limit_output_length} exceeded with {output_length} characters</b>'
24 changes: 16 additions & 8 deletions src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ define([
var params = {
// maximum number of characters the output area is allowed to print
limit_output : 10000,
limit_stream : true,
limit_execute_result : true,
limit_display_data : false,
// message to print when output is limited
limit_output_message : '<b>limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters</b>'
};
Expand All @@ -38,7 +41,6 @@ define([
update_params();
// sometimes limit_output metadata val can get stored as a string
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'];
Expand All @@ -47,6 +49,7 @@ define([
}
else {
// get MAX_CHARACTERS from cell metadata if present, otherwise param
//msg.header.msg_type
var MAX_CHARACTERS = params.limit_output;
var cell_metadata = this.element.closest('.cell').data('cell').metadata;
if (is_finite_number(cell_metadata.limit_output)) {
Expand All @@ -57,14 +60,18 @@ define([
var count = this.element.data('limit_output_count') || 0;
// update count with the length of this message
var old_count = count;
if (msg.header.msg_type === "stream") {
if (msg.header.msg_type === "stream" && params.limit_stream) {
count += String(msg.content.text).length;
}
else {
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
);
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
);
}

}
// save updated count
this.element.data('limit_output_count', count);
Expand Down Expand Up @@ -95,12 +102,13 @@ define([
"exceeded with", count, "characters. Further output muted."
);
// allow simple substitutions for output length for quick debugging
var limitmsg = params.limit_output_message.replace("{limit_output_length}", MAX_CHARACTERS)
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": {}, // included to avoid warning
"data": {"text/html": limitmsg},
"data": {"text/html": limitmsg}
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ counted, and if either exceeds `limit_output` characters will be truncated to
The `limit_output_message` parameter can be formatted to display the
`limit_output` length and the current `output_length`, using the respective
replacement fields `{limit_output_length}` and `{output_length}`.

### Parameter Overview

* limit_output - Number of characters to limit output to
* limit_stream - Enable limiting stream messages
* limit_execute_result - Enable limiting execute_result messages
* limit_display_data - Enable limiting display_data messages
* limit_output_message - Message to append when output is limited