|
| 1 | +/* |
| 2 | +* jupyter-shared.ts |
| 3 | +* |
| 4 | +* Copyright (C) 2020-2023 Posit Software, PBC |
| 5 | +* |
| 6 | +*/ |
| 7 | + |
| 8 | +import { |
| 9 | + JupyterNotebook, |
| 10 | + JupyterOutput, |
| 11 | + JupyterToMarkdownOptions, |
| 12 | +} from "./types.ts"; |
| 13 | + |
| 14 | +function fixupBokehCells( |
| 15 | + nb: JupyterNotebook, |
| 16 | + _options: JupyterToMarkdownOptions, |
| 17 | +): JupyterNotebook { |
| 18 | + for (const cell of nb.cells) { |
| 19 | + if (cell.cell_type === "code") { |
| 20 | + let needsFixup = false; |
| 21 | + for (const output of cell?.outputs ?? []) { |
| 22 | + if (output.data === undefined) { |
| 23 | + continue; |
| 24 | + } |
| 25 | + if (output.data["application/vnd.bokehjs_load.v0+json"]) { |
| 26 | + needsFixup = true; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + if (!needsFixup) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + const asTextHtml = (data: Record<string, unknown>) => { |
| 34 | + if (data["text/html"]) { |
| 35 | + return data["text/html"]; |
| 36 | + } |
| 37 | + if (data["application/javascript"]) { |
| 38 | + return [ |
| 39 | + "<script>", |
| 40 | + ...data["application/javascript"] as string[], |
| 41 | + "</script>", |
| 42 | + ]; |
| 43 | + } |
| 44 | + throw new Error( |
| 45 | + "Internal Error: Unknown data types " + |
| 46 | + JSON.stringify(Object.keys(data)), |
| 47 | + ); |
| 48 | + }; |
| 49 | + |
| 50 | + // bokeh emits one 'initialization' cell once per notebook, |
| 51 | + // and then two cells per plot. So we merge the three first cells into |
| 52 | + // one, and then merge every two cells after that. |
| 53 | + |
| 54 | + const oldOutputs = cell.outputs!; |
| 55 | + |
| 56 | + const newOutputs: JupyterOutput[] = [ |
| 57 | + { |
| 58 | + metadata: {}, |
| 59 | + output_type: "display_data", |
| 60 | + data: { |
| 61 | + "text/html": [ |
| 62 | + asTextHtml(oldOutputs[0].data!), |
| 63 | + asTextHtml(oldOutputs[1].data!), |
| 64 | + asTextHtml(oldOutputs[2].data!), |
| 65 | + ].flat(), |
| 66 | + }, |
| 67 | + }, |
| 68 | + ]; |
| 69 | + for (let i = 3; i < oldOutputs.length; i += 2) { |
| 70 | + newOutputs.push({ |
| 71 | + metadata: {}, |
| 72 | + output_type: "display_data", |
| 73 | + data: { |
| 74 | + "text/html": [ |
| 75 | + asTextHtml(oldOutputs[i].data!), |
| 76 | + asTextHtml(oldOutputs[i + 1].data!), |
| 77 | + ].flat(), |
| 78 | + }, |
| 79 | + }); |
| 80 | + } |
| 81 | + cell.outputs = newOutputs; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return nb; |
| 86 | +} |
| 87 | + |
| 88 | +const fixups: (( |
| 89 | + nb: JupyterNotebook, |
| 90 | + options: JupyterToMarkdownOptions, |
| 91 | +) => JupyterNotebook)[] = [ |
| 92 | + fixupBokehCells, |
| 93 | +]; |
| 94 | + |
| 95 | +export function fixupJupyterNotebook( |
| 96 | + nb: JupyterNotebook, |
| 97 | + options: JupyterToMarkdownOptions, |
| 98 | +): JupyterNotebook { |
| 99 | + for (const fixup of fixups) { |
| 100 | + nb = fixup(nb, options); |
| 101 | + } |
| 102 | + return nb; |
| 103 | +} |
0 commit comments