-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNotebookRender.tsx
78 lines (69 loc) · 1.99 KB
/
NotebookRender.tsx
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Code taken from https://github.com/sdobz/notebook-render/blob/master/src/index.tsx
import { IpynbRenderer } from "react-ipynb-renderer";
import type { BaseProps, CellType, LanguageType } from "react-ipynb-renderer";
type BaseIpynb = BaseProps["ipynb"];
interface IIpynb extends BaseIpynb {
metadata?: {
hide_input?: boolean;
inputHidden?: boolean;
kernelspec?: {
language?: string;
};
};
}
interface CellWithMetadata extends CellType {
metadata?: {
hide_input?: boolean;
inputHidden?: boolean;
};
}
interface NotebookRenderProps {
displayOrder: string[];
notebook: IIpynb;
theme: "light" | "dark";
showPrompt: boolean;
sourceClassName: string;
}
function processedNotebook(notebook: IIpynb): IIpynb {
const allInputHidden = notebook.metadata?.hide_input || false;
const cells = notebook.cells.map((cell) => {
const c = { ...cell } as unknown as CellWithMetadata;
const inputHidden =
allInputHidden || c.metadata?.inputHidden || c.metadata?.hide_input;
if (inputHidden) {
if (c.cell_type === "code") {
delete c["input"];
delete c["source"];
}
}
c.outputs = c.outputs?.map((o) => {
const oCopy = { ...o };
if (Object.keys(oCopy.data ?? {}).length == 0) oCopy.data = undefined;
return oCopy;
});
return c;
});
const result = { ...notebook, cells };
return result;
}
// Converts style in string to JSON object
export default function NotebookRender(props: NotebookRenderProps) {
const notebook = props.notebook as unknown as IIpynb;
const language = notebook.metadata?.kernelspec?.language ?? "python";
return (
<IpynbRenderer
ipynb={processedNotebook(notebook)}
syntaxTheme="prism"
language={language as LanguageType}
bgTransparent={false}
markdownOptions={{
mathjaxOptions: {
// https://docs.mathjax.org/en/v3.0-latest/options/input/tex.html
tex: {
tags: "ams",
},
},
}}
/>
);
}