aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <hello@eugenetriguba.com>2026-05-24 00:57:30 +0000
committerEugene Triguba <hello@eugenetriguba.com>2026-05-24 00:57:30 +0000
commit53fc53c2c3141b7ea1a47e1128ea1d368ddec47e (patch)
treed9cf396e74a11d0007717ef349d7556fc3d9fff0
parentRefactor core utilities from export into core module (diff)
downloadnb-53fc53c2c3141b7ea1a47e1128ea1d368ddec47e.zip
Add inital work in progress exec
-rw-r--r--nb/exec.py6
-rw-r--r--nbs/exec.ipynb125
2 files changed, 131 insertions, 0 deletions
diff --git a/nb/exec.py b/nb/exec.py
new file mode 100644
index 0000000..a7cbf3e
--- /dev/null
+++ b/nb/exec.py
@@ -0,0 +1,6 @@
+# AUTOGENERATED! DO NOT EDIT!
+
+# cell: 3f413657
+import nbformat
+from nbclient import NotebookClient
+from nb.core import read_nb,CellType,PathOrFileLike
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
+}