briefing 0.2.3__tar.gz → 0.2.4__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.
Files changed (107) hide show
  1. {briefing-0.2.3 → briefing-0.2.4}/CHANGELOG.md +16 -1
  2. {briefing-0.2.3 → briefing-0.2.4}/PKG-INFO +1 -1
  3. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/__init__.py +1 -1
  4. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/layout.py +31 -13
  5. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/html.py +1 -1
  6. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/static/export.js +9 -9
  7. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_export_image.py +2 -2
  8. briefing-0.2.4/tests/test_browser/test_export_menu.py +93 -0
  9. {briefing-0.2.3 → briefing-0.2.4}/tests/test_contract.py +1 -1
  10. {briefing-0.2.3 → briefing-0.2.4}/tests/test_deprecations.py +26 -0
  11. briefing-0.2.4/tests/test_renderers/test_export_menu.py +74 -0
  12. briefing-0.2.3/tests/test_browser/test_export_menu.py +0 -55
  13. briefing-0.2.3/tests/test_renderers/test_export_menu.py +0 -46
  14. {briefing-0.2.3 → briefing-0.2.4}/.gitignore +0 -0
  15. {briefing-0.2.3 → briefing-0.2.4}/LICENSE +0 -0
  16. {briefing-0.2.3 → briefing-0.2.4}/README.md +0 -0
  17. {briefing-0.2.3 → briefing-0.2.4}/noxfile.py +0 -0
  18. {briefing-0.2.3 → briefing-0.2.4}/pyproject.toml +0 -0
  19. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/__main__.py +0 -0
  20. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/_cli.py +0 -0
  21. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/_error.py +0 -0
  22. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/_frames.py +0 -0
  23. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/_preview.py +0 -0
  24. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/__init__.py +0 -0
  25. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/asset.py +0 -0
  26. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/banner.py +0 -0
  27. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/base.py +0 -0
  28. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/blocks/text.py +0 -0
  29. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/export/__init__.py +0 -0
  30. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/export/_image.py +0 -0
  31. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/export/_pdf.py +0 -0
  32. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/export/_render.py +0 -0
  33. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/__init__.py +0 -0
  34. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/_blocks.py +0 -0
  35. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/_render_calendar.py +0 -0
  36. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/_render_datadive.py +0 -0
  37. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/_render_profile.py +0 -0
  38. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/static/calendar.js +0 -0
  39. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/lab/static/lab.js +0 -0
  40. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/py.typed +0 -0
  41. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/__init__.py +0 -0
  42. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/formatting.py +0 -0
  43. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/formula.py +0 -0
  44. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/normalize.py +0 -0
  45. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/plot.py +0 -0
  46. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/registry.py +0 -0
  47. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/script_json.py +0 -0
  48. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/renderers/table.py +0 -0
  49. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/static/export.css +0 -0
  50. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/static/report.css +0 -0
  51. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/static/report.js +0 -0
  52. {briefing-0.2.3 → briefing-0.2.4}/src/briefing/templates/report.html.j2 +0 -0
  53. {briefing-0.2.3 → briefing-0.2.4}/tests/__init__.py +0 -0
  54. {briefing-0.2.3 → briefing-0.2.4}/tests/banner_images.py +0 -0
  55. {briefing-0.2.3 → briefing-0.2.4}/tests/compat_reports.py +0 -0
  56. {briefing-0.2.3 → briefing-0.2.4}/tests/conftest.py +0 -0
  57. {briefing-0.2.3 → briefing-0.2.4}/tests/contract.py +0 -0
  58. {briefing-0.2.3 → briefing-0.2.4}/tests/html_helpers.py +0 -0
  59. {briefing-0.2.3 → briefing-0.2.4}/tests/image_helpers.py +0 -0
  60. {briefing-0.2.3 → briefing-0.2.4}/tests/pdf_reader.py +0 -0
  61. {briefing-0.2.3 → briefing-0.2.4}/tests/snapshots/compat_0_1_2/blocks.body.html +0 -0
  62. {briefing-0.2.3 → briefing-0.2.4}/tests/snapshots/compat_0_1_2/pages.body.html +0 -0
  63. {briefing-0.2.3 → briefing-0.2.4}/tests/test_blocks/__init__.py +0 -0
  64. {briefing-0.2.3 → briefing-0.2.4}/tests/test_blocks/test_banner.py +0 -0
  65. {briefing-0.2.3 → briefing-0.2.4}/tests/test_blocks/test_base.py +0 -0
  66. {briefing-0.2.3 → briefing-0.2.4}/tests/test_blocks/test_layout.py +0 -0
  67. {briefing-0.2.3 → briefing-0.2.4}/tests/test_blocks/test_text.py +0 -0
  68. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/__init__.py +0 -0
  69. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/conftest.py +0 -0
  70. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_banner.py +0 -0
  71. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_bignumber.py +0 -0
  72. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_calendar.py +0 -0
  73. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_cli_export.py +0 -0
  74. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_code.py +0 -0
  75. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_datadive.py +0 -0
  76. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_datatable.py +0 -0
  77. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_export_pdf.py +0 -0
  78. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_formula.py +0 -0
  79. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_layout.py +0 -0
  80. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_notebook.py +0 -0
  81. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_plots.py +0 -0
  82. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_print.py +0 -0
  83. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_spacing.py +0 -0
  84. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_text.py +0 -0
  85. {briefing-0.2.3 → briefing-0.2.4}/tests/test_browser/test_toggle_theme.py +0 -0
  86. {briefing-0.2.3 → briefing-0.2.4}/tests/test_cli.py +0 -0
  87. {briefing-0.2.3 → briefing-0.2.4}/tests/test_compat.py +0 -0
  88. {briefing-0.2.3 → briefing-0.2.4}/tests/test_docs_gallery.py +0 -0
  89. {briefing-0.2.3 → briefing-0.2.4}/tests/test_docstrings.py +0 -0
  90. {briefing-0.2.3 → briefing-0.2.4}/tests/test_export.py +0 -0
  91. {briefing-0.2.3 → briefing-0.2.4}/tests/test_frames.py +0 -0
  92. {briefing-0.2.3 → briefing-0.2.4}/tests/test_lab/__init__.py +0 -0
  93. {briefing-0.2.3 → briefing-0.2.4}/tests/test_lab/test_calendar.py +0 -0
  94. {briefing-0.2.3 → briefing-0.2.4}/tests/test_lab/test_datadive.py +0 -0
  95. {briefing-0.2.3 → briefing-0.2.4}/tests/test_lab/test_lab_boundary.py +0 -0
  96. {briefing-0.2.3 → briefing-0.2.4}/tests/test_lab/test_profile.py +0 -0
  97. {briefing-0.2.3 → briefing-0.2.4}/tests/test_no_viz_deps.py +0 -0
  98. {briefing-0.2.3 → briefing-0.2.4}/tests/test_pdf_reader.py +0 -0
  99. {briefing-0.2.3 → briefing-0.2.4}/tests/test_preview.py +0 -0
  100. {briefing-0.2.3 → briefing-0.2.4}/tests/test_readme.py +0 -0
  101. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/__init__.py +0 -0
  102. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_formula.py +0 -0
  103. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_html.py +0 -0
  104. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_normalize.py +0 -0
  105. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_plot.py +0 -0
  106. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_reproducible.py +0 -0
  107. {briefing-0.2.3 → briefing-0.2.4}/tests/test_renderers/test_table.py +0 -0
@@ -10,6 +10,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
 
11
11
  ## [Unreleased]
12
12
 
13
+ ## [0.2.4] - 2026-11-17
14
+
15
+ ### Fixed
16
+
17
+ - The export menu's "Save as PNG" keeps a 32 px margin of page background around the report,
18
+ so content no longer touches the edge of the image (#119).
19
+
20
+ ### Deprecated
21
+
22
+ - `bf.Briefing(..., export="pdf")` no longer adds a "Print" button: it only opened the
23
+ browser's print dialog, which Ctrl+P / ⌘P opens with the same print layout. It still runs,
24
+ with a `DeprecationWarning`; from 0.4.0 it raises an error. Use `export="png"` for the
25
+ "Save as PNG" button, and `bf.save_pdf` for a PDF file (#119).
26
+
13
27
  ## [0.2.3] - 2026-11-07
14
28
 
15
29
  ### Fixed
@@ -251,7 +265,8 @@ First public release.
251
265
  inherits its base's renderer.
252
266
  - Ships inline type hints (`py.typed`). Python 3.11+.
253
267
 
254
- [Unreleased]: https://github.com/jeanmidevacc/briefing/compare/v0.2.3...HEAD
268
+ [Unreleased]: https://github.com/jeanmidevacc/briefing/compare/v0.2.4...HEAD
269
+ [0.2.4]: https://github.com/jeanmidevacc/briefing/compare/v0.2.3...v0.2.4
255
270
  [0.2.3]: https://github.com/jeanmidevacc/briefing/compare/v0.2.2...v0.2.3
256
271
  [0.2.2]: https://github.com/jeanmidevacc/briefing/compare/v0.2.1...v0.2.2
257
272
  [0.2.1]: https://github.com/jeanmidevacc/briefing/compare/v0.2.0...v0.2.1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: briefing
3
- Version: 0.2.3
3
+ Version: 0.2.4
4
4
  Summary: Build beautiful self-contained HTML reports from Python analysis
5
5
  Project-URL: Homepage, https://github.com/jeanmidevacc/briefing
6
6
  Project-URL: Documentation, https://briefing.readthedocs.io
@@ -134,7 +134,7 @@ def stringify(
134
134
  return render_report(wrapped, name=name, formatting=formatting, now=now)
135
135
 
136
136
 
137
- __version__ = "0.2.3"
137
+ __version__ = "0.2.4"
138
138
 
139
139
  __all__: list[str] = [
140
140
  # error
@@ -204,6 +204,14 @@ class Page(ContainerBlock):
204
204
  self.title = title
205
205
 
206
206
 
207
+ EXPORT_PDF_DEPRECATION = (
208
+ "Briefing(export='pdf') is deprecated and no longer adds a 'Print' button: it only opened the browser's "
209
+ "print dialog, which Ctrl+P (Cmd+P on a Mac) opens with the same print layout. For a PDF file, use "
210
+ "bf.save_pdf(report, 'report.pdf'). From 0.4.0, export='pdf' raises an error; pass export='png' for the "
211
+ "'Save as PNG' button, or leave export out."
212
+ )
213
+
214
+
207
215
  class Briefing(ContainerBlock):
208
216
  """Root document container.
209
217
 
@@ -213,19 +221,22 @@ class Briefing(ContainerBlock):
213
221
  *arg_blocks: The report's blocks, in order.
214
222
  blocks: The blocks as a list, instead of positional arguments.
215
223
  export: Opt-in export menu in the report header: ``"png"`` adds "Save as
216
- PNG" (the open page, drawn in the reader's browser at 2×) and ``"pdf"``
217
- adds "Print" (the print dialog, which can save a PDF). Reports without
218
- it contain no export code. "Save as PNG" is best effort: charts
219
- drawn in iframes (Bokeh) and WebGL Plotly traces come out blank, and
220
- fonts are whatever the reader's machine has. For exact, reproducible
221
- files use :func:`~briefing.save_image` / :func:`~briefing.save_pdf`.
224
+ PNG" (the open page with a margin around it, drawn in the reader's
225
+ browser at 2×). Reports without it contain no export code. "Save as
226
+ PNG" is best effort: charts drawn in iframes (Bokeh) and WebGL Plotly
227
+ traces come out blank, and fonts are whatever the reader's machine
228
+ has. For exact, reproducible files use :func:`~briefing.save_image` /
229
+ :func:`~briefing.save_pdf`. To print, use the browser's own print
230
+ (Ctrl+P or ⌘P): it gets the same print layout. *Deprecated:*
231
+ ``"pdf"`` (the "Print" button) raises a ``DeprecationWarning`` and
232
+ adds nothing; it will be rejected from 0.4.0.
222
233
 
223
234
  Example::
224
235
 
225
236
  report = bf.Briefing(
226
237
  bf.Text("# My report"),
227
238
  bf.Plot(fig),
228
- export=("png", "pdf"),
239
+ export="png",
229
240
  )
230
241
  bf.save(report, "report.html")
231
242
  """
@@ -233,7 +244,9 @@ class Briefing(ContainerBlock):
233
244
  min_blocks: t.ClassVar[int] = 1
234
245
 
235
246
  #: The actions export= accepts, in menu order.
236
- EXPORTS: t.ClassVar[tuple[str, ...]] = ("png", "pdf")
247
+ EXPORTS: t.ClassVar[tuple[str, ...]] = ("png",)
248
+ #: Accepted with a DeprecationWarning, and dropped: they add no button.
249
+ DEPRECATED_EXPORTS: t.ClassVar[dict[str, str]] = {"pdf": EXPORT_PDF_DEPRECATION}
237
250
  export: tuple[str, ...]
238
251
 
239
252
  def __init__(
@@ -252,11 +265,16 @@ class Briefing(ContainerBlock):
252
265
  @classmethod
253
266
  def _check_export(cls, export: object) -> tuple[str, ...]:
254
267
  actions = (export,) if isinstance(export, str) else export
255
- if not isinstance(actions, Sequence) or any(a not in cls.EXPORTS for a in actions):
256
- raise BriefingError(
257
- f"export accepts 'png' and/or 'pdf', for example export=('png', 'pdf'); got {export!r}."
258
- )
259
- return tuple(dict.fromkeys(t.cast("Sequence[str]", actions)))
268
+ if not isinstance(actions, Sequence) or any(
269
+ a not in cls.EXPORTS and a not in cls.DEPRECATED_EXPORTS for a in actions
270
+ ):
271
+ raise BriefingError(f"export accepts 'png', for example export='png'; got {export!r}.")
272
+ unique = tuple(dict.fromkeys(t.cast("Sequence[str]", actions)))
273
+ for action in unique:
274
+ if action in cls.DEPRECATED_EXPORTS:
275
+ # stacklevel 3: _check_export <- __init__ <- the caller's bf.Briefing(...)
276
+ warnings.warn(cls.DEPRECATED_EXPORTS[action], DeprecationWarning, stacklevel=3)
277
+ return tuple(a for a in unique if a in cls.EXPORTS)
260
278
 
261
279
  @classmethod
262
280
  def wrap(
@@ -57,7 +57,7 @@ _CSS = _load_static("report.css")
57
57
  _JS = _load_static("report.js")
58
58
  _EXPORT_JS = _load_static("export.js")
59
59
  _EXPORT_CSS = _load_static("export.css")
60
- _EXPORT_LABELS = {"png": "Save as PNG", "pdf": "Print"}
60
+ _EXPORT_LABELS = {"png": "Save as PNG"}
61
61
 
62
62
  _jinja_env = Environment(
63
63
  loader=PackageLoader("briefing", "templates"),
@@ -39,6 +39,10 @@
39
39
  }
40
40
 
41
41
  // Draw the report as it is on screen (the open page) into a canvas at 2x and download it.
42
+ // The image keeps this much of the page background around the report on every side
43
+ // (CSS px, the page's own top padding), so content never touches the image's edge.
44
+ var MARGIN = 32;
45
+
42
46
  function savePng() {
43
47
  var report = document.querySelector('.bf-report');
44
48
  var rect = report.getBoundingClientRect();
@@ -52,13 +56,13 @@
52
56
  var image = new Image();
53
57
  image.onload = function () {
54
58
  var canvas = document.createElement('canvas');
55
- canvas.width = width * scale;
56
- canvas.height = height * scale;
59
+ canvas.width = (width + 2 * MARGIN) * scale;
60
+ canvas.height = (height + 2 * MARGIN) * scale;
57
61
  var ctx = canvas.getContext('2d');
58
62
  ctx.fillStyle = background;
59
63
  ctx.fillRect(0, 0, canvas.width, canvas.height);
60
64
  ctx.scale(scale, scale);
61
- ctx.drawImage(image, 0, 0);
65
+ ctx.drawImage(image, MARGIN, MARGIN);
62
66
  canvas.toBlob(function (blob) {
63
67
  var link = document.createElement('a');
64
68
  link.href = URL.createObjectURL(blob);
@@ -72,10 +76,6 @@
72
76
  image.src = 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg);
73
77
  }
74
78
 
75
- menu.querySelectorAll('.bf-export__btn').forEach(function (button) {
76
- button.addEventListener('click', function () {
77
- if (button.getAttribute('data-export') === 'png') savePng();
78
- else window.print();
79
- });
80
- });
79
+ var png = menu.querySelector('.bf-export__btn[data-export="png"]');
80
+ if (png) png.addEventListener('click', savePng);
81
81
  })();
@@ -144,7 +144,7 @@ def _with_menu(export: tuple[str, ...]) -> bf.Briefing:
144
144
 
145
145
  def test_export_menu_is_hidden_while_exporting() -> None:
146
146
  def check() -> None:
147
- with open_report(_with_menu(("png", "pdf")), width=800, scale=1, name="Q1") as session:
147
+ with open_report(_with_menu(("png",)), width=800, scale=1, name="Q1") as session:
148
148
  assert session.page.locator(".bf-export").count() == 1
149
149
  assert not session.page.locator(".bf-export").is_visible()
150
150
 
@@ -153,6 +153,6 @@ def test_export_menu_is_hidden_while_exporting() -> None:
153
153
 
154
154
  def test_images_leave_the_export_menu_out(tmp_path: Path) -> None:
155
155
  """#100: a report with export= exports the same image as one without it."""
156
- with_menu = bf.save_image(_with_menu(("png", "pdf")), tmp_path / "menu.png", width=800, scale=1, name="Q1")
156
+ with_menu = bf.save_image(_with_menu(("png",)), tmp_path / "menu.png", width=800, scale=1, name="Q1")
157
157
  without = bf.save_image(_with_menu(()), tmp_path / "plain.png", width=800, scale=1, name="Q1")
158
158
  assert with_menu[0].read_bytes() == without[0].read_bytes()
@@ -0,0 +1,93 @@
1
+ """The in-report export menu works in Chromium (#49)."""
2
+ from __future__ import annotations
3
+
4
+ import base64
5
+ from pathlib import Path
6
+
7
+ import pandas as pd
8
+ from playwright.sync_api import Page
9
+
10
+ import briefing as bf
11
+ from tests.image_helpers import png_size
12
+ from tests.pdf_reader import read_pdf
13
+ from tests.test_browser.conftest import Viewer
14
+
15
+ #: The page background kept around the report in a "Save as PNG" image, in CSS px (export.js MARGIN).
16
+ MARGIN = 32
17
+
18
+
19
+ def _report() -> bf.Briefing:
20
+ return bf.Briefing(
21
+ bf.Page(bf.Text("# Summary\n\nShort."), bf.BigNumber("Revenue", "$4.2M"), title="Summary"),
22
+ bf.Page(bf.Text("# Detail"), bf.DataTable(pd.DataFrame({"n": range(30)})), title="Detail"),
23
+ export="png",
24
+ )
25
+
26
+
27
+ def _report_size(page: Page) -> tuple[float, float]:
28
+ box = page.locator(".bf-report").bounding_box()
29
+ assert box is not None
30
+ return box["width"], box["height"]
31
+
32
+
33
+ def test_save_as_png_downloads_the_current_page_at_2x(viewer: Viewer, tmp_path: Path) -> None:
34
+ page = viewer.open(_report(), name="Q1 Report")
35
+ page.get_by_role("tab", name="Detail").click()
36
+ width, height = _report_size(page)
37
+ with page.expect_download() as download:
38
+ page.get_by_role("button", name="Save as PNG").click()
39
+ saved = tmp_path / download.value.suggested_filename
40
+ download.value.save_as(saved)
41
+ assert saved.name == "q1_report_detail.png"
42
+ w, h = png_size(saved)
43
+ assert abs(w - 2 * (width + 2 * MARGIN)) <= 2 and abs(h - 2 * (height + 2 * MARGIN)) <= 2
44
+
45
+
46
+ # Decode a PNG in the page: the colours found in its outer band of `band` pixels, and how
47
+ # many pixels just inside that band differ from the page background.
48
+ _EDGES_JS = """async ([src, band]) => {
49
+ const img = new Image(); img.src = src; await img.decode();
50
+ const canvas = document.createElement('canvas');
51
+ canvas.width = img.naturalWidth; canvas.height = img.naturalHeight;
52
+ const ctx = canvas.getContext('2d'); ctx.drawImage(img, 0, 0);
53
+ const px = ctx.getImageData(0, 0, canvas.width, canvas.height).data;
54
+ const at = (x, y) => Array.from(px.slice((y * canvas.width + x) * 4, (y * canvas.width + x) * 4 + 3)).join(',');
55
+ const probe = document.createElement('canvas').getContext('2d');
56
+ probe.fillStyle = getComputedStyle(document.body).backgroundColor; probe.fillRect(0, 0, 1, 1);
57
+ const background = Array.from(probe.getImageData(0, 0, 1, 1).data.slice(0, 3)).join(',');
58
+ const edge = new Set(); let inside = 0;
59
+ for (let y = 0; y < canvas.height; y++) for (let x = 0; x < canvas.width; x++) {
60
+ const inBand = x < band || y < band || x >= canvas.width - band || y >= canvas.height - band;
61
+ if (inBand) edge.add(at(x, y));
62
+ else if (at(x, y) !== background) inside++;
63
+ }
64
+ return {edge: Array.from(edge), background, inside};
65
+ }"""
66
+
67
+
68
+ def test_save_as_png_keeps_a_margin_of_page_background(viewer: Viewer, tmp_path: Path) -> None:
69
+ """Content never touches the image's edge: a MARGIN-wide band of page background frames it."""
70
+ page = viewer.open(_report(), name="Q1 Report")
71
+ with page.expect_download() as download:
72
+ page.get_by_role("button", name="Save as PNG").click()
73
+ saved = tmp_path / "q1.png"
74
+ download.value.save_as(saved)
75
+ src = "data:image/png;base64," + base64.b64encode(saved.read_bytes()).decode("ascii")
76
+ found = page.evaluate(_EDGES_JS, [src, 2 * MARGIN]) # the image is drawn at 2x
77
+ assert found["edge"] == [found["background"]]
78
+ assert found["inside"] > 0, "the report itself should be drawn inside the margin"
79
+
80
+
81
+ def test_there_is_no_print_button(viewer: Viewer) -> None:
82
+ """The browser's own print (Ctrl+P) does what the old "Print" button did."""
83
+ page = viewer.open(_report(), name="Q1 Report")
84
+ assert page.locator(".bf-export__btn").all_inner_texts() == ["Save as PNG"]
85
+
86
+
87
+ def test_the_menu_does_not_print(viewer: Viewer) -> None:
88
+ page = viewer.open(_report(), name="Q1 Report")
89
+ text = "\n".join(read_pdf(page.pdf(format="A4")).pages)
90
+ assert "Save as PNG" not in text and "Print" not in text
91
+ page.emulate_media(media="print")
92
+ page.evaluate("window.dispatchEvent(new Event('beforeprint'))")
93
+ assert page.locator(".bf-export").evaluate("e => getComputedStyle(e).display") == "none"
@@ -150,7 +150,7 @@ SCENARIOS: dict[str, Build] = {
150
150
  bf.Banner("## Left", image=RED_SVG, side="left", text_position=40, focus=(20, 80), alt="A red field"),
151
151
  ),
152
152
  "export-menu": lambda _: bf.Briefing(
153
- bf.Page(bf.Text("# One"), title="One"), bf.Page(bf.Text("# Two"), title="Two"), export=("png", "pdf")
153
+ bf.Page(bf.Text("# One"), title="One"), bf.Page(bf.Text("# Two"), title="Two"), export="png"
154
154
  ),
155
155
  "everything": lambda df: bf.Briefing(
156
156
  bf.Text("# Everything"),
@@ -92,3 +92,29 @@ def test_the_day_calendar_alone_does_not_warn(df_pipeline_log: t.Any) -> None:
92
92
  warnings.simplefilter("error")
93
93
  bf.lab.CalendarHeatmap(df_pipeline_log, date="run_at")
94
94
  bf.lab.CalendarHeatmap(df_pipeline_log, date="run_at", views=("days",))
95
+
96
+
97
+ # ── Briefing(export="pdf"): the "Print" button ────────────────────────────────
98
+
99
+
100
+ @pytest.mark.parametrize("export", ["pdf", ("png", "pdf"), ["pdf", "png", "pdf"]])
101
+ def test_export_pdf_warns_once_and_adds_no_button(export: t.Any) -> None:
102
+ expected = r"export='pdf'.*deprecated.*Ctrl\+P.*bf\.save_pdf.*0\.4\.0"
103
+ with pytest.warns(DeprecationWarning, match=expected) as caught:
104
+ report = bf.Briefing(bf.Text("x"), export=export)
105
+ assert len(caught) == 1
106
+ assert caught[0].filename == __file__ # points at the caller's line (stacklevel=3)
107
+ assert "pdf" not in report.export
108
+
109
+
110
+ def test_export_png_alone_does_not_warn() -> None:
111
+ with warnings.catch_warnings():
112
+ warnings.simplefilter("error")
113
+ bf.Briefing(bf.Text("x"), export="png")
114
+
115
+
116
+ def test_export_pdf_is_rejected_from_0_4() -> None:
117
+ # Fails from 0.4 on until "pdf" leaves Briefing.DEPRECATED_EXPORTS and raises BriefingError.
118
+ version = tuple(int(part) for part in bf.__version__.split(".")[:2])
119
+ if version >= (0, 4):
120
+ assert "pdf" not in bf.Briefing.DEPRECATED_EXPORTS
@@ -0,0 +1,74 @@
1
+ """The opt-in export menu: bf.Briefing(..., export="png") (#49, #119)."""
2
+ from __future__ import annotations
3
+
4
+ import warnings
5
+
6
+ import pytest
7
+
8
+ import briefing as bf
9
+ from tests.html_helpers import attr, find_one, parse
10
+
11
+ EXPORT_JS_MARKER = "briefing export menu"
12
+
13
+
14
+ def test_menu_lists_the_requested_actions_in_the_header() -> None:
15
+ html = bf.stringify(bf.Briefing(bf.Text("x"), export=("png",)), name="Q1")
16
+ menu = find_one(parse(html), ".bf-report__header .bf-export")
17
+ buttons = menu.select("button.bf-export__btn")
18
+ assert [attr(b, "data-export") for b in buttons] == ["png"]
19
+ assert [b.get_text() for b in buttons] == ["Save as PNG"]
20
+ assert EXPORT_JS_MARKER in html
21
+
22
+
23
+ def test_a_single_string_works() -> None:
24
+ soup = parse(bf.stringify(bf.Briefing(bf.Text("x"), export="png"), name="Q1"))
25
+ assert [attr(b, "data-export") for b in soup.select(".bf-export button")] == ["png"]
26
+
27
+
28
+ def test_reports_without_export_contain_no_export_code() -> None:
29
+ for report in (bf.Briefing(bf.Text("x")), bf.Briefing(bf.Text("x"), export=())):
30
+ html = bf.stringify(report, name="Q1")
31
+ assert "bf-export" not in html and EXPORT_JS_MARKER not in html
32
+
33
+
34
+ def test_menu_gets_a_header_even_with_the_default_name() -> None:
35
+ soup = parse(bf.stringify(bf.Briefing(bf.Text("x"), export=("png",))))
36
+ assert find_one(soup, ".bf-report__header .bf-export")
37
+
38
+
39
+ @pytest.mark.parametrize("bad", [("png", "gif"), ("svg",), "print", 3])
40
+ def test_unknown_actions_raise(bad: object) -> None:
41
+ with pytest.raises(bf.BriefingError, match="export accepts 'png'"):
42
+ bf.Briefing(bf.Text("x"), export=bad) # type: ignore[arg-type]
43
+
44
+
45
+ def test_export_is_kept_when_a_briefing_is_rewrapped() -> None:
46
+ inner = bf.Briefing(bf.Text("x"), export=("png",))
47
+ assert bf.Briefing(inner).export == ("png",)
48
+ assert bf.Briefing.wrap(inner).export == ("png",)
49
+
50
+
51
+ # ── export="pdf": the "Print" button is gone (deprecated) ────────────────────
52
+
53
+
54
+ def test_pdf_adds_no_print_button() -> None:
55
+ with pytest.warns(DeprecationWarning):
56
+ report = bf.Briefing(bf.Text("x"), export=("png", "pdf"))
57
+ assert report.export == ("png",)
58
+ soup = parse(bf.stringify(report, name="Q1"))
59
+ assert [b.get_text() for b in soup.select(".bf-export button")] == ["Save as PNG"]
60
+
61
+
62
+ def test_pdf_alone_leaves_no_menu_and_no_export_code() -> None:
63
+ with pytest.warns(DeprecationWarning):
64
+ report = bf.Briefing(bf.Text("x"), export="pdf")
65
+ html = bf.stringify(report, name="Q1")
66
+ assert "bf-export" not in html and EXPORT_JS_MARKER not in html
67
+
68
+
69
+ def test_rewrapping_a_briefing_does_not_warn_twice() -> None:
70
+ with pytest.warns(DeprecationWarning):
71
+ inner = bf.Briefing(bf.Text("x"), export=("png", "pdf"))
72
+ with warnings.catch_warnings():
73
+ warnings.simplefilter("error")
74
+ assert bf.Briefing(inner).export == ("png",)
@@ -1,55 +0,0 @@
1
- """The in-report export menu works in Chromium (#49)."""
2
- from __future__ import annotations
3
-
4
- from pathlib import Path
5
-
6
- import pandas as pd
7
- from playwright.sync_api import Page
8
-
9
- import briefing as bf
10
- from tests.image_helpers import png_size
11
- from tests.pdf_reader import read_pdf
12
- from tests.test_browser.conftest import Viewer
13
-
14
-
15
- def _report() -> bf.Briefing:
16
- return bf.Briefing(
17
- bf.Page(bf.Text("# Summary\n\nShort."), bf.BigNumber("Revenue", "$4.2M"), title="Summary"),
18
- bf.Page(bf.Text("# Detail"), bf.DataTable(pd.DataFrame({"n": range(30)})), title="Detail"),
19
- export=("png", "pdf"),
20
- )
21
-
22
-
23
- def _report_size(page: Page) -> tuple[float, float]:
24
- box = page.locator(".bf-report").bounding_box()
25
- assert box is not None
26
- return box["width"], box["height"]
27
-
28
-
29
- def test_save_as_png_downloads_the_current_page_at_2x(viewer: Viewer, tmp_path: Path) -> None:
30
- page = viewer.open(_report(), name="Q1 Report")
31
- page.get_by_role("tab", name="Detail").click()
32
- width, height = _report_size(page)
33
- with page.expect_download() as download:
34
- page.get_by_role("button", name="Save as PNG").click()
35
- saved = tmp_path / download.value.suggested_filename
36
- download.value.save_as(saved)
37
- assert saved.name == "q1_report_detail.png"
38
- w, h = png_size(saved)
39
- assert abs(w - 2 * width) <= 2 and abs(h - 2 * height) <= 2
40
-
41
-
42
- def test_print_opens_the_print_dialog(viewer: Viewer) -> None:
43
- page = viewer.open(_report(), name="Q1 Report")
44
- page.evaluate("() => { window.__printed = 0; window.print = () => { window.__printed += 1; }; }")
45
- page.get_by_role("button", name="Print").click()
46
- assert page.evaluate("() => window.__printed") == 1
47
-
48
-
49
- def test_the_menu_does_not_print(viewer: Viewer) -> None:
50
- page = viewer.open(_report(), name="Q1 Report")
51
- text = "\n".join(read_pdf(page.pdf(format="A4")).pages)
52
- assert "Save as PNG" not in text and "Print" not in text
53
- page.emulate_media(media="print")
54
- page.evaluate("window.dispatchEvent(new Event('beforeprint'))")
55
- assert page.locator(".bf-export").evaluate("e => getComputedStyle(e).display") == "none"
@@ -1,46 +0,0 @@
1
- """The opt-in export menu: bf.Briefing(..., export=("png", "pdf")) (#49)."""
2
- from __future__ import annotations
3
-
4
- import pytest
5
-
6
- import briefing as bf
7
- from tests.html_helpers import attr, find_one, parse
8
-
9
- EXPORT_JS_MARKER = "briefing export menu"
10
-
11
-
12
- def test_menu_lists_the_requested_actions_in_the_header() -> None:
13
- html = bf.stringify(bf.Briefing(bf.Text("x"), export=("png", "pdf")), name="Q1")
14
- menu = find_one(parse(html), ".bf-report__header .bf-export")
15
- buttons = menu.select("button.bf-export__btn")
16
- assert [attr(b, "data-export") for b in buttons] == ["png", "pdf"]
17
- assert [b.get_text() for b in buttons] == ["Save as PNG", "Print"]
18
- assert EXPORT_JS_MARKER in html
19
-
20
-
21
- def test_one_action_and_a_single_string() -> None:
22
- soup = parse(bf.stringify(bf.Briefing(bf.Text("x"), export="pdf"), name="Q1"))
23
- assert [attr(b, "data-export") for b in soup.select(".bf-export button")] == ["pdf"]
24
-
25
-
26
- def test_reports_without_export_contain_no_export_code() -> None:
27
- for report in (bf.Briefing(bf.Text("x")), bf.Briefing(bf.Text("x"), export=())):
28
- html = bf.stringify(report, name="Q1")
29
- assert "bf-export" not in html and EXPORT_JS_MARKER not in html
30
-
31
-
32
- def test_menu_gets_a_header_even_with_the_default_name() -> None:
33
- soup = parse(bf.stringify(bf.Briefing(bf.Text("x"), export=("png",))))
34
- assert find_one(soup, ".bf-report__header .bf-export")
35
-
36
-
37
- @pytest.mark.parametrize("bad", [("png", "gif"), ("svg",), 3])
38
- def test_unknown_actions_raise(bad: object) -> None:
39
- with pytest.raises(bf.BriefingError, match="png.*pdf"):
40
- bf.Briefing(bf.Text("x"), export=bad) # type: ignore[arg-type]
41
-
42
-
43
- def test_export_is_kept_when_a_briefing_is_rewrapped() -> None:
44
- inner = bf.Briefing(bf.Text("x"), export=("pdf",))
45
- assert bf.Briefing(inner).export == ("pdf",)
46
- assert bf.Briefing.wrap(inner).export == ("pdf",)
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes