Open In App

Manipulating Jupyter Notebooks with the NBFormat Python Library

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The NBFormat Python library is a powerful tool, that is used to manipulate Jupyter Notebooks programmatically. It provides a set of functions that allow developers to create, read, modify, and validate Jupyter Notebook files (.ipynb) without manually doing it. We can use this library to automate notebook-related tasks, batch processing, and integrate notebook functionality into other applications.

In this article, we will understand the functionalities of the NBFormat Python library. We will start by understanding how to install this library, next we will understand functionalities like creating a new notebook, reading an existing one, editing a notebook cell, adding metadata, and validating a notebook. We will also implement all these functionalities. By the end of this article, we will be able to perform all these functionalities to manipulate a Jupyter notebook.

Installation

To install the NBFormat library, simply run the below command in our terminal:

pip install nbformat

Creating a New Jupyter Notebook

The first functionality of the NBFormat library is that we can create Jupyter Notebooks programmatically. Generative Pre-trained Transformers like ChatGPT and Gemini utilize this functionality of the `nbformat` library to create notebooks upon user requests in the backend. This allows us to generate notebooks without needing to open the Jupyter interface.

Now lets implement a code to create and save a new Jupyter notebook programmatically.

  • First, we will import nbformat library and create a new notebook object using nbformat`.v4.new_notebook()`.
  • Then, we will add a markdown cell to the notebook with the text "## This is a markdown cell" using the `new_markdown_cell()` function.
  • Next, we will add a code cell that contains a print statement, which outputs "GeeksForGeeks is one of the best coding platforms in India." using the `new_code_cell()` function.
  • Finally, we will save the notebook to a file named `example_notebook.ipynb` using a `with open` statement and the nbformat`.write()` function.
Python
import nbformat

nb = nbformat.v4.new_notebook()

nb['cells'].append(nbformat.v4.new_markdown_cell("## This is a markdown cell"))

nb['cells'].append(nbformat.v4.new_code_cell("print('GeeksForGeeks is one of the best coding platforms in India.')"))

with open('example_notebook.ipynb', 'w') as f:
    nbformat.write(nb, f)

After running this code, a new file named 'example_notebook.ipynb' will be saved in the current directory.

Screenshot-2024-09-04-115651
Create a Jupyter Notebook using NBFormat

Reading an Existing Jupyter Notebook

Now that we have created a notebook, let’s use the `nbformat` library to read and display the content of the notebook. GPT models use this functionality to read the notebooks users upload in order to process and respond to their requests.

Now, to implement this,

  • First, we will open and read the notebook file `example_notebook.ipynb` using the `nbformat.read()` function, specifying version 4 of the notebook format.
  • Then, we will iterate through the cells of the notebook using a `for` loop. For each cell, we will print the cell number and its content using the `source` attribute.

For clarity, we will print a separator line after each cell's content.

Python
with open('example_notebook.ipynb', 'r') as f:
    nb = nbformat.read(f, as_version=4)

for i, cell in enumerate(nb['cells']):
    print(f"Cell {i+1}:")
    print(cell['source'])
    print('-' * 40)

Output:

Screenshot-2024-09-04-115822
Reading jupytee Notebook in NBFormat

Modifying a Notebook Cell

The next main functionality that NBFormat library provides is the ability to edit and update the notebook. We can change the cell types from markdown to code and vice versa. we can use it to add and delete a specific cell from the notebook. GPTs use this functionality to update the notebooks based on users new request.

To implement this,

  • First, we will open and read the `example_notebook.ipynb` file using `nbformat.read()`, specifying version 4 of the notebook format.
  • We will then modify the content of the second cell (a code cell) by updating its source code to print "GeeksForGeeks is one of the best coding platforms in the world."
  • After making this change, we will save the modified notebook to a new file named `modified_notebook.ipynb` using the `nbformat.write()` function.

Finally, we will iterate through the cells of the modified notebook and print their contents, along with a separator line for clarity.

Python
with open('example_notebook.ipynb', 'r') as f:
    nb = nbformat.read(f, as_version=4)

nb['cells'][1]['source'] = "print('GeeksForGeeks is one of the best coding platforms in the world.')"

with open('modified_notebook.ipynb', 'w') as f:
    nbformat.write(nb, f)

for i, cell in enumerate(nb['cells']):
    print(f"Cell {i+1}:")
    print(cell['source'])
    print('-' * 40)

Output

Screenshot-2024-09-04-115939
Updating Jupyter Notebook using NBFormat

Adding Metadata to a Notebook

Adding metadata to a notebook helps us capture important details like the author, creation date, description, and custom tags. The `nbformat` library provides an easy way to incorporate this information into the notebook, making it more informative and organized.

In this example,

  • We will add custom metadata to a Jupyter notebook and then save the updated notebook to a new file.
  • First, we will add two metadata fields to the notebook: `author`, set to "Adil Naib," and `description`, set to "This is a GeeksForGeeks notebook."
  • These fields will be stored in the `metadata` attribute of the notebook object. After adding the metadata, we will save the updated notebook to a file named `notebook_with_metadata.ipynb` using the `nbformat.write()` function.
  • Finally, we will print the notebook's metadata using nb.metadata.
Python
nb.metadata['author'] = 'Adil Naib'
nb.metadata['description'] = 'This is a GeeksForGeeks notebook.'

with open('notebook_with_metadata.ipynb', 'w') as f:
    nbformat.write(nb, f)

print(nb.metadata)

Output:

Screenshot-2024-09-04-120156
Adding Metadata

Validating a Notebook

NBFormat has a validation feature that allows us to check if a notebook is in a expected structure and format. This is mainly useful when we are working with multiple notebooks and need to check the quality and structure of the notebook.

In this example, we will validate a Jupyter notebook using the `nbformat` library to ensure it conforms to the required structure.

To implement this functionality we will first open and read the `example_notebook.ipynb` file using `nbformat.read()`, specifying version 4 of the notebook format. We will then use the `validate()` function to check the validity of the notebook.

If the notebook is valid, we will print the message "Notebook is valid." If the notebook fails validation, a `NotebookValidationError` will be raised, and we will catch this exception to print an error message detailing the issue, indicating that the notebook is invalid.

Python
from nbformat import validate

with open('example_notebook.ipynb', 'r') as f:
    nb = nbformat.read(f, as_version=4)

try:
    validate(nb)
    print("The notebook is valid.")
except nbformat.validator.NotebookValidationError as e:
    print(f"The notebook is invalid: {e}")

Output:

Screenshot-2024-09-04-120251
Validating Notebook

Conclusion

The nbformat Python library is a robust and essential tool for programmatically manipulating Jupyter Notebooks. Throughout this article, we explored the various functionalities of the nbformat library, including creating new notebooks, reading existing ones, modifying notebook cells, adding metadata, and validating notebooks. Each of these features can significantly enhance the efficiency of working with Jupyter Notebooks, whether we're automating repetitive tasks, batch processing multiple notebooks, or integrating notebook functionality into larger applications.


Article Tags :
Practice Tags :

Similar Reads