Alexander Ivanov
Alexander Ivanov

Reputation: 325

How to delete all markdown cells in jupyter notebook?

I want to delete all markdown cells from a jupyter notebook. The only possible way to do this, as I see, to download it as .py file, then copy and paste in a new jupyter notebook. Is there a way to do this without breaking cell structure?

Upvotes: 0

Views: 1186

Answers (1)

Wayne
Wayne

Reputation: 9810

nbformat can be used to do that:

import nbformat as nbf
ntbk = nbf.read("old_notebook.ipynb", nbf.NO_CONVERT)
new_ntbk = ntbk
new_ntbk.cells = [cell for cell in ntbk.cells if cell.cell_type != "markdown"]
nbf.write(new_ntbk, "no_markdown_notebook.ipynb", version=nbf.NO_CONVERT)

For less 'broad-strokes' notebook cleaning, Chris Holdgraf's nbclean is my go-to tool for this type of thing. Also, see here.

(This answer was adapted from code to do the opposite process: Delete all code cells except markdown text).

Upvotes: 1

Related Questions