Skip to content

Widget: a Download on each figure a run shows - #505

Merged
neuromechanist merged 5 commits into
developfrom
feature/issue-492-figure-download
Sep 25, 2026
Merged

neuromechanist merged 5 commits into
developfrom
feature/issue-492-figure-download

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Closes #492

Stacked on #503 (issue #491): its base is feature/issue-491-run-code, because it reuses that PR's saveFile download helper and runFileStem naming, and the run markup it adds figures' buttons to. The diff below is this issue's alone; once #503 merges I will rebase this onto develop and retarget it.

What

Each figure a run shows gets a Download button in a right-aligned row under it (never over the figure, where a legend often sits), which saves that figure's PNG, byte for byte, as <dataset>-run-<n>-figure-<k>.png (for example nm000132-run-3-figure-1.png).

  • The bytes come from the run record's base64, decoded (pngBytes), in an image/png Blob through saveFile. The click re-reads the record by data-msg-index/data-run-index/data-image-index and re-checks isShowableImage, so a stale button saves nothing rather than other bytes under a .png name.
  • <k> is the figure's number among the figures the run shows (a figure the widget will not show is skipped), as the workspace names a run's figure-K.png; <dataset>-run-<n> is Widget: show a run's code in place, with Copy and Download #491's run name.
  • It is a real <button type="button"> with visible text ("Download"), an aria-label naming the figure and the file (Download figure 1 as nm000132-run-3-figure-1.png), and a title. The figure's alternative text is numbered to match (Figure 1 produced by the code, was Figure produced by the code).
  • It is drawn on the panel in the panel's colors, like the code block's bar (muted text, the accent on hover, a dark-panel hover rule). The wrapper is the figure's own width (fit-content), so the row ends where the figure does, however narrow it is.
  • A figure under an open "Edit and run" editor downloads as the reader's run it came from (the record the run added), not the run whose editor it is under.
  • Only runs with figures have any of this; with no reply to act on (executionsHtml without msgIndex) the figures render exactly as before, with no wrapper or button.

Done when

  • A test for the download's bytes and filename: frontend/test-widget-tools.js, 22 new checks (407 in all): the markup for a run with an unshowable figure ahead of two PNGs (only ours, one button per shown figure, numbered 1 and 2, labels and alt text, real buttons with visible text); the bytes of each saved file equal its PNG's decoded base64, signature included, as image/png, named nm000132-run-2-figure-2.png and -figure-1.png; a stale button saves nothing; the echo's figure named nm000132-run-3-figure-1.png and saving its PNG; no button without a reply to act on or without figures.
  • docs/community-widget.md ("A run in the chat") and CHANGELOG updated.

Also checked by hand in headless Chrome 153 against widget_e2e.py (scripted "plot", real Pyodide): the figure's Download takes focus, a real Enter key press on it (Input.dispatchKeyEvent with text: '\r') started a download suggested as nm000132-run-1-figure-1.png, and the saved file equals the figure's base64, byte for byte (41,625 bytes). Not committed, since it needs the network for Pyodide.

Mutation checks

Each applied alone, the suite run, restored, and the restore confirmed by comparing git diff --stat before and after. All 10 caught:

Mutation Caught by
no Download on figures "one per figure shown"
label numbered by the record index "each labeled for a screen reader ..."
file numbered by the record index "each figure its own file"
saves the base64 text, not the bytes "the second figure's file is its PNG, byte for byte"
wrong Blob type "as PNG"
no PNG re-check on click "a figure that is no longer a PNG downloads nothing"
echo located at the editor's run "named for the reader's run, the reply's third"
alt text not numbered "numbered among the figures shown"
no aria-label "each labeled for a screen reader ..."
tabindex="-1" "real buttons, reachable with Tab"

Other suites

Widget suites: tools 407, capsule 133, color scheme 128, notebook tab 168, first paint 82, dataset questions 60, pop-out 132; the runtime bundle is current. No Python change.

@github-actions

github-actions Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Preview Deployment

Name Link
Preview URL https://feature-issue-492-figure-dow-demo.osc.earth
Branch feature/issue-492-figure-download
Commit 14fafd9

This preview will be updated automatically when you push new commits.

@neuromechanist
neuromechanist force-pushed the feature/issue-491-run-code branch from 5b0c894 to ef106ee Compare September 25, 2026 00:53
@neuromechanist
neuromechanist force-pushed the feature/issue-492-figure-download branch from 97dd342 to af98c70 Compare September 25, 2026 00:56
@neuromechanist

Copy link
Copy Markdown
Member Author

Review response

Rebased onto #503's updated branch (feature/issue-491-run-code at ef106ee, itself on current develop), so this PR still shows only #492's diff; head af98c70.

  1. The Download sat over the figure, where a legend often is. Fixed in c0e31fb and af98c70. The button now sits in a row under the figure, right-aligned (.osa-figure-actions, flex, justify-content: flex-end), inside a wrapper that is the figure's own width, so the row ends where the figure does. It keeps its visible "Download", its aria-label ("Download figure 1 as nm000132-run-3-figure-1.png") and the numbering. It now takes the panel's colors, like the code block's buttons: --osa-text-light, the accent on hover, with a dark-panel hover rule. The CSS comment and the docs no longer say it keeps light colors over a white figure. The docs sentence now reads "in a row under it, right-aligned, as the code's bar is, never over the figure, where a legend often sits". New test, against the real stylesheet after init: the button is in the flow, not absolutely positioned; its row is right-aligned and follows the image; it uses the panel's muted color. Mutations, each caught: putting it back over the figure, dropping the right alignment, putting the row before the image, and fixing its colors. In Chrome, the button is under the figure at its right edge, and a real Enter keypress still saves nm000132-run-1-figure-1.png byte for byte.

Skipped: the click-time recount could in theory diverge from the rendered label if a record changed without a re-render. Every change to a run's figures goes through renderMessages, which rebuilds the label and the button together.

Widget suites: tools 424, pop-out 140, capsule 133, color scheme 128, notebook tab 168, first paint 82, dataset questions 60.

Base automatically changed from feature/issue-491-run-code to develop September 25, 2026 00:58
Each figure gets a Download button over its corner that saves its
exact PNG, built from the run record's base64, as
<dataset>-run-<n>-figure-<k>.png, k counting the figures shown. It
is a real, labeled button; the figure's alt text is numbered to
match. The echo under an open editor names the reader's run.

Tested: bun frontend/test-widget-tools.js (407 passed, 22 new); ten
mutations each caught and restored; a real Enter press on it saved
the PNG byte for byte in headless Chrome.
Tested: docs only.
The button moves from over the figure's top-right corner, where
matplotlib often puts a legend, to a right-aligned row under it, in
the panel's colors like the code block's bar. Label, aria-label and
numbering are unchanged.

Tested: bun frontend/test-widget-tools.js (424 passed); placing it
back over the figure, dropping the right alignment, putting it before
the figure, or fixing its colors each fail it; checked in Chrome.
@neuromechanist
neuromechanist force-pushed the feature/issue-492-figure-download branch from af98c70 to e3c87fa Compare September 25, 2026 01:01
The attribute-escaping drift test flagged the three data-* indices. String() first, since happy-dom renders textContent = 0 as empty.

Tested: pytest 2923 passed (drift test green), every Bun suite (tools 424), ruff
@neuromechanist
neuromechanist merged commit 64ac2ab into develop Sep 25, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-492-figure-download branch September 25, 2026 01:14
neuromechanist added a commit that referenced this pull request Sep 25, 2026
Brings in #505 and #509; CHANGELOG keeps both Added entries.

Tested: build-runtime-bundle --check (current); test-widget-tools passed;
tests/test_frontend_job_parity.py passed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant