clearoutput.py 942 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(
  11. {'collapsed', 'scrolled'}
  12. ).tag(config=True)
  13. def preprocess_cell(self, cell, resources, cell_index):
  14. """
  15. Apply a transformation on each cell. See base.py for details.
  16. """
  17. if cell.cell_type == 'code':
  18. cell.outputs = []
  19. cell.execution_count = None
  20. # Remove metadata associated with output
  21. if 'metadata' in cell:
  22. for field in self.remove_metadata_fields:
  23. cell.metadata.pop(field, None)
  24. return cell, resources