Skip to content

Latest commit

 

History

History
63 lines (44 loc) · 2.18 KB

README.md

File metadata and controls

63 lines (44 loc) · 2.18 KB

nbclean

Installation

You can install nbclean with pip:

pip install nbclean

Usage

You can use nbclean to "clean up" Jupyter notebooks, including:

  • Clear cell outputs, cell content, or components of cell outputs.
  • Replace text in cells with new text of your choosing.
  • Filter the above operations by the presence of cell tags.

The primary feature of nbclean is the NotebookCleaner class, which performs the above actions on a notebook according to tags that are in each cell's metadata.

# Clear different parts of the notebook cells based on tags
ntbk = nbc.NotebookCleaner(path_notebook)
ntbk.clear(kind='output', tag='hide_output')
ntbk.clear(kind='content', tag='hide_content')
ntbk.clear(kind=['stderr'], tag='hide_stderr')

# Removing entire cells
ntbk.remove_cells(tag='remove')
ntbk.remove_cells(tag='remove_if_empty', empty=True)
ntbk.remove_cells(search_text="# HIDDEN")

# Replacing text
text_replace_begin = '### SOLUTION BEGIN'
text_replace_end = '### SOLUTION END'
ntbk.replace_text(text_replace_begin, text_replace_end)

ntbk.save(path_notebook_cleaned)

Example

For an example, the following two notebooks show off nbclean's functionality:

Additionally, you can give it a try yourself by clicking the Binder button below:

Binder

Thanks

Thanks to jhamrick and the nbgrader project, which was the initial inspiration for this. You should check it out if you want a more fully-featured grading solution!