Skip to content

Instantly share code, notes, and snippets.

@geblanco
Last active June 25, 2019 12:47
Show Gist options
  • Save geblanco/8c228e7ed3d09529f89a8a8fdca173a6 to your computer and use it in GitHub Desktop.
Save geblanco/8c228e7ed3d09529f89a8a8fdca173a6 to your computer and use it in GitHub Desktop.
Extracts all the content inside an *.ipynb file and write it to a file of code, non code cells go as comments
import os
import sys
import json
languages = dict(
python=dict(ext='py', comment='#'),
javascript=dict(ext='js', comment='//')
)
if len(sys.argv) < 2:
print('Usage {} <ipython notebook> [<ipython notebook>, ...]'.format(sys.argv[0]))
sys.exit(0)
notebooks = sys.argv[1:]
# print(notebooks)
def extract(notebook, lang_comment):
lines = []
for cell in notebook['cells']:
prepend = ''
if cell['cell_type'] != 'code':
# comment non code lines
prepend = lang_comment + ' '
lines.extend([(prepend if not s.startswith(lang_comment) else '') + s for s in cell['source']])
lines.append('\n\n')
return ''.join(lines)
for notebook in notebooks:
notebook_path = os.path.abspath(notebook)
notebook_content = json.load(open(notebook_path, 'r'))
notebook_language = notebook_content['metadata']['kernelspec'].get('language', 'python')
if notebook_language not in languages:
print('Unkown language for notebook: ', notebook_pathk)
else:
notebook_name = os.path.splitext(os.path.basename(notebook_path))[0]
notebook_dirname = os.path.dirname(notebook_path)
notebook_output_name = notebook_name + '_code.' + languages.get(notebook_language).get('ext')
notebook_output = os.path.join(notebook_dirname, notebook_output_name)
lang_comment = languages.get(notebook_language).get('comment')
extracted = extract(notebook_content, lang_comment)
with open(notebook_output, 'w') as fout:
fout.write(extracted)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment