Created
August 12, 2014 06:18
-
-
Save qbilius/e4165c0d71f556efce5f to your computer and use it in GitHub Desktop.
Simple TOC generator for IPython Notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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