|
| 1 | +/*************************************************************************** |
| 2 | + * Copyright (c) 2018, Voilà contributors * |
| 3 | + * Copyright (c) 2018, QuantStack * |
| 4 | + * * |
| 5 | + * Distributed under the terms of the BSD 3-Clause License. * |
| 6 | + * * |
| 7 | + * The full license is in the file LICENSE, distributed with this software. * |
| 8 | + ****************************************************************************/ |
| 9 | + |
| 10 | +import { |
| 11 | + JupyterFrontEnd, |
| 12 | + JupyterFrontEndPlugin |
| 13 | +} from '@jupyterlab/application'; |
| 14 | + |
| 15 | +import { IRenderMimeRegistry } from '@jupyterlab/rendermime'; |
| 16 | + |
| 17 | +import { Widget } from '@lumino/widgets'; |
| 18 | + |
| 19 | +/** |
| 20 | + * The plugin that renders outputs. |
| 21 | + */ |
| 22 | +export const renderOutputsPlugin: JupyterFrontEndPlugin<void> = { |
| 23 | + id: '@voila-dashboards/voila:render-outputs', |
| 24 | + autoStart: true, |
| 25 | + requires: [IRenderMimeRegistry], |
| 26 | + activate: async ( |
| 27 | + app: JupyterFrontEnd, |
| 28 | + rendermime: IRenderMimeRegistry |
| 29 | + ): Promise<void> => { |
| 30 | + // Render outputs |
| 31 | + const cellOutputs = document.body.querySelectorAll( |
| 32 | + 'script[type="application/vnd.voila.cell-output+json"]' |
| 33 | + ); |
| 34 | + cellOutputs.forEach(async (cellOutput) => { |
| 35 | + const model = JSON.parse(cellOutput.innerHTML); |
| 36 | + |
| 37 | + const mimeType = rendermime.preferredMimeType(model.data, 'any'); |
| 38 | + |
| 39 | + if (!mimeType) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + const output = rendermime.createRenderer(mimeType); |
| 43 | + output.renderModel(model).catch((error) => { |
| 44 | + // Manually append error message to output |
| 45 | + const pre = document.createElement('pre'); |
| 46 | + pre.textContent = `Javascript Error: ${error.message}`; |
| 47 | + output.node.appendChild(pre); |
| 48 | + |
| 49 | + // Remove mime-type-specific CSS classes |
| 50 | + pre.className = 'lm-Widget jp-RenderedText'; |
| 51 | + pre.setAttribute('data-mime-type', 'application/vnd.jupyter.stderr'); |
| 52 | + }); |
| 53 | + |
| 54 | + output.addClass('jp-OutputArea-output'); |
| 55 | + |
| 56 | + if (cellOutput.parentElement) { |
| 57 | + const container = cellOutput.parentElement; |
| 58 | + |
| 59 | + container.removeChild(cellOutput); |
| 60 | + |
| 61 | + // Attach output |
| 62 | + Widget.attach(output, container); |
| 63 | + } |
| 64 | + }); |
| 65 | + const node = document.getElementById('rendered_cells'); |
| 66 | + if (node) { |
| 67 | + const cells = new Widget({ node }); |
| 68 | + app.shell.add(cells, 'main'); |
| 69 | + } |
| 70 | + } |
| 71 | +}; |
0 commit comments