-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathexport.py
More file actions
49 lines (41 loc) · 1.58 KB
/
export.py
File metadata and controls
49 lines (41 loc) · 1.58 KB
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
# To export the ipynb to text files (for submission)
import json, os
#Copied from https://stackoverflow.com/questions/37797709/convert-json-ipython-notebook-ipynb-to-py-file
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-input", "-i", type = str, help="Input file/folder", default = '.')
parser.add_argument("--noCode", help="Do not write the code",
action="store_true")
parser.add_argument("-output", "-o", type = str, help="Output", default = 'ExportedNB')
args = parser.parse_args()
writeCode = not args.noCode
writeMarkdown = True
writeAllMarkdown = True
files = []
if not os.path.exists(args.input):
print("Input file/folder does not exist")
exit()
elif os.path.isfile(args.input):
files.append(args.input)
else:
for file in os.listdir(args.input):
if not file.endswith('.ipynb'):
continue
files.append(os.path.join(args.input, file))
if not os.path.exists(args.output):
os.makedirs(args.output)
for file in files:
code = json.load(open(file))
py_file = open(f"{args.output}/{file.replace('ipynb', 'txt')}", "w+")
for cell in code['cells']:
if cell['cell_type'] == 'code' and writeCode:
for line in cell['source']:
py_file.write(line)
py_file.write("\n")
elif cell['cell_type'] == 'markdown' and writeMarkdown:
py_file.write("\n")
for line in cell['source']:
if line and line[0] == "#" or writeAllMarkdown:
py_file.write(line)
py_file.write("\n")
py_file.close()