CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/jupyter/nbgrader/jupyter-parse.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { readFile } from "node:fs/promises";
7
8
// Strip output and attachments from all cells.
9
export async function jupyter_strip_notebook(
10
ipynb_path: string
11
): Promise<string> {
12
// Load the file
13
const contents = (await readFile(ipynb_path)).toString();
14
15
// Parse as JSON
16
const obj: any = JSON.parse(contents);
17
18
// Strip output from cells
19
if (obj != null && obj.cells != null) {
20
for (const cell of obj.cells) {
21
if (cell.outputs != null) {
22
// Just deleting this field would result in an invalid ipynb file. I couldn't
23
// find a statement that this required in the nbformat spec, but testing
24
// the classic server implies that it is.
25
cell.outputs = [];
26
}
27
if (cell.attachments != null) {
28
delete cell.attachments;
29
}
30
}
31
}
32
33
// Return version converted back to a string.
34
return JSON.stringify(obj);
35
}
36
37