我的 Jupyter 笔记本越来越长,这使得导航变得困难。
我想将每一章(以标题 1 开头的 Cel)保存到不同的文件中。我怎样才能做到这一点?在笔记本之间剪切和粘贴多个单元格似乎是不可能的。
我的 Jupyter 笔记本越来越长,这使得导航变得困难。
我想将每一章(以标题 1 开头的 Cel)保存到不同的文件中。我怎样才能做到这一点?在笔记本之间剪切和粘贴多个单元格似乎是不可能的。
这是我使用的方法 - 它有点尴尬,但它有效:
我相信开发人员可能正在为未来的版本开发更好的解决方案。
笔记本文件是 json 格式,所以我将所有数据都以 JSON 格式获取并自动将其拆分为多个文件。
这段代码是我做的。
代码似乎很复杂,但如果你检查一下它就很简单,这是一个单独文件的示例,http: //www.fun-coding.org/DS&AL4-1.html 我也对其进行了转换拆分后作为HTML。
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)
这是一个检查笔记本文件中章节编号的功能。我在markdown单元格中使用'## 1. chapter name'将章节号添加到笔记本文件中,所以只需检查##数字。带有正则表达式的模式。
然后,接下来的代码是将单元格的数据复制到这一章编号中,并将仅复制的单元格和其他(元数据、nbformat 和 nbformat_minor)保存到单独的文件中。
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)
最简单的方法可能是在文本编辑器中编辑 .ipnb 文件。下面我列出了一个非常简单的笔记本的内容。
笔记本看起来像
第1章
[1]中:1+1
输出[1]:2
第2章
[2]中:2+2
输出[2]:4
要取出第 1 章并将其放在第 2 章后面,您可以这样做
您可以以类似的方式操作多个笔记本。
这是示例的 .ipnb 文件
{
"metadata": {
"name": "",
"signature": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 1"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"1+1"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 1,
"text": [
"2"
]
}
],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Chapter 2"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"2+2"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 2,
"text": [
"4"
]
}
],
"prompt_number": 2
}
],
"metadata": {}
}
]
}