umbruch-matplotlib 0.1.0__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: umbruch-matplotlib
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Matplotlib backend that renders PDF, text and math through umbruch
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://github.com/manuel-rhdt/umbruch-matplotlib
@@ -51,6 +51,19 @@ Agg. The page background is the figure's facecolor, so a transparent figure come
51
51
  channel. One page per file — a figure is one page — and a `dpi` that needs more than 16.7 million
52
52
  pixels in total (the raster backend's limit) is an error.
53
53
 
54
+ ### In Jupyter
55
+
56
+ There is no interactive canvas, so `plt.show()` cannot open a window: it hands the open figures to
57
+ IPython, which prints them through this backend's own `print_png`, so a notebook shows the engine's
58
+ raster of the figure rather than an Agg preview. Run `%matplotlib inline` once: IPython then
59
+ registers its figure formatter, a figure as the last expression of a cell is displayed, and a cell
60
+ that only draws (`plt.plot(...)`) flushes the figure at its end. Without any `%matplotlib` there is
61
+ nothing that displays figures at all — that is IPython's rule, not the backend's.
62
+
63
+ To keep exploring with a fast backend and render only the final file with umbruch, leave the
64
+ notebook's backend alone and save with `fig.savefig("fig.pdf", backend="module://umbruch_matplotlib")`;
65
+ matplotlib hands the figure its Agg canvas back afterwards.
66
+
54
67
  The backend builds on parts of Matplotlib's SVG renderer that are not public API. It is tested with Matplotlib 3.9, 3.10 and 3.11 (CI runs the oldest and the newest), and on import it checks that the parts it needs are there, naming what is missing otherwise.
55
68
 
56
69
  Unsupported features raise instead of falling back to Matplotlib's own text rendering:
@@ -26,6 +26,19 @@ Agg. The page background is the figure's facecolor, so a transparent figure come
26
26
  channel. One page per file — a figure is one page — and a `dpi` that needs more than 16.7 million
27
27
  pixels in total (the raster backend's limit) is an error.
28
28
 
29
+ ### In Jupyter
30
+
31
+ There is no interactive canvas, so `plt.show()` cannot open a window: it hands the open figures to
32
+ IPython, which prints them through this backend's own `print_png`, so a notebook shows the engine's
33
+ raster of the figure rather than an Agg preview. Run `%matplotlib inline` once: IPython then
34
+ registers its figure formatter, a figure as the last expression of a cell is displayed, and a cell
35
+ that only draws (`plt.plot(...)`) flushes the figure at its end. Without any `%matplotlib` there is
36
+ nothing that displays figures at all — that is IPython's rule, not the backend's.
37
+
38
+ To keep exploring with a fast backend and render only the final file with umbruch, leave the
39
+ notebook's backend alone and save with `fig.savefig("fig.pdf", backend="module://umbruch_matplotlib")`;
40
+ matplotlib hands the figure its Agg canvas back afterwards.
41
+
29
42
  The backend builds on parts of Matplotlib's SVG renderer that are not public API. It is tested with Matplotlib 3.9, 3.10 and 3.11 (CI runs the oldest and the newest), and on import it checks that the parts it needs are there, naming what is missing otherwise.
30
43
 
31
44
  Unsupported features raise instead of falling back to Matplotlib's own text rendering:
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "umbruch-matplotlib"
7
- version = "0.1.0"
7
+ version = "0.2.0"
8
8
  description = "Matplotlib backend that renders PDF, text and math through umbruch"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -754,3 +754,34 @@ def test_rich_text_is_set_in_the_bold_and_italic_faces(real_bridge, tmp_path):
754
754
  fonts = subprocess.run(["pdffonts", str(path)], capture_output=True, text=True).stdout
755
755
  assert "DejaVuSans-Bold" in fonts, fonts
756
756
  assert "DejaVuSans-Oblique" in fonts, fonts
757
+
758
+
759
+ def test_show_hands_the_figures_to_ipython(monkeypatch):
760
+ """`plt.show()` cannot open a window here, so in a notebook it must hand the figure to
761
+ IPython, whose own figure printer renders it through this canvas' print_png."""
762
+ import types
763
+ shown = []
764
+ fake_display = types.ModuleType("IPython.display")
765
+ fake_display.display = lambda figure, **kwargs: shown.append(figure)
766
+ fake_ipython = types.ModuleType("IPython")
767
+ fake_ipython.get_ipython = lambda: None # (matplotlib asks for the shell)
768
+ monkeypatch.setitem(sys.modules, "IPython", fake_ipython)
769
+ monkeypatch.setitem(sys.modules, "IPython.display", fake_display)
770
+ fig, ax = plt.subplots()
771
+ ax.plot([0, 1])
772
+ plt.show()
773
+ assert [f for f in shown] == [fig], shown
774
+
775
+
776
+ def test_show_without_ipython_is_left_to_matplotlib(monkeypatch):
777
+ """Outside IPython there is nothing to display into, so the call goes to the non-interactive
778
+ backend — which warns where a window could have existed and is silent when headless."""
779
+ monkeypatch.setitem(sys.modules, "IPython", None) # (not importable)
780
+ monkeypatch.setitem(sys.modules, "IPython.display", None)
781
+ calls = []
782
+ monkeypatch.setattr(hb.FigureManagerBase, "pyplot_show",
783
+ classmethod(lambda cls, **kwargs: calls.append(kwargs)))
784
+ fig, ax = plt.subplots()
785
+ ax.plot([0, 1])
786
+ plt.show()
787
+ assert calls == [{"block": None}], calls
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: umbruch-matplotlib
3
- Version: 0.1.0
3
+ Version: 0.2.0
4
4
  Summary: Matplotlib backend that renders PDF, text and math through umbruch
5
5
  License-Expression: MIT
6
6
  Project-URL: Homepage, https://github.com/manuel-rhdt/umbruch-matplotlib
@@ -51,6 +51,19 @@ Agg. The page background is the figure's facecolor, so a transparent figure come
51
51
  channel. One page per file — a figure is one page — and a `dpi` that needs more than 16.7 million
52
52
  pixels in total (the raster backend's limit) is an error.
53
53
 
54
+ ### In Jupyter
55
+
56
+ There is no interactive canvas, so `plt.show()` cannot open a window: it hands the open figures to
57
+ IPython, which prints them through this backend's own `print_png`, so a notebook shows the engine's
58
+ raster of the figure rather than an Agg preview. Run `%matplotlib inline` once: IPython then
59
+ registers its figure formatter, a figure as the last expression of a cell is displayed, and a cell
60
+ that only draws (`plt.plot(...)`) flushes the figure at its end. Without any `%matplotlib` there is
61
+ nothing that displays figures at all — that is IPython's rule, not the backend's.
62
+
63
+ To keep exploring with a fast backend and render only the final file with umbruch, leave the
64
+ notebook's backend alone and save with `fig.savefig("fig.pdf", backend="module://umbruch_matplotlib")`;
65
+ matplotlib hands the figure its Agg canvas back afterwards.
66
+
54
67
  The backend builds on parts of Matplotlib's SVG renderer that are not public API. It is tested with Matplotlib 3.9, 3.10 and 3.11 (CI runs the oldest and the newest), and on import it checks that the parts it needs are there, naming what is missing otherwise.
55
68
 
56
69
  Unsupported features raise instead of falling back to Matplotlib's own text rendering:
@@ -1083,3 +1083,23 @@ class FigureCanvasUmbruch(FigureCanvasSVG):
1083
1083
 
1084
1084
 
1085
1085
  FigureCanvas = FigureCanvasUmbruch
1086
+
1087
+
1088
+ def show(*, block=None):
1089
+ """Shows the open figures where a frontend can take them — in IPython and Jupyter as the PNG
1090
+ that umbruch renders of them, since this backend has no interactive canvas — and otherwise
1091
+ leaves it to ``FigureManagerBase.pyplot_show``, which warns that the backend is
1092
+ non-interactive. IPython's own figure printer calls ``print_figure`` on whatever canvas a
1093
+ figure has, so displaying the figure object goes through umbruch already."""
1094
+ from matplotlib._pylab_helpers import Gcf
1095
+ figures = [manager.canvas.figure for manager in Gcf.get_all_fig_managers()]
1096
+ if figures:
1097
+ try:
1098
+ from IPython.display import display
1099
+ except ImportError:
1100
+ display = None
1101
+ if display is not None:
1102
+ for figure in figures:
1103
+ display(figure)
1104
+ return
1105
+ FigureManagerBase.pyplot_show(block=block)