Skip to content

Instantly share code, notes, and snippets.

@oakaigh
Last active October 27, 2022 22:02
Show Gist options
  • Save oakaigh/0053123d154e69f2bf88cf765ec2ecc4 to your computer and use it in GitHub Desktop.
Save oakaigh/0053123d154e69f2bf88cf765ec2ecc4 to your computer and use it in GitHub Desktop.
import re
def _text_sep(f, rexp):
with open(f) as cells_md_f:
s = ''
rr = re.compile(rexp)
for l in cells_md_f.readlines():
if re.match(rr, l):
yield s
s = ''
s += l
yield s
from nbformat import v4 as nbf
nb = nbf.new_notebook()
nb['cells'] = []
for md, code in zip(
_text_sep('cells.md', fr'''{re.escape('## Problem ')}.*'''),
_text_sep('cells.py', fr'''{re.escape('#PROBLEM ')}.*''')
):
nb['cells'] += [
nbf.new_markdown_cell(md),
nbf.new_code_cell(code)
]
import json
json.dump(nb, open('my_notebook.ipynb', mode='w'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment