Skip to content

refactor(tui): one render path for text and image plots - #328

Merged
henryiii merged 1 commit into
mainfrom
refactor/one-render-path
Nov 21, 2026
Merged

henryiii merged 1 commit into
mainfrom
refactor/one-render-path

Conversation

@henryiii

@henryiii henryiii commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

🤖 AI text below 🤖

Items 19, 9, part of 12, and App._exit from 23 of the review issue.

  • Plotext and MPLPlot subclass one frozen PlotItem dataclass with the shared fields and with_* methods. The caches have compare=False and copies share them. The item compares by value, so "already rendered" is item == self._rendered and the key tuples are gone. The size goes into a copy of the item (dataclasses.replace), which the handler puts in view_widget.item without the watcher.
  • One RequestRender message, one on_request_render handler, one threaded exclusive render_item worker. The worker catches the errors and posts RenderFailed(source, error, render_id). on_render_failed branches on the error type and keeps the stale-report drop and the failed-expression retry guard. The item does not hold app now, and run_posting_errors, EmptyMessage, ErrorMessage, ExpressionErrorMessage, RequestPlot and RequestImage are removed.
  • _render_failed uses the public return_code is not None instead of _exit. exit() sets both.
  • Item 9: render_canvas and MPLPlot.render build the histogram outside _PLOTEXT_LOCK and RENDER_LOCK. Only the draw holds a lock. The worker installs progress.cancel_when(...), so a progress report in a cancelled worker raises progress.CancelledError (a BaseException, like asyncio.CancelledError) and the chunked read stops. The worker ignores this exception. New tests check both modes with a slow fake read.
  • The image path does not put warnings.catch_warnings around the histogram build now. The filters are global for the process, so the build can only change them while it holds the lock. The text path did not suppress these warnings before this change, and the tests run with filterwarnings = error.
  • ViewWidget handles the Plot button and the entry box with @on (Button.Pressed, Input.Submitted, Input.Changed). PlotButton is removed, and PlotInput keeps only its binding.
  • A render failure report and the failed-expression guard compare to the current item by value. Before, a second selection of the same plot made an equal item that was not rendered again, and the report of the first item was dropped, so no error showed. A new test holds the worker to make this sequence certain.
  • Each started render gets a render ID, and on_render_failed drops a report with an old ID. Thus after A → B → A, the error of the cancelled first A render does not replace the new plot. An equal selection uses the running render and keeps its ID, so A → A still shows the error.
  • The text plot pane (PlotCanvas) posts Resized, and ViewWidget renders again (debounced). Before, the plot did not render in the smaller pane when the request after the entry box toggle came before the layout (a CI failure of test_entry_box_rerenders_plot).
  • The dead dev block at the end of browser.py and the __package__ workaround are removed.

The tests that kept a reference to an item and then read its size now read the item from the view again.

Refs #322

@henryiii henryiii left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI text below 🤖

A cancelled render can replace a newer plot with an error. I blocked the first render of A, selected B and then A, waited for the second A to display, and released the first render with a RuntimeError. The view switched from the successful plot to the first render’s error.

_render_failed compares plot items by value, so both renders of A pass the same check. Give each newly started render a request ID, include it in RenderFailed, and check it when the UI handles the message. Keep the ID when an equal selection reuses an in-flight render. That preserves the consecutive A → A case while rejecting the stale error after A → B → A. Please add the blocked-worker sequence as a regression test.

@henryiii
henryiii force-pushed the refactor/one-render-path branch from 6a8bf02 to 4ee07fe Compare November 21, 2026 00:59
@henryiii

Copy link
Copy Markdown
Member Author

🤖 AI text below 🤖

Fixed in 4ee07fe. Each started render gets a render ID, and RenderFailed carries it. on_render_failed drops a report with an old ID, so after A → B → A the error of the cancelled first render does not replace the plot. An equal selection uses the running render and keeps its ID, so A → A still shows the error. The new test test_cancelled_render_error_is_ignored_after_reselect holds the first worker for this sequence. It failed before the fix. The branch is also rebased on main.

@henryiii
henryiii force-pushed the refactor/one-render-path branch from 4ee07fe to f77ed07 Compare November 21, 2026 01:19
@henryiii
henryiii marked this pull request as ready for review November 21, 2026 01:23
@henryiii
henryiii force-pushed the refactor/one-render-path branch 2 times, most recently from b0b070f to e4a4d40 Compare November 21, 2026 01:41
Plotext and MPLPlot now subclass one frozen PlotItem dataclass. The item
compares by value, so the app compares it with the last rendered item
instead of a key tuple. One RequestRender message, one handler and one
worker render both modes. The worker catches the errors and posts one
RenderFailed message, so the item does not hold the app. Each started
render gets an ID, so the report of a cancelled render is dropped, also
after A -> B -> A. An equal selection keeps the ID of the running render,
so its error still shows.

A superseded render does not block the new one now: the data is read
outside the plotext and matplotlib locks, and a progress report in a
cancelled worker raises progress.CancelledError, which stops a chunked
read.

ViewWidget handles the entry box and the Plot button with @on. The dead
dev block and the __package__ workaround in browser.py are removed.

Refs #322

Assisted-by: ClaudeCode:claude-opus-5-5
@henryiii
henryiii force-pushed the refactor/one-render-path branch from e4a4d40 to e541a6c Compare November 21, 2026 01:48
@henryiii
henryiii merged commit 646d0ba into main Nov 21, 2026
12 checks passed
@henryiii
henryiii deleted the refactor/one-render-path branch November 21, 2026 01:52
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