Skip to content

ariglx: matplotlib figures as Ariadne windows, and display() (C1) - #1198

Merged
transfix merged 5 commits into
masterfrom
feat/ox-matplotlib
Nov 13, 2026
Merged

transfix merged 5 commits into
masterfrom
feat/ox-matplotlib

Conversation

@transfix

Copy link
Copy Markdown
Owner

The first part of roadmap C1: matplotlib in the x hosts (docs/roadmap/ARIADNE_SCRIPTING_HOSTS.md §4.6; §10's C1 row and §4.6's "as built" note are updated). Native ariglx first.

What it adds

pycvc_gl.ariadne_mpl, a matplotlib backend (module://pycvc_gl.ariadne_mpl; pycvc_gl.ariadne is a module, so the roadmap's pycvc_gl.ariadne.mpl can't be imported):

  • The canvas is Agg's. Each draw publishes a copy of buffer_rgba() with Host.publish_image as the picture figure-<num>.
  • plt.show() opens each figure in a window. A program with no document of its own gets the shipped pycvc_gl/figures.ari (one window per figure, a Figures menu). A blocking plt.show() runs the host's loop until those windows are closed. Closing a window closes its figure and fires close_event, as a GUI window does.
  • A plt.show() during a load, from a function the document calls, inside a frame or from another thread doesn't block and doesn't load a document. In those cases the document's own image: shows the figure by name, or by the name bind(fig, name) gives it.
  • draw_idle() coalesces into one draw in the host's next frame. Timers and plt.pause() run on the frames, so FuncAnimation works. plt.ion() shows a figure as soon as it is made.
  • A blocking plt.show() blocks only where a window can be closed. On an offscreen or headless Host it returns at once, as Agg's does.

pycvc_gl.ariadne_display.display(obj) shows an object's richest form, as IPython's display() does:

  • matplotlib figures, numpy pictures and PIL images as pixels;
  • image/png and image/jpeg bundles, decoded with PIL;
  • markdown, LaTeX and plain text as text.

display_id and the handle's update() replace an output in place. With no GL window (arix, ariglx --terminal, arisrvx, a document of its own) it prints text/plain. display is predefined in a python: block, beside export.

Engine (ariadne_python.py, python_engine.cpp):

  • A one-shot meta_path finder makes the backend matplotlib's default on its first import, when a GL host has lent its window and nothing else chose a backend. It sets rcParams and never touches os.environ. arix, ariglx --terminal and <host> python keep matplotlib's own default.
  • No loop inside the loop. The app keeps, in app.slot<PythonActivity>, whether a python: block is loading, whether the host's loop is running, and which thread attached the engine. _arihost.run() refuses in four cases: while the loop runs, during a load, from another thread, and from inside a frame (HostHandles::in_frame, AriRuntime::in_frame()). Engine::run_loop() marks every after_load().
  • No new process-wide state. The loading counter lives on the app, and each figure carries its own Host.

ci.yml's pycvc-linux job now installs matplotlib-cp312, so the new tests run there instead of being skipped. The writable-data allowlist is unchanged.

How it was tested (Windows, shared Release build: libcvc, cvcGL, ari/arix, arigl/ariglx, pycvc, pycvc_gl; cvcpkg python312 + numpy + matplotlib in its own prefix)

  • ctest set (19 tests): 19 passed, 0 failed
  • pycvc_gl_ariadne_mpl: passed (90 checks, matplotlib 3.10.0)
  • ariglx_matplotlib_show / _close / _block / _nested: 4 passed
  • arix_matplotlib_backend: passed
  • ariglx_python_check, ariglx_python_program: 2 passed
  • arix_python_block, arix_python_program, arix_python_error, arix_python_interpreter: 4 passed
  • ari_python_needs_arix: passed (after building ari)
  • pycvc_ariadne, pycvc_ariadne_server, pycvc_gl_smoke, pycvc_ari, pycvc_gl_ariadne_host, pycvc_gl_ariadne_gil: 6 passed
  • Manual: ariglx run mpl_nested.ari in a real window: no crash, frames kept running, EXIT=0
  • Manual: mpl_block with the Python ownership check forced to true: the C++ load refusal held, frame_count 0, exit 0
  • The last commit only reformats _arihost.run's refusal chain with clang-format 18.1.8, which the changed-lines check needs. After it, arix and ariglx were rebuilt and the 12 arix/ariglx python and matplotlib tests plus pycvc_gl_ariadne_mpl passed again. git-clang-format --diff against the merge base reports no changes.

Left out

  • Nothing merged, tagged or published to cvcpkg.org.
  • arisrvx was not built or tested in this tree (CVC_BUILD_ARISRV=OFF). Its engine path changed slightly: attach now records the host thread on the app, and the server-mode guards were added. pycvc_ariadne_server passed under plain python.
  • A process-wide default Host for pyplot is removed, not kept, because adding one needs Joe's OK first. Restoring it (one call to attach before any figure exists) needs his decision. The roadmap's as-built note records it as pending.
  • Documents can't yet opt in to figure windows of their own. In a document of its own, display() prints, and figures show through the document's own image:.
  • Interaction (wheel, pan, zoom, keys, the cursor) is C2. The browser (W5) and VTK or mesh display() come later.

Merging waits on Joe's review.

#1159 (O7c) kept the last source sea_surface found in a thread_local, so
the velocity a fluid field reads after a cell's height at the same point
and time did not invert the sideways move again. A thread_local that
keeps state between calls is a static cache, which libcvc's no-singletons
rule forbids (VOLROVER-LIBRARY-PORTS-ROADMAP.md 1.3,
tools/singleton_scan). #1159's release-grpc job, where the scan runs, was
cancelled, so the scan never judged it; the next PR that merges master
would fail that lane on it.

The memo now belongs to the caller:

- sea_surface::source_memo, and height() / velocity() overloads that take
  one. The plain reads share nothing.
- fluid_surface::height_with / velocity_with and a surface_memo the
  caller owns; the defaults ignore it. ocean_surface passes it to the sea.
- field_set::apply keeps one memo per cell, for the cell's height and its
  velocity at the same clipped centre.

The memo is keyed on the point, the time and the digest as before, so
every value is unchanged.
The first part of roadmap C1 (ARIADNE_SCRIPTING_HOSTS.md §4.6), in
Python, .ari and CMake only, with no C++ change.

pycvc_gl.ariadne_mpl is a matplotlib backend,
module://pycvc_gl.ariadne_mpl (pycvc_gl.ariadne is a module, so the
roadmap's pycvc_gl.ariadne.mpl can't import):
- the canvas is Agg's, and each draw publishes a copy of buffer_rgba()
  with Host.publish_image as the picture figure-<num>;
- plt.show() opens each figure in a window. A program with no document
  of its own gets the shipped pycvc_gl/figures.ari (one window per
  figure, a Figures menu), and a blocking plt.show() runs the host's
  loop until those windows are closed. Closing a window closes its
  figure, with a close_event, as a GUI window does;
- plt.show() during a load, from a function the document calls, in a
  frame or off the main thread neither blocks nor loads a document.
  There the document's own image: shows the figure by name, or by the
  name bind(fig, name) gives it, with no window;
- draw_idle() coalesces into one draw in the host's next frame, and
  timers tick on the frames, so FuncAnimation runs;
- plt.ion() shows a figure as it is made;
- a plain python program with a Host of its own calls attach(host).

pycvc_gl.ariadne_display.display(obj) shows an object's richest form,
as IPython's display() does: matplotlib figures, numpy pictures and
PIL images as pixels, image/png and image/jpeg bundles decoded with
PIL, and markdown, LaTeX or plain text as text. display_id and the
handle's update() replace an output in place. With no host it prints
text/plain. It shares figures.ari's windows (Figures, one per Host,
kept on the Host) and imports matplotlib only for a figure.
pycvc_gl.ariadne.display forwards to it.

The engine (ariadne_python.py):
- a one-shot meta_path finder makes the backend matplotlib's default
  on its first import when a GL host has lent its window and nothing
  chose a backend (MPLBACKEND, a matplotlibrc). It sets rcParams, never
  os.environ. arix, ariglx --terminal and <host> python keep
  matplotlib's own default;
- load_document counts the python: blocks it runs (loading), so the
  backend can tell a load is under way;
- display is predefined in a python: block's source, beside export.

ci.yml's pycvc-linux installs matplotlib-cp312, so its new tests run
rather than skip there.

Interaction (wheel, pan, zoom, keys, the cursor) is C2. arix, the
browser (W5) and VTK or mesh display() come later.

Tests: pycvc_gl_ariadne_mpl (new: a headless Host; draws, windows,
user-closed windows, titles, bind, timers, FuncAnimation, blocking
show, calls from frames and threads, display); ariglx_matplotlib_show
and ariglx_matplotlib_block (new: the default backend, a blocking
plt.show() through figures.ari, a python: block's figure shown by the
document's own image:); pycvc_gl_ariadne_host, pycvc_gl_ariadne_gil,
pycvc_gl_smoke, pycvc_ari, ariglx_python_*, arix_python_* rerun.
The review of C1's first part, fixed.

No loop inside the loop. A plt.show() from a document's function that
has a top level of its own (exec(), runpy, a script-style import) got
past program_level(), and host.run() started a second after_load()
inside the frame: in run mode its locals went while the outer loop
still called them. Now:
- the engine keeps on the app (app.slot, PythonActivity) whether a
  python: block is loading and whether the host's loop runs, and the
  thread that attached it. _arihost.run() refuses while the loop runs,
  during a load, from another thread, or from inside a frame
  (HostHandles::in_frame, AriRuntime::in_frame()), before it marks the
  loop as run. Engine::run_loop() runs a host's own loops, the
  document's too, so ariglx and arix mark every after_load();
- ariglx's after_load takes its input pump and warning listener back
  when the loop ends, however it ends;
- program_owns(host) gates a blocking plt.show(), plt.pause() and
  figures.ari's load: the program's own code, on the host's thread,
  outside a frame and a load, and not a document the scripting host
  loaded. mainloop returns on any of _arihost.run's refusals.

No process-wide state. ariadne_python.py's loading counter is gone
(_arihost.loading() reads the app's), and so is ariadne_display's
module-wide attached Host: a figure carries its Host. attach(host)
gives it pyplot's open figures (and the ones pyplot makes while one of
them is current), show_in(host) attaches and shows, and display() takes
host=. A process-wide default host, which pyplot's own figures would
want, is left for a decision.

pycvc_gl.ariadne_mpl:
- a blocking plt.show() blocks only where a window can be closed: a
  plain Host in a window (until figures.ari shows nothing, so a later
  plt.show() shows again), or the scripting host's loop, which ends
  once (a later blocking one warns). On an offscreen or headless Host
  it returns at once, as Agg's does;
- plt.pause() runs the host's frames for its interval
  (start_event_loop), where the program runs them.

pycvc_gl.ariadne_display:
- a timer's tick, a deferred draw or a close_event handler that raises
  is printed (as matplotlib's GUI backends print one), and the frame
  goes on: the other canvases draw, and figures.ari's close check runs;
- display() to a closed host, or one its scripting host let go, prints;
- figures.ari's windows are ari_figure_<k>, so no document's own id is
  taken, and a document of the host's own gets none: display() prints
  its text/plain form there, and a figure shows through the document's
  image:. Only an embedded host's figures.ari closes itself;
- the scripting host is adopted only where it has lent a GL window, and
  pycvc_gl.ariadne never adopts arisrvx's (mode server).

The display() predefined in a python: block prints where no GL window
is lent (arix, ariglx --terminal, arisrvx) instead of reaching for a
host.

Tests: ariglx_matplotlib_nested (new: exec() and runpy plt.show() from
a document function in a frame return with no frame run, host.run() is
refused, and the frames go on; it also ends under ariglx run);
ariglx_matplotlib_close (new: the last window closing ends the
scripting host's loop well before the snapshot's 60 frames);
arix_matplotlib_backend (new: arix keeps matplotlib's own default);
ariglx_matplotlib_show (host.run() from a frame of the program's own
is refused and leaves the loop to run); ariglx_matplotlib_block (no
frame runs during the load; the block's own display() prints);
pycvc_gl_ariadne_mpl (per-figure hosts, two Hosts, failing timers and
draws, a headless show returns, a window show blocks twice, plt.pause,
display() with no window); pycvc_gl_ariadne_host, pycvc_gl_ariadne_gil,
pycvc_gl_smoke, pycvc_ari, pycvc_ariadne*, ariglx_python_*,
arix_python_*, ari_python_needs_arix rerun. The browser build compiles
the touched sources.
Whitespace only: the changed-lines check wanted the conditional chain laid out as clang-format 18 does.
@transfix
transfix enabled auto-merge (squash) November 13, 2026 03:54
@transfix
transfix merged commit 48de5c1 into master Nov 13, 2026
29 of 33 checks passed
@transfix
transfix deleted the feat/ox-matplotlib branch November 13, 2026 08:50
transfix added a commit that referenced this pull request Nov 13, 2026
transfix added a commit that referenced this pull request Nov 13, 2026
#1198 edited bindings/pycvc/pymod_gl/ariadne.py, which this branch moves
to bindings/pycvc/pymod/ari.py (pycvc.ari), leaving an alias behind. The
alias stays as it was, and #1198's two hunks move to ari.py:

- _arihost() never adopts arisrvx's host (mode "server").
- display(obj, **kwargs) imports pycvc_gl.ariadne_display by its full
  name, because a relative import does not resolve from pycvc.ari.
  pycvc_gl.ariadne.display is the same function, through the alias.
transfix added a commit that referenced this pull request Nov 13, 2026
transfix added a commit that referenced this pull request Nov 13, 2026
The one conflict was arigl_web_smoke.mjs. Both sides are kept:
- the /apps/up endpoint, with #1200's ISOLATION headers on its replies as on every response;
- the settle probe reads out and isolated/sab;
- each case runs its before/inject and #1200's isolation check.
The headless-Chrome smoke passes 6/6 here, the picked-file upload included.
Master since then: #1179, #1174, #1196, #1198, #1178, #1157, #1172, #1200.
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