-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataTableRenderer.ts
64 lines (57 loc) · 1.56 KB
/
dataTableRenderer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import type {
OutputItem,
RendererContext
} from 'vscode-notebook-renderer';
import { OutputLoader } from './outputLoader';
import './styles.css';
const inputs = require('@observablehq/inputs');
/**
* Notebook cell output render info.
*/
interface IRenderInfo {
container: HTMLElement;
mimeType: string;
value: OutputItem;
context: RendererContext<unknown>;
}
/**
* Renders notebook cell output.
* @param output Notebook cell output info to render.
*/
export function render(output: IRenderInfo) {
console.log(`data.table:mimeType: ${output.mimeType}`);
// load and parse output data
const outputLoader: OutputLoader = new OutputLoader(output.value, output.mimeType);
let data: any = outputLoader.getData();
if (Array.isArray(data)) {
// create data table view
const table = inputs.table(data, {
layout: 'auto',
width: 'auto',
height: 360,
});
// add table to cell data output container
output.container.appendChild(table);
}
else {
// create text output display nodes
const pre = document.createElement('pre');
pre.className = 'text-output';
const code = document.createElement('code');
if (typeof data !== 'string') {
// stringify json data
code.textContent = JSON.stringify(data, null, 2);
}
else {
// show cell output text
code.textContent = output.value.text();
}
pre.appendChild(code);
output.container.appendChild(pre);
}
}
if (module.hot) {
module.hot.addDisposeHandler(() => {
// TODO: dispose resources and save renderer state
});
}