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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "451c8f58",
"metadata": {},
"outputs": [],
"source": [
"%%capture\n",
"%load_ext autoreload\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f290d8fb",
"metadata": {},
"outputs": [],
"source": [
"#| default_exp: cli"
]
},
{
"cell_type": "markdown",
"id": "0c27d4d0",
"metadata": {},
"source": [
"# CLI\n",
"\n",
"> Command-line interface for using `nb`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a62b4fb4",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"import argparse,json,sys\n",
"from pathlib import Path\n",
"\n",
"import nbformat\n",
"\n",
"from nb import export_nb,clean_nb\n",
"from nb.export import read_nb"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12e3162c",
"metadata": {},
"outputs": [],
"source": [
"import tempfile\n",
"from nb import test"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "96737c5a",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def main():\n",
" p = _get_parser()\n",
" args = p.parse_args()\n",
" if not args.cmd:\n",
" p.print_help()\n",
" raise SystemExit(1)\n",
" if args.cmd == 'export':\n",
" for nb in args.notebooks:\n",
" export_nb(nb, args.export_dir)\n",
" if args.cmd == 'clean':\n",
" for nb in args.notebooks:\n",
" nbformat.write(clean_nb(nb), nb)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "69b642d9",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def _get_parser():\n",
" parser = argparse.ArgumentParser(prog='nb')\n",
" subparsers = parser.add_subparsers(dest='cmd')\n",
" export_parser = subparsers.add_parser('export', help='Export ipynb notebooks to Python')\n",
" export_parser.add_argument('notebooks', nargs='+', help='Notebook files to export')\n",
" export_parser.add_argument('--export-dir', default='.', help='Directory to export Python files to')\n",
" clean_parser = subparsers.add_parser('clean', help='Clean notebooks')\n",
" clean_parser.add_argument('notebooks', nargs='+', help='Notebook files to clean')\n",
" return parser"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b95c2d4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"usage: nb [-h] {export,clean} ...\n",
"\n",
"positional arguments:\n",
" {export,clean}\n",
" export Export ipynb notebooks to Python\n",
" clean Clean notebooks\n",
"\n",
"options:\n",
" -h, --help show this help message and exit\n"
]
}
],
"source": [
"sys.argv = ['script']\n",
"with test.raises(exc=SystemExit, contains='1'): main()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5da41c17",
"metadata": {},
"outputs": [],
"source": [
"with tempfile.TemporaryDirectory() as tmp:\n",
" nb = nbformat.v4.new_notebook()\n",
" nb.cells = [\n",
" nbformat.v4.new_code_cell('#| default_exp: mymod'),\n",
" nbformat.v4.new_code_cell('#| export\\ndef hello(): return \"hi\"'),\n",
" ]\n",
" nb_path = tmp + '/tmp.ipynb'\n",
" with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n",
" sys.argv = ['script', 'export', '--export-dir', tmp, nb_path]\n",
" main()\n",
" test.equal(\n",
" Path(tmp + '/mymod.py').read_text(),\n",
" f'# AUTOGENERATED! DO NOT EDIT!\\n\\n# cell: {nb.cells[1]['id']}\\ndef hello(): return \"hi\"\\n',\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f361af2b",
"metadata": {},
"outputs": [],
"source": [
"with tempfile.TemporaryDirectory() as tmp:\n",
" nb = nbformat.v4.new_notebook()\n",
" nb.metadata['bogus'] = 'should be removed'\n",
" nb.cells = [\n",
" nbformat.v4.new_code_cell('#| default_exp: mymod'),\n",
" nbformat.v4.new_code_cell('print(\"hi\")', execution_count=42),\n",
" ]\n",
" nb_path = tmp + '/tmp.ipynb'\n",
" with open(nb_path, mode='w+') as f: nbformat.write(nb, f)\n",
" sys.argv = ['script', 'clean', nb_path]\n",
" main()\n",
" cleaned = read_nb(nb_path)\n",
" test.equal(cleaned.cells[1].execution_count, None)\n",
" test.equal('bogus' not in cleaned.metadata, True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c086e4b4",
"metadata": {},
"outputs": [],
"source": [
"export_nb('cli.ipynb', src_dir='../nb')"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
|