-
Notifications
You must be signed in to change notification settings - Fork 106
/
nbmerge.py
64 lines (57 loc) · 2.02 KB
/
nbmerge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# From https://gist.github.com/fperez/e2bbc0a208e82e450f69
# 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
"""
from __future__ import print_function
import io
import os
import string
import sys
import nbformat
def fix_images_paths(cells, filename):
# find parent path
path_filename = filename.split('/')
full_path = '/'.join(path_filename[:-1]) + "/"
# fix paths
for cell in cells:
if ("![" in cell['source'] and ".svg)" in cell['source']):
source = cell['source']
new_source = source
# where the link starts
start = source.find("](")
if source[start+2:start+4] == './':
new_source = source[:start+2] + full_path + source[start+4:]
elif source[start+2] in string.ascii_letters:
new_source = source[:start+2] + full_path + source[start+2:]
cell['source'] = new_source
return cells
def merge_notebooks(filenames, outfile):
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:
nb.cells = fix_images_paths(nb.cells, fname)
# 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"
result=nbformat.writes(merged)
with io.open(outfile, 'w', encoding='utf-8') as out:
out.write(result)
if __name__ == '__main__':
notebooks = sys.argv[1:-1]
outfile = sys.argv[-1]
if not notebooks:
print(__doc__, file=sys.stderr)
sys.exit(1)
merge_notebooks(notebooks, outfile)