From 53fc53c2c3141b7ea1a47e1128ea1d368ddec47e Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 24 May 2026 00:57:30 +0000 Subject: Add inital work in progress exec --- nbs/exec.ipynb | 125 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 nbs/exec.ipynb (limited to 'nbs/exec.ipynb') diff --git a/nbs/exec.ipynb b/nbs/exec.ipynb new file mode 100644 index 0000000..fe13569 --- /dev/null +++ b/nbs/exec.ipynb @@ -0,0 +1,125 @@ +{ + "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 +} -- cgit v1.2.3