-
-
Save rlugojr/2c08202e08b9856e9fe42e9b79bed23e to your computer and use it in GitHub Desktop.
Merge/concatenate multiple IPython notebooks into one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Note, updated version of | |
# https://github.com/ipython/ipython-in-depth/blob/master/tools/nbmerge.py | |
""" | |
usage: | |
python nbmerge.py A.ipynb B.ipynb C.ipynb > merged.ipynb | |
""" | |
import io | |
import os | |
import sys | |
from IPython import nbformat | |
def merge_notebooks(filenames): | |
merged = None | |
for fname in filenames: | |
with io.open(fname, 'r', encoding='utf-8') as f: | |
nb = nbformat.read(f, as_version=4) | |
if merged is None: | |
merged = nb | |
else: | |
# TODO: add an optional marker between joined notebooks | |
# like an horizontal rule, for example, or some other arbitrary | |
# (user specified) markdown cell) | |
merged.cells.extend(nb.cells) | |
if not hasattr(merged.metadata, 'name'): | |
merged.metadata.name = '' | |
merged.metadata.name += "_merged" | |
print(nbformat.writes(merged)) | |
if __name__ == '__main__': | |
notebooks = sys.argv[1:] | |
if not notebooks: | |
print(__doc__, file=sys.stderr) | |
sys.exit(1) | |
merge_notebooks(notebooks) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment