-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipynb_to_py.py
46 lines (43 loc) · 1.3 KB
/
ipynb_to_py.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
'''
Converts ipynb to py.
Works even when Jupyter is not installed.
'''
import json
from io import StringIO as IO
import sys
from os import path
from myfile import openAsciize
def convert(inIO):
print('Parsing json into dict...')
root = json.load(inIO)
print('done')
outIO = IO()
print('extracting source code...')
for i, cell in enumerate(root['cells']):
print('#' * 5, 'cell', i, '#' * 5, file = outIO)
if cell['cell_type'] != 'code':
print('', *cell['source'], file = outIO, sep = '#')
else:
print(*cell['source'], file=outIO, sep = '')
outIO.seek(0)
print('done')
return outIO
def main():
if len(sys.argv) < 2:
from console import console
console({'convert': convert})
else:
in_filename = sys.argv[1]
with openAsciize(in_filename, verbose = True) as inF:
print('File opened:', in_filename)
out_filename = path.splitext(in_filename)[0] + '.py'
print('Will overwrite:', out_filename)
if input('Ok? y/n ') == 'y':
with open(out_filename, 'w') as outF:
outF.write(convert(inF).read())
print('ok')
else:
print('canceled')
if __name__ == '__main__':
main()
sys.exit(0)