Skip to content

Commit

Permalink
Merge pull request #738 from seeM/jupyter-hooks-user
Browse files Browse the repository at this point in the history
add `user` option to `jupyter_hooks` setting
  • Loading branch information
seeM committed Aug 4, 2022
2 parents a0a2e7e + 9b8765f commit 9953eab
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
19 changes: 13 additions & 6 deletions nbdev/clean.py
Expand Up @@ -122,14 +122,21 @@ def nbdev_clean(
# %% ../nbs/11_clean.ipynb 24
def clean_jupyter(path, model, **kwargs):
"Clean Jupyter `model` pre save to `path`"
get_config.cache_clear() # Reset Jupyter's cache
get_config.cache_clear() # Allow config changes without restarting Jupyter
try: cfg = get_config(path=path)
except FileNotFoundError: return
except FileNotFoundError: cfg = {}

jupyter_hooks = cfg.get('jupyter_hooks', 'user')
try: jupyter_hooks = str2bool(jupyter_hooks)
except ValueError: pass
else:
warn(("Boolean-valued `jupyter_hooks` is deprecated. Use one of `{'user','nbdev','none'} instead.\n"
"See the docs for more: https://nbdev.fast.ai/clean.html#clean_jupyter"), DeprecationWarning)
jupyter_hooks = 'nbdev' if jupyter_hooks else 'none'

if not (model['type']=='notebook' and model['content']['nbformat']==4): return
in_nbdev_repo = 'nbs_path' in cfg
jupyter_hooks = str2bool(cfg.get('jupyter_hooks', True))
if model['type'] != 'notebook': return
is_nb_v4 = model['content']['nbformat'] == 4
if in_nbdev_repo and jupyter_hooks and is_nb_v4: _nbdev_clean(model['content'])
if jupyter_hooks=='user' or (jupyter_hooks=='nbdev' and in_nbdev_repo): _nbdev_clean(model['content'])

# %% ../nbs/11_clean.ipynb 26
@call_parse
Expand Down
27 changes: 19 additions & 8 deletions nbs/11_clean.ipynb
Expand Up @@ -335,23 +335,34 @@
"#|export\n",
"def clean_jupyter(path, model, **kwargs):\n",
" \"Clean Jupyter `model` pre save to `path`\"\n",
" get_config.cache_clear() # Reset Jupyter's cache\n",
" get_config.cache_clear() # Allow config changes without restarting Jupyter\n",
" try: cfg = get_config(path=path)\n",
" except FileNotFoundError: return\n",
" except FileNotFoundError: cfg = {}\n",
"\n",
" jupyter_hooks = cfg.get('jupyter_hooks', 'user')\n",
" try: jupyter_hooks = str2bool(jupyter_hooks)\n",
" except ValueError: pass\n",
" else:\n",
" warn((\"Boolean-valued `jupyter_hooks` is deprecated. Use one of `{'user','nbdev','none'} instead.\\n\"\n",
" \"See the docs for more: https://nbdev.fast.ai/clean.html#clean_jupyter\"), DeprecationWarning)\n",
" jupyter_hooks = 'nbdev' if jupyter_hooks else 'none'\n",
"\n",
" if not (model['type']=='notebook' and model['content']['nbformat']==4): return\n",
" in_nbdev_repo = 'nbs_path' in cfg\n",
" jupyter_hooks = str2bool(cfg.get('jupyter_hooks', True))\n",
" if model['type'] != 'notebook': return\n",
" is_nb_v4 = model['content']['nbformat'] == 4\n",
" if in_nbdev_repo and jupyter_hooks and is_nb_v4: _nbdev_clean(model['content'])"
" if jupyter_hooks=='user' or (jupyter_hooks=='nbdev' and in_nbdev_repo): _nbdev_clean(model['content'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This implements Jupyter's [`ContentsManager.pre_save_hook`](https://jupyter-notebook.readthedocs.io/en/6.4.12/extending/savehooks.html). The easiest way to install it for Jupyter Notebook and Lab is by running `nbdev_install_hooks`.\n",
"This implements a `pre_save_hook` from Jupyter's [file save hook API](https://jupyter-server.readthedocs.io/en/latest/developers/savehooks.html). The easiest way to install it for both Jupyter Notebook and Lab is by running `nbdev_install_hooks`.\n",
"\n",
"The `jupyter_hooks` key in your `settings.ini` controls when hooks are run, accepting:\n",
"\n",
"It only runs on notebooks in an `nbdev` repo. You can disable it for an nbdev repo at any time by setting `jupyter_hooks = False` in `settings.ini`."
"- `nbdev` (default): Only on notebooks in `nbdev` repos\n",
"- `user`: All notebooks for this user\n",
"- `none`: Never"
]
},
{
Expand Down

0 comments on commit 9953eab

Please sign in to comment.