Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nbdev_clean to accept multiple filenames or globs (#1480) #1488

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions nbdev/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def _nbdev_clean(nb, path=None, clear_all=None):
# %% ../nbs/api/11_clean.ipynb
@call_parse
def nbdev_clean(
fname:str=None, # A notebook name or glob to clean
fname=None, # A notebook path or a list of paths/globs to clean
clear_all:bool=False, # Remove all cell metadata and cell outputs?
disp:bool=False, # Print the cleaned outputs
stdin:bool=False # Read notebook from input stream
Expand All @@ -141,7 +141,12 @@ def nbdev_clean(
_write = partial(process_write, warn_msg='Failed to clean notebook', proc_nb=_clean)
if stdin: return _write(f_in=sys.stdin, f_out=sys.stdout)
if fname is None: fname = get_config().nbs_path
for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re='^[_.]'): _write(f_in=f, disp=disp)
# If `fname` is a single string, wrap it in a list.
# Otherwise assume it's already a list/tuple of paths/globs.
if isinstance(fname, (str, Path)): fname = [str(fname)]
for f in fname:
for nb_path in globtastic(f, file_glob='*.ipynb', skip_folder_re='^[_.]'):
_write(f_in=nb_path, disp=disp)

# %% ../nbs/api/11_clean.ipynb
def clean_jupyter(path, model, **kwargs):
Expand Down
47 changes: 45 additions & 2 deletions nbs/api/11_clean.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@
"#|export\n",
"@call_parse\n",
"def nbdev_clean(\n",
" fname:str=None, # A notebook name or glob to clean\n",
" fname=None, # A notebook path or a list of paths/globs to clean\n",
" clear_all:bool=False, # Remove all cell metadata and cell outputs?\n",
" disp:bool=False, # Print the cleaned outputs\n",
" stdin:bool=False # Read notebook from input stream\n",
Expand All @@ -399,7 +399,12 @@
" _write = partial(process_write, warn_msg='Failed to clean notebook', proc_nb=_clean)\n",
" if stdin: return _write(f_in=sys.stdin, f_out=sys.stdout)\n",
" if fname is None: fname = get_config().nbs_path\n",
" for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re='^[_.]'): _write(f_in=f, disp=disp)"
" # If `fname` is a single string, wrap it in a list.\n",
" # Otherwise assume it's already a list/tuple of paths/globs.\n",
" if isinstance(fname, (str, Path)): fname = [str(fname)]\n",
" for f in fname:\n",
" for nb_path in globtastic(f, file_glob='*.ipynb', skip_folder_re='^[_.]'):\n",
" _write(f_in=nb_path, disp=disp)"
]
},
{
Expand Down Expand Up @@ -811,6 +816,44 @@
"test_eq(nb.cells[-1].output, ours.cells[-1].output)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#|hide\n",
"#|eval: false\n",
"with tempfile.TemporaryDirectory() as d, working_directory(d):\n",
" _run('git init')\n",
" _run(\"git config user.email 'nbdev@fast.ai'\")\n",
" _run(\"git config user.name 'nbdev'\")\n",
"\n",
" nbs_path = Path('nbs')\n",
" nbs_path.mkdir()\n",
" Config('.', 'settings.ini', create={'nbs_path':nbs_path,'author':'fastai'})\n",
" _run('nbdev_install_hooks')\n",
" \n",
" # Create two minimal notebooks\n",
" nb1 = nbs_path/'nb1.ipynb'\n",
" nb2 = nbs_path/'nb2.ipynb'\n",
" write_nb({}, nb1)\n",
" write_nb({}, nb2)\n",
" \n",
" # Commit them so we can confirm nbdev_clean runs with a clean repo\n",
" _run(\"git add . && git commit -m 'Add nb1 and nb2'\")\n",
" \n",
" # Run nbdev_clean with multiple notebook paths\n",
" proc = _run(f'nbdev_clean --fname \"{nb1}\" \"{nb2}\"', check=False)\n",
" \n",
" if proc.stderr: \n",
" raise AssertionError(f'nbdev_clean command failed with:\\n\\n{proc.stderr}')\n",
" \n",
" # Check that both files still exist (and if you want, verify they’re “cleaned”)\n",
" assert nb1.exists(), \"nb1.ipynb was removed or not found\"\n",
" assert nb2.exists(), \"nb2.ipynb was removed or not found\""
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down