-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertZepNotebook2py.py
More file actions
60 lines (53 loc) · 2.38 KB
/
ConvertZepNotebook2py.py
File metadata and controls
60 lines (53 loc) · 2.38 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
50
51
52
53
54
55
56
57
58
59
60
import json
import codecs
from os import path
source_files = [r'C:\Testing.json', r'C:\Testing2.json']
extract_language = ['python', 'markdown']
for source_file in source_files:
wrong_files = []
if path.exists(source_file):
notebook = json.load(codecs.open(source_file, 'r', 'utf-8-sig'))
cell_script = []
script = ''
for i in notebook['paragraphs']:
language = i['config']['editorSetting']['language']
code = i['text'].splitlines()
if extract_language == None:
languageFilter = True
else:
languageFilter = (language in extract_language)
if languageFilter:
para =code[1:]
if len(para)>0:
if language == 'markdown':
para = '# ===================Markdown===================\n# '+'\n# '.join(para) + '\n# ================Markdown End=================='
else:
para = '\n'.join(para)
cell_script.append(para)
if 'results' in i.keys():
results = i['results']
if results['code'] == 'SUCCESS' and results['msg'][0]['type'] == 'TEXT':
cell_script.append('#===============Code Results================\n#' + results['msg'][0]['data'].replace('\n', '\n#') + '\n#===============End Results================')
script = '\n\n'.join(cell_script)
output_file = path.basename(source_file)
output_path = source_file.replace(output_file, '')
_, ext = path.splitext(source_file)
if len(ext) > 0:
output_file = output_file[:-len(ext)]
output_file = output_path + output_file + '.py'
j = 0
while path.exists(output_file):
if j-1 < 0:
output_file = output_file[:-3]
else:
output_file = output_file[:-3][:-len(str(j-1))]
output_file = output_file + str(j) + '.py'
j +=1
# output_file = output_path + output_file
with open(output_file, 'w') as f:
f.write(script)
else:
wrong_files.append(source_file)
if len(wrong_files) > 0:
print(f'The following file(s) were not successfully exported due to file path error:')
print('\n'.joint(wrong_files))