Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions nbconvert/nbconvertapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ def validate(self, obj, value):
"""Clear output of current file and save in place,
overwriting the existing notebook. """,
),
"clear-metadata": (
{
"NbConvertApp": {
"use_output_suffix": False,
"export_format": "notebook",
},
"FilesWriter": {"build_directory": ""},
"ClearMetadataPreprocessor": {"enabled": True},
},
"""Clear metadata of current file and save in place,
overwriting the existing notebook. """,
),
"coalesce-streams": (
{
"NbConvertApp": {"use_output_suffix": False, "export_format": "notebook"},
Expand Down
25 changes: 12 additions & 13 deletions nbconvert/preprocessors/clearmetadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Module containing a preprocessor that removes metadata from code cells"""
"""Module containing a preprocessor that removes notebook or cell metadata"""

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Expand All @@ -10,7 +10,7 @@

class ClearMetadataPreprocessor(Preprocessor):
"""
Removes all the metadata from all code cells in a notebook.
Removes global or cell-level metadata from a notebook.
"""

clear_cell_metadata = Bool(
Expand All @@ -24,16 +24,16 @@ class ClearMetadataPreprocessor(Preprocessor):
preserve_nb_metadata_mask = Set(
[("language_info", "name")],
help=(
"Indicates the key paths to preserve when deleting metadata "
"across both cells and notebook metadata fields. Tuples of "
"keys can be passed to preserved specific nested values"
"Indicates the key paths to preserve when deleting notebook "
"metadata fields. Tuples of keys can be passed to preserve "
"specific nested values."
),
).tag(config=True)
preserve_cell_metadata_mask = Set(
help=(
"Indicates the key paths to preserve when deleting metadata "
"across both cells and notebook metadata fields. Tuples of "
"keys can be passed to preserved specific nested values"
"across all cells in a notebook. Tuples of keys can be passed "
"to preserve specific nested values."
)
).tag(config=True)

Expand Down Expand Up @@ -73,14 +73,13 @@ def nested_filter(self, items, mask):

def preprocess_cell(self, cell, resources, cell_index):
"""
All the code cells are returned with an empty metadata field.
All the cells are returned with a trimmed down metadata field.
"""
if self.clear_cell_metadata and cell.cell_type == "code": # noqa: SIM102
if self.clear_cell_metadata and "metadata" in cell:
# Remove metadata
if "metadata" in cell:
cell.metadata = dict(
self.nested_filter(cell.metadata.items(), self.preserve_cell_metadata_mask)
)
cell.metadata = dict(
self.nested_filter(cell.metadata.items(), self.preserve_cell_metadata_mask)
)
return cell, resources

def preprocess(self, nb, resources):
Expand Down
Loading