clearoutput.py 930 B

1234567891011121314151617181920212223242526272829
  1. """Module containing a preprocessor that removes the outputs from code cells"""
  2. # Copyright (c) IPython Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. from traitlets import Set
  5. from .base import Preprocessor
  6. class ClearOutputPreprocessor(Preprocessor):
  7. """
  8. Removes the output from all code cells in a notebook.
  9. """
  10. remove_metadata_fields = Set({"collapsed", "scrolled"}).tag(config=True)
  11. def preprocess_cell(self, cell, resources, cell_index):
  12. """
  13. Apply a transformation on each cell. See base.py for details.
  14. """
  15. if cell.cell_type == "code":
  16. cell.outputs = []
  17. cell.execution_count = None
  18. # Remove metadata associated with output
  19. if "metadata" in cell:
  20. for field in self.remove_metadata_fields:
  21. cell.metadata.pop(field, None)
  22. return cell, resources