Skip to main content

A tool to merge / concatenate Jupyter (IPython) notebooks

Project description

https://travis-ci.org/jbn/nbmerge.svg?branch=master https://ci.appveyor.com/api/projects/status/69kj3prrrieyp8q2/branch/master?svg=true https://coveralls.io/repos/github/jbn/nbmerge/badge.svg?branch=master https://img.shields.io/pypi/dm/nbmerge.svg https://img.shields.io/pypi/v/nbmerge.svg https://img.shields.io/badge/license-MIT-blue.svg https://img.shields.io/pypi/pyversions/nbmerge.svg

nbmerge: merge / concatenate Jupyter notebooks

Installation

pip install nbmerge

Usage

For the usage as originally specified by @fperez’s gist,

nbmerge file_1.ipynb file_2.ipynb file_3.ipynb > merged.ipynb

Alternatively, nbmerge can cursively collect all files in the current directory and below, recursively. After collection, it sorts them lexicographically. You can use a regular expression as a file name predicate. All .ipynb_checkpoints are automatically ignored. And, you can use the -i option to ignore any notebook prefixed with an underscore (think pseudo-private in python).

For example, the following command collects all notebooks in your project that have the word intro in the file name and saves it to a merged file named _merged.ipynb,

nbmerge --recursive -i -p ".*intro.*" -o _merged.ipynb

Finally, you can also instruct the script to demarcate the boundary between each original file with the -b / -boundary [BOUNDARY] flag. The src_nb value in the metadata for the first cell in each original notebook will then contain the path of the original notebook, relative to the cwd at the point of script execution.

Lineage

@fperez wrote an nbmerge.py script which “Merge[s]/concatenate[s] multiple IPython notebooks into one.” I use it a lot. Evidently, other people do, too. In early 2016, he opened an issue to add the script as an nbconvert tool, but nothing came of it. However, he and @Carreau came up with good (i.e. unsurprising) semantics for metadata merging and notebook naming:

metadata = {}
for n in reversed(notebooks):
    metadata.update(n.metadata)

I don’t think it’s possible to implement the merger as a preprocessor. Preprocessors are stateless, so you can’t implement a reduce operation. Instead, I wrote (er, packaged up) this library as an nbstripoutput-like package . It fits in a Makefile script just fine.

Right now, only the basic (originally fperez’s) functionality is implemented. However, I’m going to follow kynan’s lead and slowly pull in functionality similar to his nbstripout package.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbmerge-0.0.4.tar.gz (7.6 kB view details)

Uploaded Source

File details

Details for the file nbmerge-0.0.4.tar.gz.

File metadata

  • Download URL: nbmerge-0.0.4.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nbmerge-0.0.4.tar.gz
Algorithm Hash digest
SHA256 f432797ee685ed495a2daa4dbb76e2b52028d2c3a38142b523bb5cb832df0882
MD5 9c8928046d7d938398eff76782f7ce84
BLAKE2b-256 73a956a02f2a3405397af9924e1d372b2e546ebcdb312d800f3945b321a328a5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page