Skip to content

Commit

Permalink
Handle nbformat 5.5 (#1841)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Aug 21, 2022
1 parent aec3928 commit 8c1146d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 31 deletions.
9 changes: 8 additions & 1 deletion nbconvert/exporters/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import Optional

import nbformat
from nbformat import validator
from traitlets import Bool, HasTraits, List, TraitError, Unicode
from traitlets.config import Config
from traitlets.config.configurable import LoggingConfigurable
Expand Down Expand Up @@ -304,7 +305,10 @@ def _init_resources(self, resources):

def _validate_preprocessor(self, nbc, preprocessor):
try:
nbformat.validate(nbc, relax_add_props=True)
if not hasattr(validator, "normalize"):
nbformat.validate(nbc, relax_add_props=True)
else:
nbformat.validate(nbc)
except nbformat.ValidationError:
self.log.error("Notebook is invalid after preprocessor %s", preprocessor)
raise
Expand All @@ -329,6 +333,9 @@ def _preprocess(self, nb, resources):
nbc = copy.deepcopy(nb)
resc = copy.deepcopy(resources)

if hasattr(validator, "normalize"):
_, nbc = validator.normalize(nbc)

# Run each preprocessor on the notebook. Carry the output along
# to each preprocessor
for preprocessor in self._preprocessors:
Expand Down
18 changes: 0 additions & 18 deletions nbconvert/exporters/tests/files/notebook_inject.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,6 @@
],
"source": [""]
},
{
"cell_type": "code",
"execution_count": null,
"id": "64d5d39f",
"metadata": {},
"outputs": [
{
"output_type": "execute_result",
"svg_filename": "\"><script>alert('svg_filename')</script>",
"data": {
"image/svg+xml": [""]
},
"execution_count": null,
"metadata": {}
}
],
"source": [""]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
6 changes: 3 additions & 3 deletions nbconvert/exporters/tests/test_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@


class PizzaPreprocessor(Preprocessor):
"""Simple preprocessor that adds a 'pizza' entry to the NotebookNode. Used
"""Simple preprocessor that adds a 'pizza' entry to the Notebook metadata. Used
to test Exporter.
"""

def preprocess(self, nb, resources):
nb["pizza"] = "cheese"
nb["metadata"]["pizza"] = "cheese"
return nb, resources


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

def test_get_export_names_disable(self):
"""Can we disable all exporters then enable a single one"""
Expand Down
2 changes: 1 addition & 1 deletion nbconvert/preprocessors/tagremove.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def preprocess_cell(self, cell, resources, cell_index):
cell.metadata.pop(field, None)

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

if cell.get("outputs", []):
cell.outputs = [
Expand Down
8 changes: 4 additions & 4 deletions share/templates/base/null.j2
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ consider calling super even if it is a leaf block, we might insert more blocks l
{%- if cell.cell_type == 'code'-%}
{%- if resources.global_content_filter.include_code -%}
{%- block codecell scoped -%}
{%- if resources.global_content_filter.include_input and not cell.get("transient",{}).get("remove_source", false) -%}
{%- if resources.global_content_filter.include_input and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
{%- block input_group -%}
{%- if resources.global_content_filter.include_input_prompt -%}
{%- block in_prompt -%}{%- endblock in_prompt -%}
Expand Down Expand Up @@ -83,19 +83,19 @@ consider calling super even if it is a leaf block, we might insert more blocks l
{%- endblock codecell -%}
{%- endif -%}
{%- elif cell.cell_type in ['markdown'] -%}
{%- if resources.global_content_filter.include_markdown and not cell.get("transient",{}).get("remove_source", false) -%}
{%- if resources.global_content_filter.include_markdown and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
{%- block markdowncell scoped-%} {%- endblock markdowncell -%}
{%- endif -%}
{%- elif cell.cell_type in ['raw'] -%}
{%- if resources.global_content_filter.include_raw and not cell.get("transient",{}).get("remove_source", false) -%}
{%- if resources.global_content_filter.include_raw and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
{%- block rawcell scoped -%}
{%- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) -%}
{{ cell.source }}
{%- endif -%}
{%- endblock rawcell -%}
{%- endif -%}
{%- else -%}
{%- if resources.global_content_filter.include_unknown and not cell.get("transient",{}).get("remove_source", false) -%}
{%- if resources.global_content_filter.include_unknown and not cell.metadata.get("transient",{}).get("remove_source", false) -%}
{%- block unknowncell scoped-%}
{%- endblock unknowncell -%}
{%- endif -%}
Expand Down
8 changes: 4 additions & 4 deletions share/templates/latex/null.j2
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ consider calling super even if it is a leaf block, we might insert more blocks l
((*- if cell.cell_type == 'code'-*))
((*- if resources.global_content_filter.include_code -*))
((*- block codecell scoped -*))
((*- if resources.global_content_filter.include_input and not cell.get("transient",{}).get("remove_source", false) -*))
((*- if resources.global_content_filter.include_input and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
((*- block input_group -*))
((*- if resources.global_content_filter.include_input_prompt -*))
((*- block in_prompt -*))((*- endblock in_prompt -*))
Expand Down Expand Up @@ -81,19 +81,19 @@ consider calling super even if it is a leaf block, we might insert more blocks l
((*- endblock codecell -*))
((*- endif -*))
((*- elif cell.cell_type in ['markdown'] -*))
((*- if resources.global_content_filter.include_markdown and not cell.get("transient",{}).get("remove_source", false) -*))
((*- if resources.global_content_filter.include_markdown and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
((*- block markdowncell scoped-*)) ((*- endblock markdowncell -*))
((*- endif -*))
((*- elif cell.cell_type in ['raw'] -*))
((*- if resources.global_content_filter.include_raw and not cell.get("transient",{}).get("remove_source", false) -*))
((*- if resources.global_content_filter.include_raw and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
((*- block rawcell scoped -*))
((*- if cell.metadata.get('raw_mimetype', '').lower() in resources.get('raw_mimetypes', ['']) -*))
((( cell.source )))
((*- endif -*))
((*- endblock rawcell -*))
((*- endif -*))
((*- else -*))
((*- if resources.global_content_filter.include_unknown and not cell.get("transient",{}).get("remove_source", false) -*))
((*- if resources.global_content_filter.include_unknown and not cell.metadata.get("transient",{}).get("remove_source", false) -*))
((*- block unknowncell scoped-*))
((*- endblock unknowncell -*))
((*- endif -*))
Expand Down

0 comments on commit 8c1146d

Please sign in to comment.