Skip to content

Commit

Permalink
add flag for preserving magics
Browse files Browse the repository at this point in the history
  • Loading branch information
hamelsmu committed Sep 12, 2024
1 parent 44c9a7f commit b725c01
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 12 deletions.
1 change: 1 addition & 0 deletions nbdev/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def _apply_defaults(
clear_all:bool_arg=False, # Remove all cell metadata and cell outputs?
cell_number:bool_arg=True, # Add cell number to the exported file
put_version_in_init:bool_arg=True, # Add the version to the main __init__.py in nbdev_export
keep_magics:bool = False, # Keep magic commands in rendered documentation (useful for AImagic notebooks)
):
"Apply default settings where missing in `cfg`."
if getattr(cfg,'repo',None) is None:
Expand Down
3 changes: 2 additions & 1 deletion nbdev/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def ai_magics(cell):

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

# %% ../nbs/api/10_processors.ipynb
_re_hdr_dash = re.compile(r'^#+\s+.*\s+-\s*$', re.MULTILINE)
Expand Down
12 changes: 2 additions & 10 deletions nbs/api/01_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
" clear_all:bool_arg=False, # Remove all cell metadata and cell outputs?\n",
" cell_number:bool_arg=True, # Add cell number to the exported file\n",
" put_version_in_init:bool_arg=True, # Add the version to the main __init__.py in nbdev_export\n",
" keep_magics:bool = False, # Keep magic commands in rendered documentation (useful for AImagic notebooks)\n",
"):\n",
" \"Apply default settings where missing in `cfg`.\"\n",
" if getattr(cfg,'repo',None) is None:\n",
Expand Down Expand Up @@ -195,16 +196,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" else: warn('Neither GITHUB_TOKEN nor GITHUB_JWT_TOKEN found: running as unauthenticated')\n"
]
}
],
"outputs": [],
"source": [
"#|hide\n",
"if os.getenv('GITHUB_ACTIONS') != 'true': # GITHUB_TOKEN in actions has limited scope.\n",
Expand Down
3 changes: 2 additions & 1 deletion nbs/api/10_processors.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,8 @@
"\n",
"def clean_magics(cell):\n",
" \"A preprocessor to remove cell magic commands\"\n",
" if cell.cell_type == 'code': cell.source = _magics_pattern.sub('', cell.source).strip()"
" if cell.cell_type == 'code' and not get_config().get('keep_magics', False):\n",
" cell.source = _magics_pattern.sub('', cell.source).strip()"
]
},
{
Expand Down

0 comments on commit b725c01

Please sign in to comment.