Skip to content

Commit b725c01

Browse files
committed
add flag for preserving magics
1 parent 44c9a7f commit b725c01

File tree

4 files changed

+7
-12
lines changed

4 files changed

+7
-12
lines changed

nbdev/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def _apply_defaults(
6363
clear_all:bool_arg=False, # Remove all cell metadata and cell outputs?
6464
cell_number:bool_arg=True, # Add cell number to the exported file
6565
put_version_in_init:bool_arg=True, # Add the version to the main __init__.py in nbdev_export
66+
keep_magics:bool = False, # Keep magic commands in rendered documentation (useful for AImagic notebooks)
6667
):
6768
"Apply default settings where missing in `cfg`."
6869
if getattr(cfg,'repo',None) is None:

nbdev/processors.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ def ai_magics(cell):
183183

184184
def clean_magics(cell):
185185
"A preprocessor to remove cell magic commands"
186-
if cell.cell_type == 'code': cell.source = _magics_pattern.sub('', cell.source).strip()
186+
if cell.cell_type == 'code' and not get_config().get('keep_magics', False):
187+
cell.source = _magics_pattern.sub('', cell.source).strip()
187188

188189
# %% ../nbs/api/10_processors.ipynb
189190
_re_hdr_dash = re.compile(r'^#+\s+.*\s+-\s*$', re.MULTILINE)

nbs/api/01_config.ipynb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@
153153
" clear_all:bool_arg=False, # Remove all cell metadata and cell outputs?\n",
154154
" cell_number:bool_arg=True, # Add cell number to the exported file\n",
155155
" put_version_in_init:bool_arg=True, # Add the version to the main __init__.py in nbdev_export\n",
156+
" keep_magics:bool = False, # Keep magic commands in rendered documentation (useful for AImagic notebooks)\n",
156157
"):\n",
157158
" \"Apply default settings where missing in `cfg`.\"\n",
158159
" if getattr(cfg,'repo',None) is None:\n",
@@ -195,16 +196,7 @@
195196
"cell_type": "code",
196197
"execution_count": null,
197198
"metadata": {},
198-
"outputs": [
199-
{
200-
"name": "stderr",
201-
"output_type": "stream",
202-
"text": [
203-
"/home/dienhoa/miniconda3/envs/se/lib/python3.9/site-packages/ghapi/core.py:102: UserWarning: Neither GITHUB_TOKEN nor GITHUB_JWT_TOKEN found: running as unauthenticated\n",
204-
" else: warn('Neither GITHUB_TOKEN nor GITHUB_JWT_TOKEN found: running as unauthenticated')\n"
205-
]
206-
}
207-
],
199+
"outputs": [],
208200
"source": [
209201
"#|hide\n",
210202
"if os.getenv('GITHUB_ACTIONS') != 'true': # GITHUB_TOKEN in actions has limited scope.\n",

nbs/api/10_processors.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,8 @@
578578
"\n",
579579
"def clean_magics(cell):\n",
580580
" \"A preprocessor to remove cell magic commands\"\n",
581-
" if cell.cell_type == 'code': cell.source = _magics_pattern.sub('', cell.source).strip()"
581+
" if cell.cell_type == 'code' and not get_config().get('keep_magics', False):\n",
582+
" cell.source = _magics_pattern.sub('', cell.source).strip()"
582583
]
583584
},
584585
{

0 commit comments

Comments
 (0)