diff --git a/pyproject.toml b/pyproject.toml index 0e286a6..78d2e24 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "maxplotlibx" -version = "0.1.5" +version = "0.1.6" description = "A reproducible plotting module with various backends and export options." readme = "README.md" requires-python = ">=3.8" diff --git a/pytest.ini b/pytest.ini index e407553..b4fff9a 100644 --- a/pytest.ini +++ b/pytest.ini @@ -1,3 +1,4 @@ # pytest.ini [pytest] -addopts = -ra -q \ No newline at end of file +addopts = -ra -q +pythonpath = src \ No newline at end of file diff --git a/src/maxplotlib/canvas/canvas.py b/src/maxplotlib/canvas/canvas.py index e224614..cc7918c 100644 --- a/src/maxplotlib/canvas/canvas.py +++ b/src/maxplotlib/canvas/canvas.py @@ -946,7 +946,24 @@ def show( layers: list | None = None, usetex: bool | None = None, verbose: bool = False, + block: bool = True, ): + """ + Render and display the canvas. + + Parameters + ---------- + block : bool, optional + matplotlib backend only (default: True). Whether to block until + the figure window is closed before returning. Passed straight to + ``plt.show(block=...)`` rather than left at its default, because + that default follows ``matplotlib.is_interactive()`` -- which + other imported code (IPython, a prior interactive session) can + flip to True behind this call's back, silently turning off + blocking. Forcing it explicitly is what makes repeated + ``canvas.show()`` calls in a loop display one figure at a time + instead of every window appearing together. + """ if verbose: print(f"Showing canvas using backend: {backend}") @@ -962,7 +979,7 @@ def show( ) if verbose: print("Displaying Matplotlib figure...") - plt.show() + plt.show(block=block) return fig, axes elif backend == "plotly": resolved_usetex = self._usetex if usetex is None else usetex diff --git a/src/maxplotlib/tests/test_canvas.py b/src/maxplotlib/tests/test_canvas.py index b72dbb7..7bd8f9a 100644 --- a/src/maxplotlib/tests/test_canvas.py +++ b/src/maxplotlib/tests/test_canvas.py @@ -347,11 +347,34 @@ def test_canvas_show_uses_matplotlib_show(monkeypatch): fig, axes = canvas.show() - assert calls == [((), {})] + # block=True is passed explicitly (not left at plt.show()'s own default) + # so a caller looping over several canvases shows them one at a time + # regardless of matplotlib's interactive-mode state. + assert calls == [((), {"block": True})] assert fig is not None assert axes is not None +def test_canvas_show_block_false_is_forwarded(monkeypatch): + import matplotlib.pyplot as plt + + from maxplotlib import Canvas + + calls = [] + + monkeypatch.setattr( + plt, "show", lambda *args, **kwargs: calls.append((args, kwargs)) + ) + + canvas = Canvas() + subplot = canvas.add_subplot() + subplot.plot([0, 1], [0, 1]) + + canvas.show(block=False) + + assert calls == [((), {"block": False})] + + def test_show_canvas_script_invokes_canvas_show(monkeypatch): import maxplotlib