Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872from __future__ import print_function1import io2import IPython.nbformat as nbformat3import sys456def remove_formatting(nb):7c = nb['cells']8for i in range (len(c)):9if 'source' in c[i].keys():10if c[i]['source'][0:16] == '#format the book':11del c[i]12return131415def remove_links(nb):16c = nb['cells']17for i in range (len(c)):18if 'source' in c[i].keys():19if c[i]['source'][0:19] == '[Table of Contents]':20del c[i]21return222324def remove_links_add_appendix(nb):25c = nb['cells']26for i in range (len(c)):27if 'source' in c[i].keys():28if c[i]['source'][0:19] == '[Table of Contents]':29c[i]['source'] = '\\appendix'30return313233def merge_notebooks(filenames):34merged = None35added_appendix = False36for fname in filenames:37with io.open(fname, 'r', encoding='utf-8') as f:38nb = nbformat.read(f, nbformat.NO_CONVERT)39remove_formatting(nb)40if not added_appendix and fname[0:8] == 'Appendix':41remove_links_add_appendix(nb)42added_appendix = True43else:44remove_links(nb)45if merged is None:46merged = nb47else:48merged.cells.extend(nb.cells)49#merged.metadata.name += "_merged"5051print(nbformat.writes(merged, nbformat.NO_CONVERT))525354if __name__ == '__main__':55#merge_notebooks(sys.argv[1:])56merge_notebooks(57['../00_Preface.ipynb',58'../01_g-h_filter.ipynb',59'../Appendix_A_Installation.ipynb'])606162