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
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15981-a-jupyter-notebook-opens-read-only-in-th → main 0 commits fork 2 threads · 2 unresolved
6 files
changed +37
−4
docs/features/notebook.md +3 −3
| @@ -5,6 +5,6 @@ row: Jupyter notebooks | ||
| 5 | 5 | # Jupyter notebooks |
| 6 | 6 | |
| 7 | 7 | 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."; | ||
| 1221 | 1221 | /** Why an archive too big to list is not listed, as one sentence. */ |
| 1222 | 1222 | function overCap(path: string, blob: Blob): string | undefined { |
| 1223 | 1223 | 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"}.`; | |
| 1225 | 1225 | if (kindByName(path) === "gcode" && blob.size > RENDER_CAP.gcode) |
| 1226 | 1226 | return `G-code, over the ${fileSize(RENDER_CAP.gcode)} the page draws, so its layers are not drawn. Download it to read it.`; |
| 1227 | 1227 | 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 = [ | ||
| 26 | 26 | ] as const; |
| 27 | 27 | /** The most outputs and cells drawn, so a hostile file cannot make a huge page. */ |
| 28 | 28 | 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); | |
| 29 | 34 | |
| 30 | 35 | const stripAnsi = (s: string): string => s.replace(/\x1b\[[0-9;]*[A-Za-z]/g, ""); |
| 31 | 36 | |
| @@ -44,6 +49,7 @@ export interface NbCell { | ||
| 44 | 49 | |
| 45 | 50 | function image(mime: string, data: unknown): NbOutput | undefined { |
| 46 | 51 | 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" }; | |
| 47 | 53 | 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. |
||
| 48 | 54 | } |
| 49 | 55 |
test/blob-notebook-page.test.ts +8
| @@ -12,6 +12,7 @@ import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | ||
| 12 | 12 | import { serve, type Servers } from "../src/http/server.ts"; |
| 13 | 13 | import { seedAdmin } from "./helpers/admin.ts"; |
| 14 | 14 | import { quietLogs } from "./helpers/globals.ts"; |
| 15 | import { fileSize } from "../src/web/ui.ts"; | |
| 15 | 16 | |
| 16 | 17 | quietLogs(); |
| 17 | 18 | |
| @@ -20,6 +21,7 @@ describe("a .ipynb on its blob page", () => { | ||
| 20 | 21 | let app: App; |
| 21 | 22 | let servers: Servers; |
| 22 | 23 | let base = ""; |
| 24 | let bigSize = ""; | |
| 23 | 25 | |
| 24 | 26 | beforeAll(async () => { |
| 25 | 27 | dataDir = await mkdtemp(join(tmpdir(), "forge-blob-ipynb-")); |
| @@ -34,6 +36,7 @@ describe("a .ipynb on its blob page", () => { | ||
| 34 | 36 | cells: [{ cell_type: "code", execution_count: 1, source: "1", outputs: [{ output_type: "execute_result", data: { "text/html": "<script>alert(1)</script>" } }] }], |
| 35 | 37 | }); |
| 36 | 38 | const big = JSON.stringify({ cells: [{ cell_type: "markdown", source: "x".repeat(1_100_000) }] }); |
| 39 | bigSize = fileSize(new TextEncoder().encode(big).length); | |
| 37 | 40 | const made = await commitToBranch(app, a.value, created.value, { |
| 38 | 41 | message: "notebooks", |
| 39 | 42 | files: [ |
| @@ -67,6 +70,11 @@ describe("a .ipynb on its blob page", () => { | ||
| 67 | 70 | expect(text).toContain("This notebook is over 1 MB, so it is not drawn as cells"); |
| 68 | 71 | expect(text).not.toContain("data-plain-why"); |
| 69 | 72 | 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"); | |
| 70 | 78 | }); |
| 71 | 79 | |
| 72 | 80 | test("invalid JSON says why on the page", async () => { |
test/render-notebook.test.ts +11
| @@ -81,3 +81,14 @@ describe("notebook edge cases", () => { | ||
| 81 | 81 | }); |
| 82 | 82 | |
| 83 | 83 | }); |
| 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", () => { | ||
| 61 | 61 | expect(v.plain.join("\n")).not.toContain("<pre"); |
| 62 | 62 | expect(v.plain.join("\n")).not.toContain("&"); |
| 63 | 63 | }); |
| 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 | }); | |
| 64 | 72 | test("a malformed notebook has no view", () => { |
| 65 | 73 | expect(kindView("a.ipynb", "{not json", c)).toBeNull(); |
| 66 | 74 | expect(kindView("a.ipynb", '{"nope":1}', c)).toBeNull(); |
new above
offline
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.