Skip to content

Commit fc84ae5

Browse files
Merge pull request #3290 from SylvainCorlay/raise-on-failure-to-render
Backport PR #3280 on branch 7.x (Widgetsnbextension: throw error on failure to render)
2 parents 1f9917d + 5070686 commit fc84ae5

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

widgetsnbextension/src/extension.js

+26-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ require("./save_state");
2020
require("./embed_widgets");
2121
var PhosphorWidget = require("@lumino/widgets");
2222

23+
var NOTEBOOK_VERSION_INFO = Jupyter.version.split('.');
24+
var NOTEBOOK_MAJOR = parseInt(NOTEBOOK_VERSION_INFO[0]);
25+
var NOTEBOOK_MINOR = parseInt(NOTEBOOK_VERSION_INFO[1]);
26+
var NOTEBOOK_PATCH = parseInt(NOTEBOOK_VERSION_INFO[2]);
27+
28+
var RENDER_SHOULD_THROW =
29+
NOTEBOOK_MAJOR > 6 ||
30+
(NOTEBOOK_MAJOR == 6 && NOTEBOOK_MINOR > 4) ||
31+
(NOTEBOOK_MAJOR == 6 && NOTEBOOK_MINOR == 4 && NOTEBOOK_PATCH > 4);
32+
2333
/**
2434
* Create a widget manager for a kernel instance.
2535
*/
@@ -104,12 +114,19 @@ function register_events(Jupyter, events, outputarea) {
104114
// data is a model id
105115
var manager = Jupyter.notebook && Jupyter.notebook.kernel && Jupyter.notebook.kernel.widget_manager;
106116
if (!manager) {
107-
node.textContent = "Error rendering Jupyter widget: missing widget manager";
117+
var msg = 'Error rendering Jupyter widget: missing widget manager';
118+
if (RENDER_SHOULD_THROW) {
119+
throw new Error(msg);
120+
}
121+
node.textContent = msg;
108122
return;
109123
}
110124

111125
// Missing model id means the view was removed. Hide this element.
112126
if (data.model_id === '') {
127+
if (RENDER_SHOULD_THROW) {
128+
throw new Error('Jupyter Widgets model not found');
129+
}
113130
node.style.display = 'none';
114131
return;
115132
}
@@ -134,7 +151,13 @@ function register_events(Jupyter, events, outputarea) {
134151
})
135152
});
136153
} else {
137-
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.';
154+
var msg =
155+
'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.';
156+
if (RENDER_SHOULD_THROW) {
157+
throw new Error(msg);
158+
}
159+
node.textContent = msg;
160+
return;
138161
}
139162
}
140163

@@ -146,6 +169,7 @@ function register_events(Jupyter, events, outputarea) {
146169
element.append(toinsert);
147170
return toinsert;
148171
};
172+
149173
// Register mime type with the output area
150174
outputarea.OutputArea.prototype.register_mime_type(MIME_TYPE, append_mime, {
151175
// An output widget could contain arbitrary user javascript

0 commit comments

Comments
 (0)