Skip to content

Commit c7869f1

Browse files
committed
Error if cells that produce no output are embedded
1 parent 6c73029 commit c7869f1

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

src/core/jupyter/jupyter-embed.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,15 +240,21 @@ async function notebookMarkdown(
240240

241241
// Wrap any injected cells with a div that includes a back link to
242242
// the notebook that originated the cells
243-
const notebookMarkdown = (cells: JupyterCellOutput[], title?: string) => {
243+
const notebookMarkdown = (
244+
nbAddress: JupyterNotebookAddress,
245+
cells: JupyterCellOutput[],
246+
title?: string,
247+
) => {
244248
const markdown = [
245249
"",
246250
`:::{notebook="${nbAddress.path}" ${
247251
title ? `notebook-title="${title}"` : ""
248252
}}`,
249253
];
254+
255+
const cellOutput = cells.map((cell) => cell.markdown).join("");
250256
markdown.push("");
251-
markdown.push(cells.map((cell) => cell.markdown).join(""));
257+
markdown.push(cellOutput);
252258
markdown.push("");
253259
markdown.push(":::");
254260
return markdown.join("\n");
@@ -264,26 +270,48 @@ async function notebookMarkdown(
264270
throw new Error(
265271
`The cell ${id} does not exist in notebook`,
266272
);
273+
} else if (cell.markdown.trim() === "") {
274+
throw new Error(
275+
`The notebook ${nbAddress.path} doesn't contain output to embed with the cell id, tag, or label '${id}'. Please be sure to have executed any cells that you are embedding.`,
276+
);
267277
} else {
268278
return cell;
269279
}
270280
});
271-
return notebookMarkdown(theCells, notebookInfo.title);
281+
return notebookMarkdown(nbAddress, theCells, notebookInfo.title);
272282
} else if (nbAddress.indexes) {
273283
// Filter and sort based upon cell indexes
274284
const theCells = nbAddress.indexes.map((idx) => {
275285
if (idx < 1 || idx >= notebookInfo.outputs.length) {
276286
throw new Error(
277287
`The cell index ${idx} isn't within the range of cells`,
278288
);
289+
} else {
290+
const cell = notebookInfo.outputs[idx - 1];
291+
if (cell.markdown.trim() === "") {
292+
throw new Error(
293+
`The notebook ${nbAddress.path} doesn't contain output to embed for the cell in position ${idx}. Please be sure to have executed any cells that you are embedding.`,
294+
);
295+
}
296+
return cell;
279297
}
280-
return notebookInfo.outputs[idx - 1];
281298
});
282-
return notebookMarkdown(theCells, notebookInfo.title);
299+
return notebookMarkdown(nbAddress, theCells, notebookInfo.title);
283300
} else {
284301
// Return all the cell outputs as there is no addtional
285302
// specification of cells
286-
return notebookMarkdown(notebookInfo.outputs, notebookInfo.title);
303+
const notebookMd = notebookMarkdown(
304+
nbAddress,
305+
notebookInfo.outputs,
306+
notebookInfo.title,
307+
);
308+
if (notebookMd.trim() === "") {
309+
throw new Error(
310+
`The notebook ${nbAddress.path} doesn't contain output to embed. Please be sure to have executed any cells that you are embedding.`,
311+
);
312+
} else {
313+
return notebookMd;
314+
}
287315
}
288316
}
289317

0 commit comments

Comments
 (0)