From 7673d5db1368caf187e8b43e5da776b9b34d99e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ju=CC=88rgen=20Hasch?= Date: Wed, 8 Feb 2017 20:57:29 +0100 Subject: [PATCH 1/3] Add different limits for stream types --- .../nbextensions/limit_output/limit-output.yaml | 16 ++++++++++++++-- .../nbextensions/limit_output/main.js | 15 +++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml index eb7a390e6..02e4da8da 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml @@ -6,7 +6,19 @@ Icon: icon.png Main: main.js Compatibility: 4.x Parameters: -- name: limit_output +- name: limit_stream + description: Number of characters to limit output to + input_type: number + default: 10000 + step: 1 + min: 0 +- name: limit_execute_result + description: Number of characters to limit output to + input_type: number + default: 10000 + step: 1 + min: 0 +- name: limit_display_data description: Number of characters to limit output to input_type: number default: 10000 @@ -15,4 +27,4 @@ Parameters: - name: limit_output_message description: Message to append when output is limited input_type: text - default: 'limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters' + default: 'limit_output extension: Maximum message size for {message_type} of {limit_output_length} exceeded with {output_length} characters' diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js index 31e0aa3e7..36ec33163 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js @@ -14,7 +14,9 @@ define([ // define default values for config parameters var params = { // maximum number of characters the output area is allowed to print - limit_output : 10000, + limit_stream : 10000, + limit_execute_result : 10000, + limit_display_data : 10000, // message to print when output is limited limit_output_message : 'limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters' }; @@ -37,7 +39,10 @@ define([ config.loaded.then(function() { update_params(); // sometimes limit_output metadata val can get stored as a string - params.limit_output = parseFloat(params.limit_output); + //params.limit_output = parseFloat(params.limit_output); + params.limit_stream = parseFloat(params.limit_stream); + params.limit_execute_result = parseFloat(params.limit_execute_result); + params.limit_display_data = parseFloat(params.limit_display_data); var old_handle_output = oa.OutputArea.prototype.handle_output; oa.OutputArea.prototype.handle_output = function (msg) { @@ -47,7 +52,8 @@ define([ } else { // get MAX_CHARACTERS from cell metadata if present, otherwise param - var MAX_CHARACTERS = params.limit_output; + //msg.header.msg_type + var MAX_CHARACTERS = 10000 ; //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); @@ -95,7 +101,8 @@ 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", From be5ada586494df7e57a937a4e04d7caeba253330 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ju=CC=88rgen=20Hasch?= Date: Sat, 11 Feb 2017 17:30:10 +0100 Subject: [PATCH 2/3] Allow selecting message type for limiting output --- .../limit_output/limit-output.yaml | 22 ++++++------- .../nbextensions/limit_output/main.js | 31 ++++++++++--------- .../nbextensions/limit_output/readme.md | 10 ++++++ 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml index 02e4da8da..951822b2f 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/limit-output.yaml @@ -6,24 +6,24 @@ Icon: icon.png Main: main.js Compatibility: 4.x Parameters: -- name: limit_stream +- name: limit_output description: Number of characters to limit output to input_type: number default: 10000 step: 1 min: 0 +- name: limit_stream + description: Enable limiting stream messages + input_type: checkbox + default: true - name: limit_execute_result - description: Number of characters to limit output to - input_type: number - default: 10000 - step: 1 - min: 0 + description: Enable limiting execute_result messages + input_type: checkbox + default: true - name: limit_display_data - description: Number of characters to limit output to - input_type: number - default: 10000 - step: 1 - min: 0 + 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 diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js index 36ec33163..cee27c055 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/main.js @@ -14,9 +14,10 @@ define([ // define default values for config parameters var params = { // maximum number of characters the output area is allowed to print - limit_stream : 10000, - limit_execute_result : 10000, - limit_display_data : 10000, + limit_output : 10000, + limit_stream : true, + limit_execute_result : true, + limit_display_data : false, // message to print when output is limited limit_output_message : 'limit_output extension: Maximum message size of {limit_output_length} exceeded with {output_length} characters' }; @@ -39,11 +40,7 @@ define([ config.loaded.then(function() { update_params(); // sometimes limit_output metadata val can get stored as a string - //params.limit_output = parseFloat(params.limit_output); - params.limit_stream = parseFloat(params.limit_stream); - params.limit_execute_result = parseFloat(params.limit_execute_result); - params.limit_display_data = parseFloat(params.limit_display_data); - + 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']; @@ -53,7 +50,7 @@ define([ else { // get MAX_CHARACTERS from cell metadata if present, otherwise param //msg.header.msg_type - var MAX_CHARACTERS = 10000 ; //params.limit_output; + 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); @@ -63,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); @@ -107,7 +108,7 @@ define([ this.append_output({ "output_type": "display_data", "metadata": {}, // included to avoid warning - "data": {"text/html": limitmsg}, + "data": {"text/html": limitmsg} }); } } diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md index 97764ce38..72c856a01 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md @@ -37,3 +37,13 @@ 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 | Type | Description +----------|------|------------- +limit_output | number | Number of characters to limit output to +limit_stream | bool | Enable limiting stream messages +limit_execute_result | bool | Enable limiting execute_result messages +limit_display_data | bool | Enable limiting display_data messages +limit_output_message | string | Message to append when output is limited + From 6b3611d6fa99850753c484f243dd202c10394ab2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ju=CC=88rgen=20Hasch?= Date: Mon, 13 Feb 2017 21:22:12 +0100 Subject: [PATCH 3/3] Don't use tables. --- .../nbextensions/limit_output/readme.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md index 72c856a01..badc4e4d4 100644 --- a/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md +++ b/src/jupyter_contrib_nbextensions/nbextensions/limit_output/readme.md @@ -38,12 +38,11 @@ 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 -Parameter | Type | Description -----------|------|------------- -limit_output | number | Number of characters to limit output to -limit_stream | bool | Enable limiting stream messages -limit_execute_result | bool | Enable limiting execute_result messages -limit_display_data | bool | Enable limiting display_data messages -limit_output_message | string | Message to append when output is limited +* 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