Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove output metadata in ClearOutputPreprocessor. #569

Merged
merged 15 commits into from Apr 21, 2017
Merged
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -29,6 +29,7 @@ before_install:
- git clone --quiet --depth 1 https://github.com/minrk/travis-wheels travis-wheels
install:
- wget https://github.com/jgm/pandoc/releases/download/1.19.1/pandoc-1.19.1-1-amd64.deb && sudo dpkg -i pandoc-1.19.1-1-amd64.deb
- pip install --upgrade setuptools pip
- pip install -f travis-wheels/wheelhouse . codecov coverage
- pip install nbconvert[execute,serve,test]
- python -m ipykernel.kernelspec --user
Expand Down
9 changes: 9 additions & 0 deletions nbconvert/preprocessors/clearoutput.py
Expand Up @@ -3,18 +3,27 @@
# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

from traitlets import Set
from .base import Preprocessor

class ClearOutputPreprocessor(Preprocessor):
"""
Removes the output from all code cells in a notebook.
"""

remove_metadata_fields = Set(
{'collapsed', 'scrolled'}
).tag(config=True)

def preprocess_cell(self, cell, resources, cell_index):
"""
Apply a transformation on each cell. See base.py for details.
"""
if cell.cell_type == 'code':
cell.outputs = []
cell.execution_count = None
# Remove metadata associated with output
if 'metadata' in cell:
for field in self.remove_metadata_fields:
cell.metadata.pop(field, None)
return cell, resources
28 changes: 22 additions & 6 deletions nbconvert/preprocessors/tests/test_clearoutput.py
Expand Up @@ -12,6 +12,13 @@
class TestClearOutput(PreprocessorTestsBase):
"""Contains test functions for clearoutput.py"""

def build_notebook(self):
notebook = super(TestClearOutput, self).build_notebook()
# Add a test field to the first cell
if 'metadata' not in notebook.cells[0]:
notebook.cells[0].metadata = {}
notebook.cells[0].metadata['test_field'] = 'test_value'
return notebook

def build_preprocessor(self):
"""Make an instance of a preprocessor"""
Expand All @@ -25,9 +32,18 @@ def test_constructor(self):

def test_output(self):
"""Test the output of the ClearOutputPreprocessor"""
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor()
nb, res = preprocessor(nb, res)
assert nb.cells[0].outputs == []
assert nb.cells[0].execution_count is None
for remove_test_field in [False, True]:
nb = self.build_notebook()
res = self.build_resources()
preprocessor = self.build_preprocessor()
# Also remove the test field in addition to defaults
if remove_test_field:
preprocessor.remove_metadata_fields.add('test_field')
nb, res = preprocessor(nb, res)
assert nb.cells[0].outputs == []
assert nb.cells[0].execution_count is None
if 'metadata' in nb.cells[0]:
for field in preprocessor.remove_metadata_fields:
assert field not in nb.cells[0].metadata
# Ensure the test field is only removed when added to the traitlet
assert remove_test_field or 'test_field' in nb.cells[0].metadata