CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
"""
2
usage:
3
4
python nbmerge.py A.ipynb B.ipynb C.ipynb > merged.ipynb
5
"""
6
7
import io
8
import os
9
import sys
10
11
from IPython.nbformat import current
12
13
def merge_notebooks(filenames):
14
merged = None
15
for fname in filenames:
16
with io.open(fname, 'r', encoding='utf-8') as f:
17
nb = current.read(f, 'json')
18
if merged is None:
19
merged = nb
20
else:
21
merged.worksheets[0].cells.extend(nb.worksheets[0].cells)
22
merged.metadata.name += "_merged"
23
print current.writes(merged, 'json')
24
25
if __name__ == '__main__':
26
merge_notebooks(sys.argv[1:])
27
28