def _get_last_cell(nb):
    """
    Get last cell, ignores cells with empty source (unless the notebook only
    has one cell and it's empty)
    """
    # iterate in reverse order
    for idx in range(-1, -len(nb.cells) - 1, -1):
        cell = nb.cells[idx]

        # only return it if it has some code
        if cell['source'].strip():
            return cell

    # otherwise return the first cell
    return nb.cells[0]