def set_kernelspec()

in utils/check_nb_kernel.py [0:0]


def set_kernelspec(nb_path: str, k_tgt: str, verbose: bool = False):
    """Update specified notebooks to `k_tgt` kernelspec."""
    changed_count = 0
    good_count = 0
    for nbook in _get_notebook_paths(nb_path):
        if ".ipynb_checkpoints" in str(nbook):
            continue
        with open(str(nbook), "r") as nb_read:
            nb_obj = nbformat.read(nb_read, as_version=4.0)
        kernelspec = nb_obj.get("metadata", {}).get("kernelspec", None)
        current_kspec_name = kernelspec.get("name")
        if not kernelspec:
            print("Error: no kernel information.")
            continue
        updated = False
        tgt_spec = IP_KERNEL_SPEC[k_tgt]
        for k_name, k_item in kernelspec.items():
            if tgt_spec[k_name] != k_item:
                updated = True
                kernelspec[k_name] = tgt_spec[k_name]
        if updated:
            changed_count += 1
            _print_nb_header(nbook)
            print(
                f"Kernelspec updated from '{current_kspec_name}' to '"
                f"{kernelspec.get('name')}"
                "'"
            )
            print("  ", kernelspec, "\n")
            backup_path = (
                f"{str(nbook).strip(nbook.suffix)}-{current_kspec_name}{nbook.suffix}"
            )
            nbook.rename(backup_path)
            nbformat.write(nb_obj, str(nbook))
            continue
        if verbose:
            _print_nb_header(nbook)
            print(f"{kernelspec['name']} ok\n")
        good_count += 1
    print(f"{good_count} notebooks with no changes, {changed_count} updated")