You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

48 lines
1.4 KiB

import json
import sys
from os import path
header_comment = '# %%\n'
def nb2py(notebook):
result = []
cells = notebook['cells']
for cell in cells:
cell_type = cell['cell_type']
metadata = cell['metadata']
format_metadata = json.dumps(metadata,indent=2).split("\n")
reformat_metadata = '# !! {"metadata":'
for key in format_metadata:
if key == '{':
reformat_metadata+=f"{key}\n"
elif key == '}':
reformat_metadata+="# !! "+key+"}\n"
else:
reformat_metadata+=f'# !! {key}\n'
if cell_type == 'markdown':
result.append('%s"""\n%s\n"""'%
(header_comment+reformat_metadata, ''.join(cell['source'])))
if cell_type == 'code':
result.append("%s%s" % (header_comment+reformat_metadata, ''.join(cell['source'])))
return '\n\n'.join(result)
def convert(in_file, out_file):
_, in_ext = path.splitext(in_file)
_, out_ext = path.splitext(out_file)
if in_ext == '.ipynb' and out_ext == '.py':
with open(in_file, 'r', encoding='utf-8') as f:
notebook = json.load(f)
py_str = nb2py(notebook)
with open(out_file, 'w', encoding='utf-8') as f:
f.write(py_str)
else:
raise(Exception('Extensions must be .ipynb and .py or vice versa'))
convert('Disco_Diffusion.ipynb', 'disco.py')