Last active
October 27, 2022 22:02
-
-
Save oakaigh/0053123d154e69f2bf88cf765ec2ecc4 to your computer and use it in GitHub Desktop.
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 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