From 9ddd4ebac62cfce41f0e2514598b8f674dbaf561 Mon Sep 17 00:00:00 2001 From: seem Date: Tue, 13 Sep 2022 21:34:05 +1000 Subject: [PATCH 1/2] get `Config.types` from `docments(_apply_defaults)` --- nbdev/_modidx.py | 1 + nbdev/config.py | 12 +++++++----- nbs/api/config.ipynb | 14 ++++++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/nbdev/_modidx.py b/nbdev/_modidx.py index 2ce62234f..b3bfd99af 100644 --- a/nbdev/_modidx.py +++ b/nbdev/_modidx.py @@ -33,6 +33,7 @@ 'nbdev.config._has_py': ('api/config.html#_has_py', 'nbdev/config.py'), 'nbdev.config._nbdev_config_file': ('api/config.html#_nbdev_config_file', 'nbdev/config.py'), 'nbdev.config._prompt_user': ('api/config.html#_prompt_user', 'nbdev/config.py'), + 'nbdev.config._type': ('api/config.html#_type', 'nbdev/config.py'), 'nbdev.config._xdg_config_paths': ('api/config.html#_xdg_config_paths', 'nbdev/config.py'), 'nbdev.config.add_init': ('api/config.html#add_init', 'nbdev/config.py'), 'nbdev.config.config_key': ('api/config.html#config_key', 'nbdev/config.py'), diff --git a/nbdev/config.py b/nbdev/config.py index 5e136af6f..c5e4b8f2e 100644 --- a/nbdev/config.py +++ b/nbdev/config.py @@ -13,6 +13,7 @@ # %% ../nbs/api/config.ipynb 4 from datetime import datetime +from fastcore.docments import * from fastcore.utils import * from fastcore.meta import * from fastcore.script import * @@ -40,9 +41,9 @@ def _apply_defaults( branch='master', # Repo default branch git_url='https://github.com/%(user)s/%(repo)s', # Repo URL custom_sidebar:bool_arg=False, # Use a custom sidebar.yml? - nbs_path='.', # Path to notebooks - lib_path:str=None, # Path to package root (default: `repo` with `-` replaced by `_`) - doc_path='_docs', # Path to rendered docs + nbs_path:Path='.', # Path to notebooks + lib_path:Path=None, # Path to package root (default: `repo` with `-` replaced by `_`) + doc_path:Path='_docs', # Path to rendered docs tst_flags='notest', # Test flags version='0.0.1', # Version of this release doc_host='https://%(user)s.github.io', # Hostname for docs @@ -62,6 +63,7 @@ def _apply_defaults( allowed_cell_metadata_keys='', # Preserve the list of keys in cell level metadata jupyter_hooks=True, # Run Jupyter hooks? clean_ids=True, # Remove ids from plaintext reprs? + clear_all=False, # Remove all cell metadata and cell outputs? custom_quarto_yml=False, # Use a custom _quarto.yml? ): "Apply default settings where missing in `cfg`." @@ -188,8 +190,8 @@ def _xdg_config_paths(cfg_name=_nbdev_cfg_name): return [o/_nbdev_home_dir/cfg_name for o in xdg_config_paths] # %% ../nbs/api/config.ipynb 28 -_types = dict(custom_sidebar=bool, nbs_path=Path, lib_path=Path, doc_path=Path, recursive=bool, - black_formatting=bool, jupyter_hooks=bool, clean_ids=bool, custom_quarto_yml=bool, preview_port=int) +def _type(t): return bool if t==bool_arg else t +_types = {k:_type(v['anno']) for k,v in docments(_apply_defaults,full=True,returns=False).items() if k != 'cfg'} @functools.lru_cache(maxsize=None) def get_config(cfg_name=_nbdev_cfg_name, path=None): diff --git a/nbs/api/config.ipynb b/nbs/api/config.ipynb index 0b37d5c9e..d5f94f42a 100644 --- a/nbs/api/config.ipynb +++ b/nbs/api/config.ipynb @@ -52,6 +52,7 @@ "source": [ "#|export\n", "from datetime import datetime\n", + "from fastcore.docments import *\n", "from fastcore.utils import *\n", "from fastcore.meta import *\n", "from fastcore.script import *\n", @@ -148,9 +149,9 @@ " branch='master', # Repo default branch\n", " git_url='https://github.com/%(user)s/%(repo)s', # Repo URL\n", " custom_sidebar:bool_arg=False, # Use a custom sidebar.yml?\n", - " nbs_path='.', # Path to notebooks\n", - " lib_path:str=None, # Path to package root (default: `repo` with `-` replaced by `_`)\n", - " doc_path='_docs', # Path to rendered docs\n", + " nbs_path:Path='.', # Path to notebooks\n", + " lib_path:Path=None, # Path to package root (default: `repo` with `-` replaced by `_`)\n", + " doc_path:Path='_docs', # Path to rendered docs\n", " tst_flags='notest', # Test flags\n", " version='0.0.1', # Version of this release\n", " doc_host='https://%(user)s.github.io', # Hostname for docs\n", @@ -170,6 +171,7 @@ " allowed_cell_metadata_keys='', # Preserve the list of keys in cell level metadata\n", " jupyter_hooks=True, # Run Jupyter hooks?\n", " clean_ids=True, # Remove ids from plaintext reprs?\n", + " clear_all=False, # Remove all cell metadata and cell outputs?\n", " custom_quarto_yml=False, # Use a custom _quarto.yml?\n", "):\n", " \"Apply default settings where missing in `cfg`.\"\n", @@ -451,8 +453,8 @@ "outputs": [], "source": [ "#|export\n", - "_types = dict(custom_sidebar=bool, nbs_path=Path, lib_path=Path, doc_path=Path, recursive=bool, \n", - " black_formatting=bool, jupyter_hooks=bool, clean_ids=bool, custom_quarto_yml=bool, preview_port=int)\n", + "def _type(t): return bool if t==bool_arg else t\n", + "_types = {k:_type(v['anno']) for k,v in docments(_apply_defaults,full=True,returns=False).items() if k != 'cfg'}\n", "\n", "@functools.lru_cache(maxsize=None)\n", "def get_config(cfg_name=_nbdev_cfg_name, path=None):\n", @@ -785,7 +787,7 @@ "path = Path('../nbdev')\n", "(path/'config.py').unlink(missing_ok=True)\n", "\n", - "_basic_export_nb(\"01_config.ipynb\", 'config.py')\n", + "_basic_export_nb(\"config.ipynb\", 'config.py')\n", "\n", "g = exec_new('from nbdev import config')\n", "assert g['config'].add_init\n", From 5a407cdb65f699358bc76dafaf65595fc0d22e21 Mon Sep 17 00:00:00 2001 From: seem Date: Tue, 13 Sep 2022 21:40:21 +1000 Subject: [PATCH 2/2] fix #1041 --- nbdev/clean.py | 12 +++++------- nbs/api/clean.ipynb | 16 ++++++---------- settings.ini | 1 + 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/nbdev/clean.py b/nbdev/clean.py index c622a7153..757e07861 100644 --- a/nbdev/clean.py +++ b/nbdev/clean.py @@ -79,7 +79,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids): # %% ../nbs/api/clean.ipynb 13 def clean_nb( nb, # The notebook to clean - clear_all=False, # Remove all cell metadata and cell outputs + clear_all=False, # Remove all cell metadata and cell outputs? allowed_metadata_keys:list=None, # Preserve the list of keys in the main notebook metadata allowed_cell_metadata_keys:list=None, # Preserve the list of keys in cell level metadata clean_ids=True, # Remove ids from plaintext reprs? @@ -113,19 +113,18 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False): warn(e) # %% ../nbs/api/clean.ipynb 26 -def _nbdev_clean(nb, path=None, **kwargs): +def _nbdev_clean(nb, path=None, clear_all=None): cfg = get_config(path=path) + clear_all = clear_all or cfg.clear_all allowed_metadata_keys = cfg.get("allowed_metadata_keys").split() allowed_cell_metadata_keys = cfg.get("allowed_cell_metadata_keys").split() - clean_ids = cfg.get('clean_ids', False) - return clean_nb(nb, clean_ids=clean_ids, allowed_metadata_keys=allowed_metadata_keys, - allowed_cell_metadata_keys=allowed_cell_metadata_keys, **kwargs) + return clean_nb(nb, clear_all, allowed_metadata_keys, allowed_cell_metadata_keys, cfg.clean_ids) # %% ../nbs/api/clean.ipynb 27 @call_parse def nbdev_clean( fname:str=None, # A notebook name or glob to clean - clear_all:bool=False, # Clean all metadata and outputs + 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 ): @@ -134,7 +133,6 @@ def nbdev_clean( _clean = partial(_nbdev_clean, clear_all=clear_all) _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) diff --git a/nbs/api/clean.ipynb b/nbs/api/clean.ipynb index d926d4077..ef9b0beaa 100644 --- a/nbs/api/clean.ipynb +++ b/nbs/api/clean.ipynb @@ -12,9 +12,7 @@ { "cell_type": "code", "execution_count": null, - "metadata": { - "hide_input": false - }, + "metadata": {}, "outputs": [], "source": [ "#|default_exp clean" @@ -191,7 +189,7 @@ "#|export\n", "def clean_nb(\n", " nb, # The notebook to clean\n", - " clear_all=False, # Remove all cell metadata and cell outputs\n", + " clear_all=False, # Remove all cell metadata and cell outputs?\n", " allowed_metadata_keys:list=None, # Preserve the list of keys in the main notebook metadata\n", " allowed_cell_metadata_keys:list=None, # Preserve the list of keys in cell level metadata\n", " clean_ids=True, # Remove ids from plaintext reprs?\n", @@ -348,13 +346,12 @@ "outputs": [], "source": [ "#|export\n", - "def _nbdev_clean(nb, path=None, **kwargs):\n", + "def _nbdev_clean(nb, path=None, clear_all=None):\n", " cfg = get_config(path=path)\n", + " clear_all = clear_all or cfg.clear_all\n", " allowed_metadata_keys = cfg.get(\"allowed_metadata_keys\").split()\n", " allowed_cell_metadata_keys = cfg.get(\"allowed_cell_metadata_keys\").split()\n", - " clean_ids = cfg.get('clean_ids', False)\n", - " return clean_nb(nb, clean_ids=clean_ids, allowed_metadata_keys=allowed_metadata_keys,\n", - " allowed_cell_metadata_keys=allowed_cell_metadata_keys, **kwargs)" + " return clean_nb(nb, clear_all, allowed_metadata_keys, allowed_cell_metadata_keys, cfg.clean_ids)" ] }, { @@ -367,7 +364,7 @@ "@call_parse\n", "def nbdev_clean(\n", " fname:str=None, # A notebook name or glob to clean\n", - " clear_all:bool=False, # Clean all metadata and outputs\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", "):\n", @@ -376,7 +373,6 @@ " _clean = partial(_nbdev_clean, clear_all=clear_all)\n", " _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", - " \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)" ] diff --git a/settings.ini b/settings.ini index 0d3f27ee6..9af0a27e4 100644 --- a/settings.ini +++ b/settings.ini @@ -67,4 +67,5 @@ allowed_cell_metadata_keys = jupyter_hooks = True clean_ids = False custom_quarto_yml = True +clear_all = False