Package: nbmanips
nbmanipsA collections of utilities to manipulate IPython/Jupyter Notebooks via a python script.I - Usage/Examples1 - Basic usageA simple example of using nbmanips:fromnbmanipsimportNotebook# Read ipynb filenb=Notebook.read_ipynb("my_notebook.ipynb")# delete empty cellsnb.select("empty").delete()# save ipynb filenb.to_ipynb("new_notebook.ipynb")Examples of operations you can perform on a Notebook:replace: Replace matching text in the selected cellstag: Add metadata to the selected cellserase: Erase the content of the selected cellsdelete: Delete the selected cellskeep: Kepp the selected cells2 - SelectorsTo select cells on which to apply the previous operations, you can use:The cell numbernb[0].show()# ORnb.select(0).show()A slice objectnb[1:6:2].show()# ORselected_cells=slice(1,6,2)nb.select(selected_cells).show()A predefined selector. Available predefined selectors are the following:code_cells/markdown_cells/raw_cells: Selects cells with the given typecontains: Selects Cells containing a certain text.is_empty/empty: Selects empty cellshas_output: Checks if the cell has any outputhas_output_type: Select cells that have a given output_typehas_slide_type: Select cells that have a given slide typeis_new_slide: Selects cells where a new slide/subslide startshas_byte_size: Selects cells with byte size within a given range of values.# Show Markdown Cellsnb.select('markdown_cells').show()# Show Cells containing the equal signnb.select('contains','=').show()A function that takes a Cell object and returns True if the cell should be selected# Show Cells with length > 10nb.select(lambdacell:len(cell.source)>10).show()A list of Selectors# Show Empty Markdown Cellsnb.select(['markdown_cells','is_empty']).show()# Show Markdown or Code Cellsnb.select(['markdown_cells','code_cells'],type='or').show()3 - Export FormatsYou can export the notebooks to these formats:to_ipynbto_dbcto_htmlto_slides (using reveal.js)to_md (to markdown)to_py (to python)to_text (textual representation of the notebook)4 - Slide manipulationsYou can manipulate the slides by tagging which cells to keep and which to skip.
The following actions are available:set_slideset_subslideset_skipset_fragmentset_notesA neat trick is to useauto_slidemethod to automatically create slides out of your notebook:fromnbmanipsimportNotebook# Read ipynb filenb=Notebook.read_ipynb("my_notebook.ipynb")# Automatically create slidesnb.auto_slide()# Export to Reveal.js slides (HTML)nb.to_slides("new_slides.slides.html",theme='beige')II - CLI1 - Show a notebookTo get a readable representation of the notebooknbshowmy_notebook.ipynbOther options are available. For example, you can customize the style, weather to truncate the output of cells:nbshow-sdouble-t100my_notebook.ipynbTo show a subset of the notebook cells, you can perform a select operation:nbselect0:3|nbshowmy_notebook.ipynb# Or if you're using negative indexes ( to show the last 3 cells )nbselect[-3:]|nbshowmy_notebook.ipynb2 - Basic usageA simple example of using nbmanips via the cli:# delete empty cellsnbselectempty|nbdeletemy_notebook.ipynb--outputnew_notebook.ipynb# Or equivalently:nbmanipsselectempty|nbmanipsdeletemy_notebook.ipynb--outputnew_notebook.ipynbYou could also show the table of contents of a certain notebook:nbtocnb.ipynbOr split a notebook into multiple notebooks:nbsplitnb.ipynb5,93 - Export FormatsYou can convert a notebook to the following formats:html:nb convert html my_notebook.ipynb --output my_notebook.htmlslides (using reveal.js):nb convert slides my_notebook.ipynb --output my_notebook.slides.htmlmd (to markdown):nb convert md my_notebook.ipynb --output my_notebook.mdpy (to python):nb convert py my_notebook.ipynb --output my_notebook.py4 - Slide manipulations# Automatically set slidesnbauto-slide-fmy_notebook.ipynb# Generate a my_notebook.slides.html filenbconvertslidesmy_notebook.ipynbOr if you do not wish to modify your original notebook:# Automatically set slidesnbauto-slidemy_notebook.ipynb-omy_temp_notebook.ipynb# Generate a my_notebook.slides.html filenbconvertslidesmy_temp_notebook.ipynb-omy_notebook.slides.htmlIf you need more details you can check the --help option:nbmanips --helpIII - Optional RequirementsThere are optional requirements you may want to install to render images in the terminal.
The results, however, are not always convincing.
If you want to enable this feature, you can just run the following command:pipinstallnbmanips[images]RoadmapAdd Custom Templates