From 5edec9282ee5fe2e8405b330793803e230f5521a Mon Sep 17 00:00:00 2001 From: Chris Pyles Date: Sun, 16 Jul 2023 10:11:27 -0700 Subject: [PATCH 1/4] normalize notebook before write --- otter/assign/notebook_transformer.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/otter/assign/notebook_transformer.py b/otter/assign/notebook_transformer.py index 0f94d2ea6..7f292e6b2 100644 --- a/otter/assign/notebook_transformer.py +++ b/otter/assign/notebook_transformer.py @@ -3,6 +3,8 @@ import copy import nbformat +from nbformat.validator import normalize + from .assignment import Assignment from .blocks import BlockType, get_cell_config, is_assignment_config_cell, is_block_boundary_cell from .cell_factory import CellFactory @@ -377,6 +379,7 @@ def write_transformed_nb(self, output_path, sanitize): if self.nb_transformer.assignment.is_rmd: rmarkdown_converter.write_as_rmd(nb, str(output_path), not sanitize) else: + _, nb = normalize(nb) nbformat.write(nb, str(output_path)) def write_tests(self, tests_dir, include_hidden, force_files): From b72aac64314d0ac696573d8e946e4f5376d2ca05 Mon Sep 17 00:00:00 2001 From: Chris Pyles Date: Sun, 16 Jul 2023 10:15:31 -0700 Subject: [PATCH 2/4] changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c2ffd2664..394c63e09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ * Enabled Otter Generate for all assignments created with Otter Assign * Updated Otter Assign to use Otter Run to validate that all tests in the assignment pass, allowing tests to be run for R assignments as well as Python, per [#427](https://github.com/ucbds-infra/otter-grader/issues/427) * Allow Markdown cells to be used instead of raw cells for Otter Assign per [#592](https://github.com/ucbds-infra/otter-grader/issues/592) +* Updated Otter Assign to normalize notebooks before writing them with `nbformat.validator.normalize` per [#658](https://github.com/ucbds-infra/otter-grader/issues/658) **v4.4.0:** From f70136b46e170d050780390a69d177934f9b6c7a Mon Sep 17 00:00:00 2001 From: Chris Pyles Date: Sun, 16 Jul 2023 10:43:57 -0700 Subject: [PATCH 3/4] handle versions of nbformat without normalize --- otter/assign/notebook_transformer.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/otter/assign/notebook_transformer.py b/otter/assign/notebook_transformer.py index 7f292e6b2..0ad988d51 100644 --- a/otter/assign/notebook_transformer.py +++ b/otter/assign/notebook_transformer.py @@ -3,8 +3,6 @@ import copy import nbformat -from nbformat.validator import normalize - from .assignment import Assignment from .blocks import BlockType, get_cell_config, is_assignment_config_cell, is_block_boundary_cell from .cell_factory import CellFactory @@ -379,6 +377,11 @@ def write_transformed_nb(self, output_path, sanitize): if self.nb_transformer.assignment.is_rmd: rmarkdown_converter.write_as_rmd(nb, str(output_path), not sanitize) else: + try: + from nbformat.validator import normalize + except ImportError: + normalize = lambda nb: 0, nb + _, nb = normalize(nb) nbformat.write(nb, str(output_path)) From 08a0992462cc146af2dd393921457919e60e3a67 Mon Sep 17 00:00:00 2001 From: Chris Pyles Date: Sun, 16 Jul 2023 11:21:00 -0700 Subject: [PATCH 4/4] add parens --- otter/assign/notebook_transformer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/otter/assign/notebook_transformer.py b/otter/assign/notebook_transformer.py index 0ad988d51..68c1da3d4 100644 --- a/otter/assign/notebook_transformer.py +++ b/otter/assign/notebook_transformer.py @@ -380,7 +380,7 @@ def write_transformed_nb(self, output_path, sanitize): try: from nbformat.validator import normalize except ImportError: - normalize = lambda nb: 0, nb + normalize = lambda nb: (0, nb) _, nb = normalize(nb) nbformat.write(nb, str(output_path))