From 507068602117ee7176b7f4ae3f72d57dbc80b8dc Mon Sep 17 00:00:00 2001 From: Sylvain Corlay Date: Tue, 28 Sep 2021 19:11:09 +0200 Subject: [PATCH] Merge pull request #3280 from martinRenou/throw_error_on_failure_to_render Widgetsnbextension: throw error on failure to render --- widgetsnbextension/src/extension.js | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/widgetsnbextension/src/extension.js b/widgetsnbextension/src/extension.js index c37e154868..d320caed58 100644 --- a/widgetsnbextension/src/extension.js +++ b/widgetsnbextension/src/extension.js @@ -20,6 +20,16 @@ require("./save_state"); require("./embed_widgets"); var PhosphorWidget = require("@lumino/widgets"); +var NOTEBOOK_VERSION_INFO = Jupyter.version.split('.'); +var NOTEBOOK_MAJOR = parseInt(NOTEBOOK_VERSION_INFO[0]); +var NOTEBOOK_MINOR = parseInt(NOTEBOOK_VERSION_INFO[1]); +var NOTEBOOK_PATCH = parseInt(NOTEBOOK_VERSION_INFO[2]); + +var RENDER_SHOULD_THROW = + NOTEBOOK_MAJOR > 6 || + (NOTEBOOK_MAJOR == 6 && NOTEBOOK_MINOR > 4) || + (NOTEBOOK_MAJOR == 6 && NOTEBOOK_MINOR == 4 && NOTEBOOK_PATCH > 4); + /** * Create a widget manager for a kernel instance. */ @@ -104,12 +114,19 @@ function register_events(Jupyter, events, outputarea) { // data is a model id var manager = Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.widget_manager; if (!manager) { - node.textContent = "Error rendering Jupyter widget: missing widget manager"; + var msg = 'Error rendering Jupyter widget: missing widget manager'; + if (RENDER_SHOULD_THROW) { + throw new Error(msg); + } + node.textContent = msg; return; } // Missing model id means the view was removed. Hide this element. if (data.model_id === '') { + if (RENDER_SHOULD_THROW) { + throw new Error('Jupyter Widgets model not found'); + } node.style.display = 'none'; return; } @@ -134,7 +151,13 @@ function register_events(Jupyter, events, outputarea) { }) }); } else { - node.textContent = 'A Jupyter widget could not be displayed because the widget state could not be found. This could happen if the kernel storing the widget is no longer available, or if the widget state was not saved in the notebook. You may be able to create the widget by running the appropriate cells.'; + var msg = + 'A Jupyter widget could not be displayed because the widget state could not be found. This could happen if the kernel storing the widget is no longer available, or if the widget state was not saved in the notebook. You may be able to create the widget by running the appropriate cells.'; + if (RENDER_SHOULD_THROW) { + throw new Error(msg); + } + node.textContent = msg; + return; } } @@ -146,6 +169,7 @@ function register_events(Jupyter, events, outputarea) { element.append(toinsert); return toinsert; }; + // Register mime type with the output area outputarea.OutputArea.prototype.register_mime_type(MIME_TYPE, append_mime, { // An output widget could contain arbitrary user javascript