Skip to content

Commit

Permalink
Merge pull request #3290 from SylvainCorlay/raise-on-failure-to-render
Browse files Browse the repository at this point in the history
Backport PR #3280 on branch 7.x (Widgetsnbextension: throw error on failure to render)
  • Loading branch information
SylvainCorlay authored Sep 30, 2021
2 parents 1f9917d + 5070686 commit fc84ae5
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions widgetsnbextension/src/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
}

Expand All @@ -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
Expand Down

0 comments on commit fc84ae5

Please sign in to comment.