Merge multiple Jupyter Notebooks together with Python | by Satyam Kumar | Towards Data Science

Jupyter Notebook is considered an integral part of any Data Scientist’s toolbox. Jupyter Notebook also referred to as IPython Notebooks is a web application based on the server-client structure.
Jupyter Notebook provides an easy-to-use interactive data science environment that supports various programming languages. It is of great use for prototyping and offers interactive computing by combining code, text, and visualizations.
It is common to use multiple Jupyter Notebooks for any project development. Sometimes requirement arises to merge several notebooks together and you need to manually do copy-pasting cells of the notebook together, which becomes a tedious task.
In this article, you can read how to merge several IPython Notebooks together and efficiently, writing few lines of Python code.
A Jupyter Notebook is saved as a JSON file format. This JSON file has all the notebook contents, including text, code, outputs, and plots. The figures, images, and videos are encoded as base64 strings. The basic structure of the Jupyter Notebook JSON file is:
{ "cells": [], "metadata": {}, "nbformat": 4, "nbformat_minor": 4}
metadata: A dictionary that contains information about the kernel and language used.
nbformat, nbformat_minor: Notebook format versions (4.0).
cells: Cell is the most important component of JSON file, that contains information about each cell. Each cell is represented by a dictionary having the following components:
{"cell_type": "code", // can be code, markdown or raw"execution_count": null, // null or an integer"metadata": {}, // metadata of the cell: tags,editable,collapsed"outputs": [], // result of code or rendered markdown"source": [] // the input code or source markdown}
The basic idea to merge several Jupyter Notebooks together is to manipulate the contents of the notebook and merge the JSON file together to create one single notebook.
nbconvert is a Python tool that gives researchers the flexibility to deliver information in a timely way across different formats. It can be used to merge the contents of several JSON files. Let’s start with the merging of the notebook using the following steps.
Import nbconvert library: import nbconvert
Read all the IPython Notebooks using .read() function of nbconvert, which reads the notebook in JSON format.
# Reading the notebooksfirst_notebook = nbformat.read(file1, 4)second_notebook = nbformat.read(file2, 4)third_notebook = nbformat.read(file3, 4)
Create a new notebook that will have the merged contents of the above three notebooks. The metadata of the new notebook will be the same as that of the earlier one.
# Creating a new notebookfinal_notebook = nbformat.v4.new_notebook(metadata=first_notebook.metadata)
Concat all the values for the cell key in each of the JSON files. + keyword can be used to merge the dictionary values across the cell key of three IPython Notebooks.
# Concatenating the notebooksfinal_notebook.cells = first_notebook.cells + second_notebook.cells + second_notebook.cells
Finally, save the newly created IPyton Notebook using the .write function from nbconvert library.
# Saving the new notebook nbformat.write(final_notebook, 'final_notebook.ipynb')
The new file having the contents of each of the notebooks is saved to the desired location in few lines of Python code.
In this article, we have discussed how to combine multiple IPython Notebook files into a single notebook using the nbconvert tool. Depending on one’s requirement, they can make changes to the script and merge the notebooks in an easy and efficient manner.
nbconvert can be used to convert notebooks to other static formats including HTML, LaTeX, etc. It is an interesting package and one can go through the documentation page to get an understanding.
[1] NbConvert Documentation: https://nbconvert.readthedocs.io/en/latest/
Thank You for Reading