def split_ipynb(ipynb_text, filenames):
    'Split a Jupyter notebook based on filenames present in its blocks\n\n    Given the text of a Jupyter notebook marked with the output filename\n    in comments (e.g. <!-- jupyter-book 02_mybook.ipynb -->), return a list of\n    Jupyter notebooks separated accordingly.\n    :param str ipynb_text: ipynb code marked with individual filenames i.e. <!-- jupyter-book 02_mybook.ipynb -->\n    :param list[str] filenames: filenames\n    :return: ipynb_texts with the ipynb code for each block\n    :rtype: list[str]\n    '
    ipynb_dict = json.loads(ipynb_text)
    cells = ipynb_dict.pop('cells')
    ind_fname = []
    block_sources = [''.join(c['source']) for c in cells]
    for fname in filenames:
        marking = ('<!-- jupyter-book % s -->' % fname)
        for (b, block) in enumerate(block_sources):
            if (block.find(marking) > (- 1)):
                ind_fname.append(b)
                break
    if (len(ind_fname) != len(filenames)):
        errwarn('*** error : could not find all markings in ipynb')
        _abort()
    ipynb_texts = ([''] * len(filenames))
    for (i, ind_start) in enumerate(ind_fname):
        ind_end = None
        if ((i + 1) < len(ind_fname)):
            ind_end = ind_fname[(i + 1)]
        block_dict = ipynb_dict.copy()
        block_dict['cells'] = cells[ind_start:ind_end]
        ipynb_texts[i] = json.dumps(block_dict, indent=1, separators=(',', ':'))
    return ipynb_texts
Split a Jupyter notebook based on filenames present in its blocks

Given the text of a Jupyter notebook marked with the output filename
in comments (e.g. <!-- jupyter-book 02_mybook.ipynb -->), return a list of
Jupyter notebooks separated accordingly.
:param str ipynb_text: ipynb code marked with individual filenames i.e. <!-- jupyter-book 02_mybook.ipynb -->
:param list[str] filenames: filenames
:return: ipynb_texts with the ipynb code for each block
:rtype: list[str]
def split_ipynb(ipynb_text, filenames):
    'Split a Jupyter notebook based on filenames present in its blocks\n\n    Given the text of a Jupyter notebook marked with the output filename\n    in comments (e.g. <!-- jupyter-book 02_mybook.ipynb -->), return a list of\n    Jupyter notebooks separated accordingly.\n    :param str ipynb_text: ipynb code marked with individual filenames i.e. <!-- jupyter-book 02_mybook.ipynb -->\n    :param list[str] filenames: filenames\n    :return: ipynb_texts with the ipynb code for each block\n    :rtype: list[str]\n    '
    ipynb_dict = json.loads(ipynb_text)
    cells = ipynb_dict.pop('cells')
    ind_fname = []
    block_sources = [.join(c['source']) for c in cells]
    for fname in filenames:
        marking = ('<!-- jupyter-book % s -->' % fname)
        for (b, block) in enumerate(block_sources):
            if (block.find(marking) > (- 1)):
                ind_fname.append(b)
                break
    if (len(ind_fname) != len(filenames)):
        errwarn('*** error : could not find all markings in ipynb')
        _abort()
    ipynb_texts = ([] * len(filenames))
    for (i, ind_start) in enumerate(ind_fname):
        ind_end = None
        if ((i + 1) < len(ind_fname)):
            ind_end = ind_fname[(i + 1)]
        block_dict = ipynb_dict.copy()
        block_dict['cells'] = cells[ind_start:ind_end]
        ipynb_texts[i] = json.dumps(block_dict, indent=1, separators=(',', ':'))
    return ipynb_texts