From 15d0a3b8376165bd90f2a00244754205864bb6b7 2014-04-16 14:27:25 From: Julia Evans Date: 2014-04-16 14:27:25 Subject: [PATCH] Add preprocessor that clears cell outputs --- diff --git a/IPython/nbconvert/exporters/exporter.py b/IPython/nbconvert/exporters/exporter.py index 61a17f8..6fd13c7 100644 --- a/IPython/nbconvert/exporters/exporter.py +++ b/IPython/nbconvert/exporters/exporter.py @@ -70,6 +70,7 @@ class Exporter(LoggingConfigurable): 'IPython.nbconvert.preprocessors.CSSHTMLHeaderPreprocessor', 'IPython.nbconvert.preprocessors.RevealHelpPreprocessor', 'IPython.nbconvert.preprocessors.LatexPreprocessor', + 'IPython.nbconvert.preprocessors.ClearOutputPreprocessor', 'IPython.nbconvert.preprocessors.HighlightMagicsPreprocessor'], config=True, help="""List of preprocessors available by default, by name, namespace, diff --git a/IPython/nbconvert/preprocessors/__init__.py b/IPython/nbconvert/preprocessors/__init__.py index 3062c79..35e61e9 100755 --- a/IPython/nbconvert/preprocessors/__init__.py +++ b/IPython/nbconvert/preprocessors/__init__.py @@ -7,6 +7,7 @@ from .revealhelp import RevealHelpPreprocessor from .latex import LatexPreprocessor from .csshtmlheader import CSSHTMLHeaderPreprocessor from .highlightmagics import HighlightMagicsPreprocessor +from .clearoutput import ClearOutputPreprocessor # decorated function Preprocessors from .coalescestreams import coalesce_streams diff --git a/IPython/nbconvert/preprocessors/clearoutput.py b/IPython/nbconvert/preprocessors/clearoutput.py new file mode 100644 index 0000000..681fa23 --- /dev/null +++ b/IPython/nbconvert/preprocessors/clearoutput.py @@ -0,0 +1,32 @@ +"""Module containing a preprocessor that removes the outputs from code cells""" + +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- + +from .base import Preprocessor + + +#----------------------------------------------------------------------------- +# Classes +#----------------------------------------------------------------------------- +class ClearOutputPreprocessor(Preprocessor): + """ + Removes the output from all code cells in a notebook. + """ + + 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 = [] + return cell, resources diff --git a/IPython/nbconvert/preprocessors/tests/test_clearoutput.py b/IPython/nbconvert/preprocessors/tests/test_clearoutput.py new file mode 100644 index 0000000..6b58811 --- /dev/null +++ b/IPython/nbconvert/preprocessors/tests/test_clearoutput.py @@ -0,0 +1,46 @@ +""" +Module with tests for the clearoutput preprocessor. +""" + +#----------------------------------------------------------------------------- +# Copyright (c) 2013, the IPython Development Team. +# +# Distributed under the terms of the Modified BSD License. +# +# The full license is in the file COPYING.txt, distributed with this software. +#----------------------------------------------------------------------------- + +#----------------------------------------------------------------------------- +# Imports +#----------------------------------------------------------------------------- +from IPython.nbformat import current as nbformat + +from .base import PreprocessorTestsBase +from ..clearoutput import ClearOutputPreprocessor + + +#----------------------------------------------------------------------------- +# Class +#----------------------------------------------------------------------------- + +class TestClearOutput(PreprocessorTestsBase): + """Contains test functions for clearoutput.py""" + + + def build_preprocessor(self): + """Make an instance of a preprocessor""" + preprocessor = ClearOutputPreprocessor() + preprocessor.enabled = True + return preprocessor + + def test_constructor(self): + """Can a ClearOutputPreprocessor be constructed?""" + self.build_preprocessor() + + 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.worksheets[0].cells[0].outputs == []