My Jupyter notebooks is getting long, which makes it difficult to navigate.
I want to save each chapter (Cel starting with Heading 1) to a different file. How can I do that? Cut and paste of multiple cells between notebooks seems not possible.
Enter command mode ( Esc ), use Shift + s to toggle the current cell to either a split cell or full width.
Enter command mode (esc), use shift-s to toggle the current cell to either a split cell or full width.
This is the method I use - it is a little awkward, but it works:
I believe that the developers may be working on a better solution for a future release.
A notebook file is json format, so I get all data as JSON format and split it into several files automatically.
This code is what I made.
The code seems to be complex, but it is simple if you just check it for a while and this is an example of a separate file, http://www.fun-coding.org/DS&AL4-1.html which I also transformed as HTML after I split it.
import json
from pprint import pprint
import re
def notebook_spliter(FILENAME, chapter_num):
with open(FILENAME + '.ipynb') as data_file:
data = json.load(data_file)
copy_cell, chapter_in = list(), False
regx = re.compile("## [0-9]+\. ")
for num in range(len(data['cells'])):
if chapter_in and data['cells'][num]['cell_type'] != 'markdown':
copy_cell.append(data['cells'][num])
elif data['cells'][num]['cell_type'] == 'markdown':
regx_result = regx.match(data['cells'][num]['source'][0])
if regx_result:
print (regx_result.group())
regx2 = re.compile("[0-9]+")
regx2_result = regx2.search(regx_result.group())
if regx2_result:
print (int(regx2_result.group()))
if chapter_in == False:
if chapter_num == int(regx2_result.group()):
chapter_in = True
copy_cell.append(data['cells'][num])
else:
if chapter_num != int(regx2_result.group()):
break
elif chapter_in:
copy_cell.append(data['cells'][num])
copy_data["cells"] = copy_cell
copy_data["metadata"] = data["metadata"]
copy_data["nbformat"] = data["nbformat"]
copy_data["nbformat_minor"] = data["nbformat_minor"]
with open(FILENAME + '-' + str(chapter_num) + '.ipynb', 'w') as fd:
json.dump(copy_data, fd, ensure_ascii=False)
This is a function to check chapter numbers in a notebook file. I added chapter number to the notebook file with '## 1. chapter name' in markdown cell, so just check ## digit. pattern with regular expression.
Then, next code is to copy data of cells into this chapter number, and save the only copied cells and others(metadata, nbformat, and nbformat_minor) to separate file.
copy_data = dict()
FILENAME = 'DS&AL1'
CHAPTERS = list()
with open(FILENAME + '.ipynb') as data_file:
data = json.load(data_file)
for num in range(len(data['cells'])):
if data['cells'][num]['cell_type'] == 'markdown':
regx_result = regx.match(data['cells'][num]['source'][0])
if regx_result:
regx2 = re.compile("[0-9]+")
regx2_result = regx2.search(regx_result.group())
if regx2_result:
CHAPTERS.append(int(regx2_result.group()))
print (CHAPTERS)
for chapternum in CHAPTERS:
notebook_spliter(FILENAME, chapternum)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With