/
03_convert_py_to_ipynb.py
41 lines (35 loc) · 1.22 KB
/
03_convert_py_to_ipynb.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
from IPython.nbformat.v4.nbbase import new_code_cell, new_notebook
import IPython.nbformat as nbf
import argparse
import codecs
import os
if not os.path.exists('files_generated'):
os.makedirs('files_generated')
ap = argparse.ArgumentParser()
ap.add_argument("-nb", "--ipynb", type=str, default="files_generated/assignment.ipynb",
help="name of exported python notebook")
ap.add_argument("-py", "--py_file", type=str, default="files_generated/extracted_python_file.py",
help="name of python file generated")
args = vars(ap.parse_args())
python_file = args["py_file"]
python_notebook_name = args["ipynb"]
with open(python_file) as f:
data = f.read().split('\n')
# Convert python file to python notebook
def write_to_python_notebook():
cells = []
for index, text in enumerate(data):
cells.append(new_code_cell(
source=text,
execution_count=index,
))
nb0 = new_notebook(cells=cells,
metadata={
'language': 'python',
})
f = codecs.open(python_notebook_name, encoding='utf-8', mode='w')
nbf.write(nb0, f, 4)
f.close()
print(f"[INFO] Writing extracted text to notebook: {python_notebook_name}")
write_to_python_notebook()
print("Done!")