What is @jupyterlab/nbformat?
@jupyterlab/nbformat is an npm package that provides utilities for working with Jupyter Notebook files. It allows you to read, write, and manipulate notebook files in JSON format, making it easier to integrate Jupyter Notebooks into JavaScript/TypeScript applications.
What are @jupyterlab/nbformat's main functionalities?
Reading a Notebook
This feature allows you to read a Jupyter Notebook file from the filesystem and parse its JSON content. The code sample demonstrates how to read a notebook file named 'example.ipynb' and parse its content into a JavaScript object.
const nbformat = require('@jupyterlab/nbformat');
const fs = require('fs');
const notebookContent = fs.readFileSync('example.ipynb', 'utf8');
const notebook = JSON.parse(notebookContent);
console.log(notebook);
Writing a Notebook
This feature allows you to create a new Jupyter Notebook file and write it to the filesystem. The code sample demonstrates how to create a basic notebook object and write it to a file named 'new_notebook.ipynb'.
const nbformat = require('@jupyterlab/nbformat');
const fs = require('fs');
const notebook = {
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
};
const notebookContent = JSON.stringify(notebook, null, 2);
fs.writeFileSync('new_notebook.ipynb', notebookContent);
Manipulating Notebook Cells
This feature allows you to manipulate the cells within a Jupyter Notebook. The code sample demonstrates how to add a new code cell to an existing notebook and save the updated notebook back to the filesystem.
const nbformat = require('@jupyterlab/nbformat');
const fs = require('fs');
const notebookContent = fs.readFileSync('example.ipynb', 'utf8');
const notebook = JSON.parse(notebookContent);
// Add a new code cell
const newCell = {
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": ["print('Hello, world!')"]
};
notebook.cells.push(newCell);
const updatedContent = JSON.stringify(notebook, null, 2);
fs.writeFileSync('example.ipynb', updatedContent);
Other packages similar to @jupyterlab/nbformat
nbformat
The 'nbformat' package is a Python library for reading and writing Jupyter Notebook files. It provides similar functionalities to @jupyterlab/nbformat but is intended for use in Python environments. It is part of the Jupyter project and is widely used in the Jupyter ecosystem.
notebookjs
The 'notebookjs' package is a JavaScript library for converting Jupyter Notebooks to HTML. While it focuses on rendering notebooks as HTML, it also provides utilities for reading and parsing notebook files. It is useful for web applications that need to display notebook content.
nbconvert
The 'nbconvert' package is a Python library for converting Jupyter Notebooks to various formats, including HTML, PDF, and Markdown. It provides more advanced conversion capabilities compared to @jupyterlab/nbformat, but it is primarily intended for use in Python environments.