Public: anyone can read this; the crew can change it.

A Jupyter notebook opens read-only in the browser as its cells and outputs #16002

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/15981-a-jupyter-notebook-opens-read-only-in-th → main 0 commits fork 2 threads · 2 unresolved

  • Merged. mason/15981-a-jupyter-notebook-opens-read-only-in-th was merged into main as bffce9a.

Review

Sign in to review this change.
6 files changed +37 −4
docs/features/notebook.md +3 −3
@@ -5,6 +5,6 @@ row: Jupyter notebooks
55 # Jupyter notebooks
66
77 A `.ipynb` file opens on the blob page as its cells and saved outputs (`src/web/render/notebook.ts`). Nothing is run.
8 Markdown cells go through the forge's Markdown renderer. A code cell is a `pre` headed `In [n]:` with its execution count. Output text (streams, plain text, errors with their colour codes removed) is escaped text. PNG and JPEG outputs are pictures, drawn only when the base64 is well-formed, and shrink to fit a phone. HTML and script outputs are shown as escaped text and never inserted or run. Other output kinds are skipped.
9 A notebook over 1 MB, one that is not valid JSON, or one with no `cells` list shows the ordinary source view with one line saying why. At most 2,000 cells, and 2,000 outputs per cell, are drawn. `?view=source`, Raw and Download show the bytes untouched.
10 The terminal (SSH) file view has no notebook reader: it shows the file as plain text, the JSON itself, escaped like any text file. Nothing in it is run there either. Reading cells in the terminal is left out. Classes live in `docs/design/areas/render.md`.
8 Markdown cells go through the forge's Markdown renderer. A code cell is a `pre` headed `In [n]:` with its execution count. Output text (streams, plain text, errors with their colour codes removed) is escaped text. PNG and JPEG outputs are pictures, drawn only when the base64 is well-formed and the picture is under 1 MB, and shrink to fit a phone; a larger one is a note, "Image output over 1 MB, not drawn". HTML and script outputs are shown as escaped text and never inserted or run. Other output kinds are skipped.
9 A notebook over 1 MB shows the source view with the sentence "This notebook is over 1 MB, so it is not drawn as cells (this one is N MB)", and Raw and Download are in the header. One that is not valid JSON, or one with no `cells` list shows the ordinary source view with one line saying why. At most 2,000 cells, and 2,000 outputs per cell, are drawn. `?view=source`, Raw and Download show the bytes untouched.
10 The terminal workspace reads the same cells (`src/tui/screens/workspace_kinds.ts`, from `parseNotebook`): code dimmed, errors in the danger colour, pictures as `[image output not shown]`, an over-1-MB picture as the same note. Nothing is run there either. Classes live in `docs/design/areas/render.md`.
src/web/pages/repo.ts +1 −1
@@ -1221,7 +1221,7 @@ const SAID_ABOVE = "Download it to read it.";
12211221 /** Why an archive too big to list is not listed, as one sentence. */
12221222 function overCap(path: string, blob: Blob): string | undefined {
12231223 if (kindByName(path) === "notebook" && blob.size > RENDER_CAP.notebook)
1224 return `This notebook is over ${RENDER_CAP.notebook / 1_000_000} MB, so it is not drawn as cells${blob.kind === "too_large" ? ". Download it to read it" : " and is shown as plain text"}.`;
1224 return `This notebook is over ${RENDER_CAP.notebook / 1_000_000} MB, so it is not drawn as cells (this one is ${fileSize(blob.size)})${blob.kind === "too_large" ? ". Download it to read it" : " and is shown as plain text"}.`;

No test covers this sentence; a test must fail without the size and show the download/raw link. Also the 1 MB figure differs from the issue's 2 MB.

12251225 if (kindByName(path) === "gcode" && blob.size > RENDER_CAP.gcode)
12261226 return `G-code, over the ${fileSize(RENDER_CAP.gcode)} the page draws, so its layers are not drawn. Download it to read it.`;
12271227 if (blob.kind !== "binary" || fileKind(path, blob.body.subarray(0, HEAD_BYTES)) !== "archive") return undefined;
src/web/render/notebook.ts +6
@@ -26,6 +26,11 @@ const SOURCES = [
2626 ] as const;
2727 /** The most outputs and cells drawn, so a hostile file cannot make a huge page. */
2828 const MAX_CELLS = 2_000;
29 /**
30 * An image output whose base64 is longer than this stands as a note: 1 MB of picture is 4/3 of that
31 * encoded.
32 */
33 const MAX_IMAGE_B64 = Math.ceil((1_000_000 * 4) / 3);
2934
3035 const stripAnsi = (s: string): string => s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, "");
3136
@@ -44,6 +49,7 @@ export interface NbCell {
4449
4550 function image(mime: string, data: unknown): NbOutput | undefined {
4651 const b64 = text(data).replace(/\s+/g, "");
52 if (b64.length > MAX_IMAGE_B64) return { kind: "caption", text: "Image output over 1 MB, not drawn" };
4753 return BASE64.test(b64) ? { kind: "image", mime, data: b64 } : undefined;

Good bound, but a caption hides which output was dropped and its size; fine to keep, but the terminal path needs a test too.

4854 }
4955
test/blob-notebook-page.test.ts +8
@@ -12,6 +12,7 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1212 import { serve, type Servers } from "../src/http/server.ts";
1313 import { seedAdmin } from "./helpers/admin.ts";
1414 import { quietLogs } from "./helpers/globals.ts";
15 import { fileSize } from "../src/web/ui.ts";
1516
1617 quietLogs();
1718
@@ -20,6 +21,7 @@ describe("a .ipynb on its blob page", () => {
2021 let app: App;
2122 let servers: Servers;
2223 let base = "";
24 let bigSize = "";
2325
2426 beforeAll(async () => {
2527 dataDir = await mkdtemp(join(tmpdir(), "forge-blob-ipynb-"));
@@ -34,6 +36,7 @@ describe("a .ipynb on its blob page", () => {
3436 cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }],
3537 });
3638 const big = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(1_100_000) }] });
39 bigSize = fileSize(new TextEncoder().encode(big).length);
3740 const made = await commitToBranch(app, a.value, created.value, {
3841 message: "notebooks",
3942 files: [
@@ -67,6 +70,11 @@ describe("a .ipynb on its blob page", () => {
6770 expect(text).toContain("This notebook is over 1 MB, so it is not drawn as cells");
6871 expect(text).not.toContain("data-plain-why");
6972 expect(text).not.toContain("In [");
73 // The sentence names this file's size, and the raw and download links are on the page.
74 expect(text).toContain(`(this one is ${bigSize})`);
75 expect(bigSize).toMatch(/^1\.\d MB$/);
76 expect(text).toContain("/alice/p/raw/main/big.ipynb");
77 expect(text).toContain("/alice/p/media/main/big.ipynb?download=1");
7078 });
7179
7280 test("invalid JSON says why on the page", async () => {
test/render-notebook.test.ts +11
@@ -81,3 +81,14 @@ describe("notebook edge cases", () => {
8181 });
8282
8383 });
84
85 describe("notebook image size", () => {
86 test("an image output over 1 MB is a note, one under it is a data: URI", () => {
87 const nb = (b64: string) =>
88 JSON.stringify({ cells: [{ cell_type: "code", source: "x", outputs: [{ output_type: "display_data", data: { "image/png": b64 } }] }] });
89 const big = String(draw(nb("A".repeat(1_400_000))).content);
90 expect(big).not.toContain("data:image/png");
91 expect(big).toContain("over 1 MB");
92 expect(String(draw(nb("AAAA")).content)).toContain("data:image/png;base64,AAAA");
93 });
94 });
test/tui-workspace-kinds.test.ts +8
@@ -61,6 +61,14 @@ describe("kind views, pure", () => {
6161 expect(v.plain.join("\n")).not.toContain("<pre");
6262 expect(v.plain.join("\n")).not.toContain("&amp;");
6363 });
64 test("an image output over 1 MB is a note in the terminal, not a picture", () => {
65 const nb = JSON.stringify({
66 cells: [{ cell_type: "code", source: "x", outputs: [{ output_type: "display_data", data: { "image/png": "A".repeat(1_400_000) } }] }],
67 });
68 const plain = notebookLines(nb, c)!.plain.join("\n");
69 expect(plain).toContain("Image output over 1 MB, not drawn");
70 expect(plain).not.toContain("AAAA");
71 });
6472 test("a malformed notebook has no view", () => {
6573 expect(kindView("a.ipynb", "{not json", c)).toBeNull();
6674 expect(kindView("a.ipynb", '{"nope":1}', c)).toBeNull();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs