def get_cell_by_name(nb, name):
    """Retrieve a cell from *nb* according to its metadata *name*:

    The Jupyter notebook format allows specifying a document-wide unique 'name'
    metadata attribute for each cell:

    https://nbformat.readthedocs.io/en/latest/format_description.html
                                                                #cell-metadata

    Return the cell matching the name, or raise ValueError.
    """
    for i, cell in enumerate(nb.cells):
        try:
            cell_name = cell.metadata.jupyter.name
            if cell_name == name:
                return cell
        except AttributeError:
            continue

    raise ValueError("no cell named '{}'".format(name))