You can install nbclean
with pip:
pip install nbclean
You can use nbclean
to "clean up" Jupyter notebooks, including:
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)
For an example, the following two notebooks show off nbclean's functionality:
nbclean
syntax.Additionally, you can give it a try yourself by clicking the Binder button below:
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!