From a3eb9a5cc767f0e5f2a1f06ede3eb4bb3bc97930 Mon Sep 17 00:00:00 2001 From: Witold Date: Wed, 4 Dec 2024 11:10:44 +0100 Subject: [PATCH] fix: PDF export error when no measuring point image --- lab_notebook/pdf_export/pdf.py | 5 +-- lab_notebook/tests/pdf_export/test_pdf.py | 43 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lab_notebook/pdf_export/pdf.py b/lab_notebook/pdf_export/pdf.py index 8bbad041c..efc3c129b 100644 --- a/lab_notebook/pdf_export/pdf.py +++ b/lab_notebook/pdf_export/pdf.py @@ -137,8 +137,9 @@ def create_pdf( story.extend(generate_experimental_conditions_story(run, run_methods)) story.append(Spacer(1, 0.2 * inch)) add_comments(story, run["run_notebook"]["comments"]) - story.append(Spacer(1, 0.2 * inch)) - story.extend(generate_images_with_points_story(images, tmpdirname)) + if images: + story.append(Spacer(1, 0.2 * inch)) + story.extend(generate_images_with_points_story(images, tmpdirname)) story.append(PageBreak()) add_table_of_contents(story) story.append(PageBreak()) diff --git a/lab_notebook/tests/pdf_export/test_pdf.py b/lab_notebook/tests/pdf_export/test_pdf.py index 03895dafa..54e4a1da4 100644 --- a/lab_notebook/tests/pdf_export/test_pdf.py +++ b/lab_notebook/tests/pdf_export/test_pdf.py @@ -289,3 +289,46 @@ def test_create_pdf(): f.close() assert Canvas(f.name, pagesize=A4) + + +def test_create_pdf_with_no_image(): + run = { + "label": "test-run", + "project": {"slug": "test-project", "name": "Test Project"}, + "particle_type": "electron", + "energy_in_keV": "100", + "beamline": "test-beamline", + "run_notebook": {"comments": "Test comments"}, + } + run_methods = [ + mock.Mock( + name="Test Method", + detectors=[ + mock.Mock( + name="Test Detector", + filters=["Test Filter"], + ) + ], + ) + ] + measuring_points = [ + { + "name": "point-test-1", + "comments": "test comment 1", + "object_group": {"label": "object test 1"}, + "standard": None, + }, + { + "name": "point-test-2", + "comments": "test comment 2", + "object_group": None, + "standard": {"label": "standard test 2"}, + }, + ] + images = [] + + with tempfile.NamedTemporaryFile("w+b", suffix=".pdf", delete=False) as f: + create_pdf(f.name, run, run_methods, measuring_points, images) + f.close() + + assert Canvas(f.name, pagesize=A4)