@@ -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
133134def 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
148149def 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 = '''
157158def nbdev_clean_jupyter(**kwargs):
158159 try: from nbdev.clean import clean_jupyter
@@ -162,7 +163,7 @@ def nbdev_clean_jupyter(**kwargs):
162163c.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
166167def _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
184185def _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
190191def nbdev_install_hooks ():
191192 "Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments