Skip to content

Commit 8c1146d

Browse files
authored
Handle nbformat 5.5 (#1841)
1 parent aec3928 commit 8c1146d

File tree

6 files changed

+20
-31
lines changed

6 files changed

+20
-31
lines changed

nbconvert/exporters/exporter.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from typing import Optional
1515

1616
import nbformat
17+
from nbformat import validator
1718
from traitlets import Bool, HasTraits, List, TraitError, Unicode
1819
from traitlets.config import Config
1920
from traitlets.config.configurable import LoggingConfigurable
@@ -304,7 +305,10 @@ def _init_resources(self, resources):
304305

305306
def _validate_preprocessor(self, nbc, preprocessor):
306307
try:
307-
nbformat.validate(nbc, relax_add_props=True)
308+
if not hasattr(validator, "normalize"):
309+
nbformat.validate(nbc, relax_add_props=True)
310+
else:
311+
nbformat.validate(nbc)
308312
except nbformat.ValidationError:
309313
self.log.error("Notebook is invalid after preprocessor %s", preprocessor)
310314
raise
@@ -329,6 +333,9 @@ def _preprocess(self, nb, resources):
329333
nbc = copy.deepcopy(nb)
330334
resc = copy.deepcopy(resources)
331335

336+
if hasattr(validator, "normalize"):
337+
_, nbc = validator.normalize(nbc)
338+
332339
# Run each preprocessor on the notebook. Carry the output along
333340
# to each preprocessor
334341
for preprocessor in self._preprocessors:

nbconvert/exporters/tests/files/notebook_inject.ipynb

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,6 @@
4343
],
4444
"source": [""]
4545
},
46-
{
47-
"cell_type": "code",
48-
"execution_count": null,
49-
"id": "64d5d39f",
50-
"metadata": {},
51-
"outputs": [
52-
{
53-
"output_type": "execute_result",
54-
"svg_filename": "\"><script>alert('svg_filename')</script>",
55-
"data": {
56-
"image/svg+xml": [""]
57-
},
58-
"execution_count": null,
59-
"metadata": {}
60-
}
61-
],
62-
"source": [""]
63-
},
6446
{
6547
"cell_type": "code",
6648
"execution_count": null,

nbconvert/exporters/tests/test_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929

3030

3131
class PizzaPreprocessor(Preprocessor):
32-
"""Simple preprocessor that adds a 'pizza' entry to the NotebookNode. Used
32+
"""Simple preprocessor that adds a 'pizza' entry to the Notebook metadata. Used
3333
to test Exporter.
3434
"""
3535

3636
def preprocess(self, nb, resources):
37-
nb["pizza"] = "cheese"
37+
nb["metadata"]["pizza"] = "cheese"
3838
return nb, resources
3939

4040

@@ -56,7 +56,7 @@ def test_preprocessor(self):
5656
config = Config({"Exporter": {"preprocessors": [PizzaPreprocessor()]}})
5757
exporter = Exporter(config=config)
5858
(notebook, resources) = exporter.from_filename(self._get_notebook())
59-
self.assertEqual(notebook["pizza"], "cheese")
59+
self.assertEqual(notebook["metadata"]["pizza"], "cheese")
6060

6161
def test_get_export_names_disable(self):
6262
"""Can we disable all exporters then enable a single one"""

nbconvert/preprocessors/tagremove.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def preprocess_cell(self, cell, resources, cell_index):
119119
cell.metadata.pop(field, None)
120120

121121
if self.remove_input_tags.intersection(cell.get("metadata", {}).get("tags", [])):
122-
cell.transient = {"remove_source": True}
122+
cell.metadata["transient"] = {"remove_source": True}
123123

124124
if cell.get("outputs", []):
125125
cell.outputs = [

share/templates/base/null.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ consider calling super even if it is a leaf block, we might insert more blocks l
3232
{%- if cell.cell_type == 'code'-%}
3333
{%- if resources.global_content_filter.include_code -%}
3434
{%- block codecell scoped -%}
35-
{%- if resources.global_content_filter.include_input and not cell.get("transient",{}).get("remove_source", false) -%}
35+
{%- if resources.global_content_filter.include_input and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
3636
{%- block input_group -%}
3737
{%- if resources.global_content_filter.include_input_prompt -%}
3838
{%- block in_prompt -%}{%- endblock in_prompt -%}
@@ -83,19 +83,19 @@ consider calling super even if it is a leaf block, we might insert more blocks l
8383
{%- endblock codecell -%}
8484
{%- endif -%}
8585
{%- elif cell.cell_type in ['markdown'] -%}
86-
{%- if resources.global_content_filter.include_markdown and not cell.get("transient",{}).get("remove_source", false) -%}
86+
{%- if resources.global_content_filter.include_markdown and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
8787
{%- block markdowncell scoped-%} {%- endblock markdowncell -%}
8888
{%- endif -%}
8989
{%- elif cell.cell_type in ['raw'] -%}
90-
{%- if resources.global_content_filter.include_raw and not cell.get("transient",{}).get("remove_source", false) -%}
90+
{%- if resources.global_content_filter.include_raw and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
9191
{%- block rawcell scoped -%}
9292
{%- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) -%}
9393
{{ cell.source }}
9494
{%- endif -%}
9595
{%- endblock rawcell -%}
9696
{%- endif -%}
9797
{%- else -%}
98-
{%- if resources.global_content_filter.include_unknown and not cell.get("transient",{}).get("remove_source", false) -%}
98+
{%- if resources.global_content_filter.include_unknown and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
9999
{%- block unknowncell scoped-%}
100100
{%- endblock unknowncell -%}
101101
{%- endif -%}

share/templates/latex/null.j2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ consider calling super even if it is a leaf block, we might insert more blocks l
3333
((*- if cell.cell_type == 'code'-*))
3434
((*- if resources.global_content_filter.include_code -*))
3535
((*- block codecell scoped -*))
36-
((*- if resources.global_content_filter.include_input and not cell.get("transient",{}).get("remove_source", false) -*))
36+
((*- if resources.global_content_filter.include_input and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
3737
((*- block input_group -*))
3838
((*- if resources.global_content_filter.include_input_prompt -*))
3939
((*- block in_prompt -*))((*- endblock in_prompt -*))
@@ -81,19 +81,19 @@ consider calling super even if it is a leaf block, we might insert more blocks l
8181
((*- endblock codecell -*))
8282
((*- endif -*))
8383
((*- elif cell.cell_type in ['markdown'] -*))
84-
((*- if resources.global_content_filter.include_markdown and not cell.get("transient",{}).get("remove_source", false) -*))
84+
((*- if resources.global_content_filter.include_markdown and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
8585
((*- block markdowncell scoped-*)) ((*- endblock markdowncell -*))
8686
((*- endif -*))
8787
((*- elif cell.cell_type in ['raw'] -*))
88-
((*- if resources.global_content_filter.include_raw and not cell.get("transient",{}).get("remove_source", false) -*))
88+
((*- if resources.global_content_filter.include_raw and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
8989
((*- block rawcell scoped -*))
9090
((*- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) -*))
9191
((( cell.source )))
9292
((*- endif -*))
9393
((*- endblock rawcell -*))
9494
((*- endif -*))
9595
((*- else -*))
96-
((*- if resources.global_content_filter.include_unknown and not cell.get("transient",{}).get("remove_source", false) -*))
96+
((*- if resources.global_content_filter.include_unknown and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
9797
((*- block unknowncell scoped-*))
9898
((*- endblock unknowncell -*))
9999
((*- endif -*))

0 commit comments

Comments
 (0)