diff --git a/nbdev/clean.py b/nbdev/clean.py index 9bf06848b..53a42b9fe 100644 --- a/nbdev/clean.py +++ b/nbdev/clean.py @@ -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 @@ -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): diff --git a/nbs/api/11_clean.ipynb b/nbs/api/11_clean.ipynb index 5e88ca73b..9c8a1ae5f 100644 --- a/nbs/api/11_clean.ipynb +++ b/nbs/api/11_clean.ipynb @@ -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", @@ -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)" ] }, { @@ -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": {},