{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "b87acda1", "metadata": {}, "outputs": [], "source": [ "%%capture\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": null, "id": "bc6cf26f", "metadata": {}, "outputs": [], "source": [ "#| default_exp: exec" ] }, { "cell_type": "code", "execution_count": null, "id": "3f413657", "metadata": {}, "outputs": [], "source": [ "#| export\n", "import nbformat\n", "from nbclient import NotebookClient\n", "from nb.core import read_nb,CellType,PathOrFileLike" ] }, { "cell_type": "markdown", "id": "581d04b2", "metadata": {}, "source": [ "# Exec\n", "\n", "> Executing Jupyter Notebooks" ] }, { "cell_type": "code", "execution_count": null, "id": "c38d7ded", "metadata": {}, "outputs": [], "source": [ "def exec_nb(nb:PathOrFileLike, timeout:int=600):\n", " nb = read_nb(nb)\n", " client = NotebookClient(nb, timeout=timeout, kernel_name='python3')\n", " client.execute()\n", " return nb" ] }, { "cell_type": "code", "execution_count": null, "id": "81cb15c3", "metadata": {}, "outputs": [], "source": [ "%%timeit\n", "nb = exec_nb('exec.ipynb')\n", "nb" ] }, { "cell_type": "code", "execution_count": null, "id": "790a8536", "metadata": {}, "outputs": [], "source": [ "from concurrent.futures import ProcessPoolExecutor\n", "from pathlib import Path\n", "\n", "nb_paths = list(Path('some/dir').glob('**/*.ipynb'))\n", "with ProcessPoolExecutor() as pool:\n", " results = list(pool.map(exec_nb, nb_paths))" ] }, { "cell_type": "code", "execution_count": null, "id": "2b6edf3c", "metadata": {}, "outputs": [], "source": [ "def exec_nb_safe(path):\n", " try:\n", " nb = exec_nb(path)\n", " return path, nb, None\n", " except Exception as e:\n", " return path, None, e" ] }, { "cell_type": "code", "execution_count": null, "id": "cee2aeac", "metadata": {}, "outputs": [], "source": [ "import os\n", "max_workers = os.cpu_count()" ] }, { "cell_type": "markdown", "id": "a1e85008", "metadata": {}, "source": [] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }