The article introduces a Python package called "nbmanips" that allows users to explore, handle, and transform Jupyter notebooks directly from the terminal.
Abstract
The article titled "Rapidly Explore Jupyter Notebooks (Right in Your Terminal)" introduces a Python package called "nbmanips" that simplifies the process of exploring and manipulating Jupyter notebooks. The authors, Amal Hasni and Dhia Hmila, explain that the package enables users to display notebook content in the terminal, even with images, and provides various filtering and selecting options for notebook cells. The package can be installed using pip and includes a CLI for easy use. The authors also mention that they plan to release more articles on how to split and merge different notebooks using "nbmanips".
Opinions
The authors believe that Jupyter notebooks are useful for interactive data exploration and trying out new ideas but can leave users with messy code.
They suggest that "nbmanips" can help users quickly explore and restructure code in Jupyter notebooks.
The authors encourage users to try out the package and leave questions in the responses section.
They plan to release more articles on how to use "nbmanips" to split and merge different notebooks.
The authors provide a GitHub repository with all the Python scripts gathered in one place.
They thank readers for their attention and invite them to stay safe and look forward to their next article.
The article includes links to other articles on related topics, such as merging multiple Jupyter notebooks into one and writing cleaner code.
Rapidly Explore Jupyter Notebooks (Right in Your Terminal)
Installing nbmanips is very simple if you use pip:
pip install nbmanips
Through this package, it is possible to display your Jupyter notebook content on your terminal even if it contains images. Yes, you read that right! It will render the contained images if you install the following optional requirement:
pip install nbmanips[images]
You can test the following library with your own notebook files if you want. But in case you need test notebooks, here’s a great Git repository with over 30 Machine Learning related notebooks.
2 - nbmanips in Python
2.1 - Reading a notebook
Reading a Jupyter notebook is pretty straightforward :
Once, you’ve read the notebook you can print it to the screen with the show method:
nb.show()
Different styles are available for you for the display:
💡 Hint: the copy style is ideal to copy/paste code from the terminal 😉
📓 Other interesting parameters are:
width : width of the cells
exclude_output : True if you want to hide the cells' outputs.
2.2 - Filtering/Selecting Notebook Cells
Now we have a way to visualize our notebooks in a readable way without leaving our terminal
Jupyter notebooks, however, can have a huge amount of cells and we don’t necessarily want to see all of them on screen.
This is where Selectors come into play. They are used to specify on which cells we will apply a given action (like show or delete). In our example, the action is simply to display the cell.
To select cells on which to apply the previous operations, you can use:
1. Filter using indexing
2. Filter using a predefined selector
The available predefined selectors in nbmanips are the following:
code_cells / markdown_cells / raw_cells : Select cells with the given type
contains : Selects Cells containing a certain text/keyword
is_empty / empty : Selects empty cells
has_output : Checks if the cell has any output
has_output_type : Selects cells that have a given output_type
has_byte_size : Selects cells with byte size within a given range of values. This is especially useful to filter out cells containing large images.
has_html_tag : Selects markdown cells that have a certain HTML tag
has_tag : Selects cells that have a certain cell tag. For those who didn’t know it, it is possible to add a tag to Jupyter Notebook cell:
Screenshot by Authors
has_slide_type: Selects cells that have a given slide type
is_new_slide: Selects cells where a new slide/sub-slide starts:
Screenshot by Authors
3. Filter using a user-defined function
The function takes a Cell object and returns True if the cell should be selected:
4. Combining filters
The first way to combine selectors is simply by using a list.
Now that we know how to display specific cells in each notebook, let’s apply that to all notebooks in a given folder.
3 - nbmanips in the Terminal
nbmanips also comes with a CLI and therefore all that we have seen previously can be done in a few lines.
For example, to preview a notebook, you can run the show subcommand:
nb show nb.ipynb
If you want to see the available parameters, you can pass the --help option:
You can use filters, by piping them in this way:
You can also use the --help option to get more information:
Final Thoughts
Jupyter notebooks are very helpful for interactively exploring data or quickly trying out new ideas. However, once you are done with them, you are often left with a quick and dirty code.
nbmanips tries to offer a way to quickly explore the code, but also to easily restructure the cells and transform the notebook.
Stay tuned for our next article to see how to split and merge different Notebooks.
You can find all the Python scripts gathered in one place in this GitHub repository. If you have questions, please don’t hesitate to leave them in the responses section and we’ll be more than happy to answer.
Thank you for sticking around this far, stay safe and we will see you in our next article! 😊