Skip to content

Commit 6312fdb

Browse files
committed
Actually fix #995
1 parent 29c9652 commit 6312fdb

File tree

2 files changed

+11
-16
lines changed

2 files changed

+11
-16
lines changed

nbdev/clean.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def clean_nb(
9393
clean_ids=True, # Remove ids from plaintext reprs?
9494
):
9595
"Clean `nb` from superfluous metadata"
96+
assert isinstance(nb, AttrDict)
9697
metadata_keys = {"kernelspec", "jekyll", "jupytext", "doc", "widgets"}
9798
if allowed_metadata_keys: metadata_keys.update(allowed_metadata_keys)
9899
cell_metadata_keys = {"hide_input"}
@@ -113,7 +114,7 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False):
113114
if isinstance(f_in, (str,Path)): f_in = Path(f_in).open()
114115
try:
115116
_reconfigure(f_in, f_out)
116-
nb = loads(f_in.read())
117+
nb = dict2nb(loads(f_in.read()))
117118
proc_nb(nb)
118119
write_nb(nb, f_out) if not disp else sys.stdout.write(nb2str(nb))
119120
except Exception as e:
@@ -128,7 +129,7 @@ def _nbdev_clean(nb, path=None, clear_all=None):
128129
allowed_cell_metadata_keys = cfg.get("allowed_cell_metadata_keys").split()
129130
return clean_nb(nb, clear_all, allowed_metadata_keys, allowed_cell_metadata_keys, cfg.clean_ids)
130131

131-
# %% ../nbs/api/11_clean.ipynb 31
132+
# %% ../nbs/api/11_clean.ipynb 30
132133
@call_parse
133134
def nbdev_clean(
134135
fname:str=None, # A notebook name or glob to clean
@@ -144,15 +145,15 @@ def nbdev_clean(
144145
if fname is None: fname = get_config().nbs_path
145146
for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re='^[_.]'): _write(f_in=f, disp=disp)
146147

147-
# %% ../nbs/api/11_clean.ipynb 34
148+
# %% ../nbs/api/11_clean.ipynb 33
148149
def clean_jupyter(path, model, **kwargs):
149150
"Clean Jupyter `model` pre save to `path`"
150151
if not (model['type']=='notebook' and model['content']['nbformat']==4): return
151152
get_config.cache_clear() # Allow config changes without restarting Jupyter
152153
jupyter_hooks = get_config(path=path).jupyter_hooks
153154
if jupyter_hooks: _nbdev_clean(model['content'], path=path)
154155

155-
# %% ../nbs/api/11_clean.ipynb 37
156+
# %% ../nbs/api/11_clean.ipynb 36
156157
_pre_save_hook_src = '''
157158
def nbdev_clean_jupyter(**kwargs):
158159
try: from nbdev.clean import clean_jupyter
@@ -162,7 +163,7 @@ def nbdev_clean_jupyter(**kwargs):
162163
c.ContentsManager.pre_save_hook = nbdev_clean_jupyter'''.strip()
163164
_pre_save_hook_re = re.compile(r'c\.(File)?ContentsManager\.pre_save_hook')
164165

165-
# %% ../nbs/api/11_clean.ipynb 38
166+
# %% ../nbs/api/11_clean.ipynb 37
166167
def _add_jupyter_hooks(src, path):
167168
if _pre_save_hook_src in src: return
168169
mod = ast.parse(src)
@@ -180,12 +181,12 @@ def _add_jupyter_hooks(src, path):
180181
if src: src+='\n\n'
181182
return src+_pre_save_hook_src
182183

183-
# %% ../nbs/api/11_clean.ipynb 42
184+
# %% ../nbs/api/11_clean.ipynb 41
184185
def _git_root():
185186
try: return Path(run('git rev-parse --show-toplevel'))
186187
except OSError: return None
187188

188-
# %% ../nbs/api/11_clean.ipynb 45
189+
# %% ../nbs/api/11_clean.ipynb 44
189190
@call_parse
190191
def nbdev_install_hooks():
191192
"Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"

nbs/api/11_clean.ipynb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
" clean_ids=True, # Remove ids from plaintext reprs?\n",
211211
"):\n",
212212
" \"Clean `nb` from superfluous metadata\"\n",
213+
" assert isinstance(nb, AttrDict)\n",
213214
" metadata_keys = {\"kernelspec\", \"jekyll\", \"jupytext\", \"doc\", \"widgets\"}\n",
214215
" if allowed_metadata_keys: metadata_keys.update(allowed_metadata_keys)\n",
215216
" cell_metadata_keys = {\"hide_input\"}\n",
@@ -367,7 +368,7 @@
367368
" if isinstance(f_in, (str,Path)): f_in = Path(f_in).open()\n",
368369
" try:\n",
369370
" _reconfigure(f_in, f_out)\n",
370-
" nb = loads(f_in.read())\n",
371+
" nb = dict2nb(loads(f_in.read()))\n",
371372
" proc_nb(nb)\n",
372373
" write_nb(nb, f_out) if not disp else sys.stdout.write(nb2str(nb))\n",
373374
" except Exception as e:\n",
@@ -390,13 +391,6 @@
390391
" return clean_nb(nb, clear_all, allowed_metadata_keys, allowed_cell_metadata_keys, cfg.clean_ids)"
391392
]
392393
},
393-
{
394-
"cell_type": "code",
395-
"execution_count": null,
396-
"metadata": {},
397-
"outputs": [],
398-
"source": []
399-
},
400394
{
401395
"cell_type": "code",
402396
"execution_count": null,
@@ -859,7 +853,7 @@
859853
"split_at_heading": true
860854
},
861855
"kernelspec": {
862-
"display_name": "torch",
856+
"display_name": "python3",
863857
"language": "python",
864858
"name": "python3"
865859
}

0 commit comments

Comments
 (0)