|
95 | 95 | "assert 'h_n' in exp.in_all['some.thing'][0].source" |
96 | 96 | ] |
97 | 97 | }, |
| 98 | + { |
| 99 | + "cell_type": "markdown", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "### Optional export processors" |
| 103 | + ] |
| 104 | + }, |
98 | 105 | { |
99 | 106 | "cell_type": "code", |
100 | 107 | "execution_count": null, |
|
122 | 129 | "metadata": {}, |
123 | 130 | "outputs": [], |
124 | 131 | "source": [ |
125 | | - "_cell = read_nb('../../tests/black.ipynb')['cells'][0]\n", |
| 132 | + "_cell = read_nb('../../tests/export_procs.ipynb')['cells'][0]\n", |
126 | 133 | "black_format(_cell, force=True)\n", |
127 | 134 | "test_eq(_cell.source, 'j = [1, 2, 3]')" |
128 | 135 | ] |
|
134 | 141 | "outputs": [], |
135 | 142 | "source": [ |
136 | 143 | "#|export\n", |
137 | | - "def nb_export(nbname, lib_path=None, procs=black_format, debug=False, mod_maker=ModuleMaker, name=None):\n", |
| 144 | + "# includes the newline, because calling .strip() would affect all cells.\n", |
| 145 | + "_magics_pattern = re.compile(r'^\\s*(%%|%).*\\n?', re.MULTILINE)\n", |
| 146 | + "\n", |
| 147 | + "def scrub_magics(cell): # Cell to format\n", |
| 148 | + " \"Processor to remove cell magics from exported code\"\n", |
| 149 | + " try: cfg = get_config()\n", |
| 150 | + " except FileNotFoundError: return\n", |
| 151 | + " if cell.cell_type != 'code': return\n", |
| 152 | + " try: cell.source = _magics_pattern.sub('', cell.source)\n", |
| 153 | + " except: pass" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "markdown", |
| 158 | + "metadata": {}, |
| 159 | + "source": [ |
| 160 | + "`scrub_magics` is a processor that scrubs the jupyter \"magics\" lines out of exported cells. This can be helpful when using tools like [sparkmagic](https://github.com/jupyter-incubator/sparkmagic) or just [Jupyter's builtin magics](https://ipython.readthedocs.io/en/stable/interactive/magics.html) in an nbdev project.\n", |
| 161 | + "\n", |
| 162 | + "Usage: \n", |
| 163 | + "This behavior can be enabled by passing `scrub_magics` into the `--procs` flag of the `nbdev_export` command.\n", |
| 164 | + "- `nbdev_export --procs scrub_magics`\n", |
| 165 | + "- `nbdev_export --procs 'scrub_magics black_format'`\n", |
| 166 | + "\n", |
| 167 | + "Example:\n", |
| 168 | + "\n", |
| 169 | + "A cell like below could export the line `\"hello nbdev\"` into the `bar` module. And the `%%spark` magic line would be omitted.\n", |
| 170 | + "\n", |
| 171 | + "```python\n", |
| 172 | + "%%spark\n", |
| 173 | + "#|export bar\n", |
| 174 | + "\"hello nbdev\"\n", |
| 175 | + "```\n", |
| 176 | + "\n", |
| 177 | + "It will export as something similar to this:\n", |
| 178 | + "\n", |
| 179 | + "```python\n", |
| 180 | + "# %% ../path/to/01_bar.ipynb 1\n", |
| 181 | + "\"hello nbdev\"\n", |
| 182 | + "```\n", |
| 183 | + "\n" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": null, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "_cell = read_nb('../../tests/export_procs.ipynb')['cells'][2]\n", |
| 193 | + "scrub_magics(_cell)\n", |
| 194 | + "test_eq(_cell.source, '''#|export bar\n", |
| 195 | + "\"hello nbdev\"''')" |
| 196 | + ] |
| 197 | + }, |
| 198 | + { |
| 199 | + "cell_type": "code", |
| 200 | + "execution_count": null, |
| 201 | + "metadata": {}, |
| 202 | + "outputs": [], |
| 203 | + "source": [ |
| 204 | + "#|export\n", |
| 205 | + "import nbdev.export\n", |
| 206 | + "def optional_procs():\n", |
| 207 | + " \"An explicit list of processors that could be used by `nb_export`\"\n", |
| 208 | + " return L([p for p in nbdev.export.__all__\n", |
| 209 | + " if p not in [\"nb_export\", \"ExportModuleProc\", \"optional_procs\"]])" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "code", |
| 214 | + "execution_count": null, |
| 215 | + "metadata": {}, |
| 216 | + "outputs": [], |
| 217 | + "source": [ |
| 218 | + "# every optional processor should be explicitly listed here\n", |
| 219 | + "test_eq(optional_procs(), ['black_format', 'scrub_magics'])" |
| 220 | + ] |
| 221 | + }, |
| 222 | + { |
| 223 | + "cell_type": "markdown", |
| 224 | + "metadata": {}, |
| 225 | + "source": [ |
| 226 | + "### `nb_export`" |
| 227 | + ] |
| 228 | + }, |
| 229 | + { |
| 230 | + "cell_type": "code", |
| 231 | + "execution_count": null, |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [], |
| 234 | + "source": [ |
| 235 | + "#|export\n", |
| 236 | + "def nb_export(nbname, lib_path=None, procs=None, debug=False, mod_maker=ModuleMaker, name=None):\n", |
138 | 237 | " \"Create module(s) from notebook\"\n", |
139 | 238 | " if lib_path is None: lib_path = get_config().lib_path\n", |
140 | 239 | " exp = ExportModuleProc()\n", |
|
0 commit comments