Skip to content

Instantly share code, notes, and snippets.

@qbilius
Created August 12, 2014 06:18
Show Gist options
  • Save qbilius/e4165c0d71f556efce5f to your computer and use it in GitHub Desktop.
Save qbilius/e4165c0d71f556efce5f to your computer and use it in GitHub Desktop.
Simple TOC generator for IPython Notebooks
import json
with open('mynotebook.ipynb', 'r') as f:
with open('contents.md', 'w') as out:
nb = json.load(f)
for cell in nb['worksheets'][0]['cells']:
if cell['cell_type'] == 'heading':
pad = (cell['level'] - 1) * 2
link = cell['source'][0].replace(' ', '-')
link = link.replace('(', '%28').replace(')', '%29')
text = '- [%s](#%s)' % (cell['source'][0], link)
line = '{{0: >{}}}'.format(pad + len(text)).format(text)
out.write(line + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment