-
Notifications
You must be signed in to change notification settings - Fork 1
/
nb2md.py
160 lines (123 loc) · 4.77 KB
/
nb2md.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
将某个ipynb文件转换为markdown文件
"""
import os
import re
import json
import base64
class Notebook:
def __init__(self, notebook_path, markdown_dir=".", image_dir_name="images"):
# 读取.ipynb文件数据
self.cells = list()
with open(notebook_path, 'r', encoding='utf8') as f:
notebook_dict = json.load(f)
for cell_dict in notebook_dict.get('cells'):
self.cells.append(Cell(self, cell_dict))
self.notebook_path = notebook_path
self.notebook_dict = notebook_dict
# self.markdown_name = os.path.split(self.notebook_path)[-1].split('.')[0]
self.markdown_name = os.path.splitext(self.notebook_path)[0]
self.markdown_dir = markdown_dir
self.markdown_path = os.path.join(self.markdown_dir, self.markdown_name + ".md")
self.img_save_dir = os.path.join(self.markdown_dir, image_dir_name)
self.img_num = 1
def save_img(self, image):
# 创建图片文件夹
if not os.path.isdir(self.img_save_dir):
os.makedirs(self.img_save_dir)
# 图片编码为二进制
img_data = base64.b64decode(image)
# img_name == "demoname_plot_1.png"
img_name = f"{self.markdown_name}_plot_{self.img_num}.png"
img_path = os.path.join(self.img_save_dir, img_name)
with open(img_path, 'wb') as f:
f.write(img_data)
self.img_num += 1
# img_name = os.path.split(img_path)[-1]
return f"![](./images/{img_name})\n"
def text(self):
res = ""
for cell in self.cells:
res += cell.text()
return res
def to_markdown(self, save=False):
text = self.text()
if save:
if not os.path.isdir(self.markdown_dir):
os.makedirs(self.markdown_dir)
with open(self.markdown_path, 'w', encoding='utf8') as f:
f.write(text)
return text
class Cell(dict):
def __init__(self, notebook, cell_dict):
super().__init__()
self.update(cell_dict)
self.notebook = notebook
# self.cell_type = cell_dict['cell_type']
# self.source = cell_dict['source']
# self.outputs = cell_dict.get('outputs')
def __getattr__(self, key):
return self[key]
def __setattr__(self, key, value):
self.__dict__[key] = value
# def __repr__(self):
# return f"{self.idx} cell_type: {self.cell_type}"
def markdown_text(self):
return "".join(self.source) + '\n'
def code_text(self, text, code_type='python'):
if text:
return f"```{code_type}\n{text}```\n"
return ""
# text = "".join(self.source)
# return f"```{code_type}\n{text}\n```\n"
def outputs_text(self):
text = ""
imgs = list()
for output in self.outputs:
# print的输出
if output['output_type'] == 'stream':
text = text + "".join(output['text'])
continue
# 错误的输出
if output['output_type'] == "error":
tmp = "\n".join(output['traceback'])
tmp = re.sub(r'\x1b.*?m', '', tmp)
text = text + tmp + '\n'
# text += f"```\n{tmp}\n```\n"
continue
output_data = output['data']
# 图片类型的输出
image = output_data.get('image/png')
if image:
# 保存图片到特定文件夹
# img_data = base64.b64decode(image)
markdown_img = self.notebook.save_img(image)
imgs.append(markdown_img)
continue
# 文本类型的输出
text_plain = output_data.get('text/plain')
if text_plain:
text = text + "".join(text_plain) + "\n"
text = self.code_text(text, code_type='')
text += "".join(imgs)
return text
def text(self):
if self.cell_type == 'markdown':
return self.markdown_text()
if self.cell_type == "code":
c_text = "".join(self.source) + "\n"
res = self.code_text(c_text)
res += self.outputs_text()
return res
"""
# 单文件转换
notebook = Notebook('demo.ipynb', '/home/study/markdowns')
res = notebook.to_markdown(save=True)
"""
# 将当前文件夹下的ipynb文件转换为md文件
if __name__ == "__main__":
for file_name in os.listdir():
if file_name.split('.')[-1] != 'ipynb':
continue
notebook = Notebook(file_name)
notebook.to_markdown(save=True)