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
from __future__ import print_function
2
import io
3
import IPython.nbformat as nbformat
4
import sys
5
6
7
def remove_formatting(nb):
8
c = nb['cells']
9
for i in range (len(c)):
10
if 'source' in c[i].keys():
11
if c[i]['source'][0:16] == '#format the book':
12
del c[i]
13
return
14
15
16
def remove_links(nb):
17
c = nb['cells']
18
for i in range (len(c)):
19
if 'source' in c[i].keys():
20
if c[i]['source'][0:19] == '[Table of Contents]':
21
del c[i]
22
return
23
24
25
def remove_links_add_appendix(nb):
26
c = nb['cells']
27
for i in range (len(c)):
28
if 'source' in c[i].keys():
29
if c[i]['source'][0:19] == '[Table of Contents]':
30
c[i]['source'] = '\\appendix'
31
return
32
33
34
def merge_notebooks(filenames):
35
merged = None
36
added_appendix = False
37
for fname in filenames:
38
with io.open(fname, 'r', encoding='utf-8') as f:
39
nb = nbformat.read(f, nbformat.NO_CONVERT)
40
remove_formatting(nb)
41
if not added_appendix and fname[0:8] == 'Appendix':
42
remove_links_add_appendix(nb)
43
added_appendix = True
44
else:
45
remove_links(nb)
46
if merged is None:
47
merged = nb
48
else:
49
merged.cells.extend(nb.cells)
50
#merged.metadata.name += "_merged"
51
52
print(nbformat.writes(merged, nbformat.NO_CONVERT))
53
54
55
if __name__ == '__main__':
56
#merge_notebooks(sys.argv[1:])
57
merge_notebooks(
58
['../00_Preface.ipynb',
59
'../01_g-h_filter.ipynb',
60
'../Appendix_A_Installation.ipynb'])
61
62