-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathto.py
30 lines (21 loc) · 964 Bytes
/
to.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
# Usage: to.py notebook_name.ipynb new_python_file_name.py
import re
import argparse
import nbconvert
parser = argparse.ArgumentParser()
# first positional argument is the path to the notebook
parser.add_argument('notebook', type=str, help='path to the notebook', default='main-train.ipynb')
# second positional argument is the path to the output file
parser.add_argument('output', type=str, help='path to the output file', default='main-train.py')
# args = ['notebook_name.ipynb', 'new_python_file_name.py']
args = parser.parse_args()
# convert notebook to python script string
file = nbconvert.PythonExporter().from_filename(args.notebook)[0]
# replace '# In[ ]:\n\n' with '# %%'
file = re.sub('# In\[([\d]+| )\]:\n\n', '# %%', file)
# removed the three lines at the top of the file given by
#!/usr/bin/env python
# coding: utf-8
file = file.replace('#!/usr/bin/env python\n# coding: utf-8\n\n', '')
with open(args.output, 'w') as f:
f.write(file)