aboutsummaryrefslogtreecommitdiffstats
path: root/nbs/exec.ipynb
blob: fe1356993ef1632f355901447b4a6e792bc5b30d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
}