@@ -55,11 +55,6 @@ def _clean_cell_output_id(lines):
5555 return _skip_or_sub (lines ) if isinstance (lines ,str ) else [_skip_or_sub (o ) for o in lines ]
5656
5757# %% ../nbs/api/11_clean.ipynb 11
58- def _add_trailing_n (img ):
59- if not isinstance (img ,str ): return [ _add_trailing_n (o ) for o in img ]
60- return img + "\n " if img [- 1 ] != "\n " else img
61-
62- # %% ../nbs/api/11_clean.ipynb 12
6358def _clean_cell_output (cell , clean_ids ):
6459 "Remove `cell` output execution count and optionally ids from text reprs"
6560 outputs = cell .get ('outputs' , [])
@@ -69,11 +64,11 @@ def _clean_cell_output(cell, clean_ids):
6964 data .pop ("application/vnd.google.colaboratory.intrinsic+json" , None )
7065 for k in data :
7166 if k .startswith ('text' ) and clean_ids : data [k ] = _clean_cell_output_id (data [k ])
72- if k .startswith ('image' ): data [k ] = _add_trailing_n ( data [k ])
67+ if k .startswith ('image' ) and "svg" not in k : data [k ] = data [k ]. rstrip ( )
7368 if 'text' in o and clean_ids : o ['text' ] = _clean_cell_output_id (o ['text' ])
7469 o .get ('metadata' , {}).pop ('tags' , None )
7570
76- # %% ../nbs/api/11_clean.ipynb 13
71+ # %% ../nbs/api/11_clean.ipynb 12
7772def _clean_cell (cell , clear_all , allowed_metadata_keys , clean_ids ):
7873 "Clean `cell` by removing superfluous metadata or everything except the input if `clear_all`"
7974 if 'execution_count' in cell : cell ['execution_count' ] = None
@@ -84,7 +79,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids):
8479 cell ['metadata' ] = {} if clear_all else {
8580 k :v for k ,v in cell ['metadata' ].items () if k in allowed_metadata_keys }
8681
87- # %% ../nbs/api/11_clean.ipynb 14
82+ # %% ../nbs/api/11_clean.ipynb 13
8883def clean_nb (
8984 nb , # The notebook to clean
9085 clear_all = False , # Remove all cell metadata and cell outputs?
@@ -93,43 +88,42 @@ def clean_nb(
9388 clean_ids = True , # Remove ids from plaintext reprs?
9489):
9590 "Clean `nb` from superfluous metadata"
96- assert isinstance (nb , AttrDict )
9791 metadata_keys = {"kernelspec" , "jekyll" , "jupytext" , "doc" , "widgets" }
9892 if allowed_metadata_keys : metadata_keys .update (allowed_metadata_keys )
9993 cell_metadata_keys = {"hide_input" }
10094 if allowed_cell_metadata_keys : cell_metadata_keys .update (allowed_cell_metadata_keys )
10195 for c in nb ['cells' ]: _clean_cell (c , clear_all , cell_metadata_keys , clean_ids )
102- if nested_attr ( nb , 'metadata. kernelspec. name' ):
103- nb ['metadata' ]['kernelspec' ]['display_name' ] = nb . metadata . kernelspec . name
96+ if nb . get ( 'metadata' , {}). get ( ' kernelspec' , {}). get ( ' name', None ):
97+ nb ['metadata' ]['kernelspec' ]['display_name' ] = nb [ " metadata" ][ " kernelspec" ][ " name" ]
10498 nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
10599
106- # %% ../nbs/api/11_clean.ipynb 27
100+ # %% ../nbs/api/11_clean.ipynb 26
107101def _reconfigure (* strms ):
108102 for s in strms :
109103 if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
110104
111- # %% ../nbs/api/11_clean.ipynb 28
105+ # %% ../nbs/api/11_clean.ipynb 27
112106def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
113107 if not f_out : f_out = f_in
114- if isinstance (f_in , (str ,Path )): f_in = Path (f_in ).open ()
108+ if isinstance (f_in , (str ,Path )): f_in = Path (f_in ).open (encoding = "utf-8" )
115109 try :
116110 _reconfigure (f_in , f_out )
117- nb = dict2nb ( loads (f_in .read () ))
111+ nb = loads (f_in .read ())
118112 proc_nb (nb )
119113 write_nb (nb , f_out ) if not disp else sys .stdout .write (nb2str (nb ))
120114 except Exception as e :
121115 warn (f'{ warn_msg } ' )
122116 warn (e )
123117
124- # %% ../nbs/api/11_clean.ipynb 29
118+ # %% ../nbs/api/11_clean.ipynb 28
125119def _nbdev_clean (nb , path = None , clear_all = None ):
126120 cfg = get_config (path = path )
127121 clear_all = clear_all or cfg .clear_all
128122 allowed_metadata_keys = cfg .get ("allowed_metadata_keys" ).split ()
129123 allowed_cell_metadata_keys = cfg .get ("allowed_cell_metadata_keys" ).split ()
130124 return clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
131125
132- # %% ../nbs/api/11_clean.ipynb 30
126+ # %% ../nbs/api/11_clean.ipynb 29
133127@call_parse
134128def nbdev_clean (
135129 fname :str = None , # A notebook name or glob to clean
@@ -145,15 +139,15 @@ def nbdev_clean(
145139 if fname is None : fname = get_config ().nbs_path
146140 for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
147141
148- # %% ../nbs/api/11_clean.ipynb 33
142+ # %% ../nbs/api/11_clean.ipynb 32
149143def clean_jupyter (path , model , ** kwargs ):
150144 "Clean Jupyter `model` pre save to `path`"
151145 if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
152146 get_config .cache_clear () # Allow config changes without restarting Jupyter
153147 jupyter_hooks = get_config (path = path ).jupyter_hooks
154148 if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
155149
156- # %% ../nbs/api/11_clean.ipynb 36
150+ # %% ../nbs/api/11_clean.ipynb 35
157151_pre_save_hook_src = '''
158152def nbdev_clean_jupyter(**kwargs):
159153 try: from nbdev.clean import clean_jupyter
@@ -163,7 +157,7 @@ def nbdev_clean_jupyter(**kwargs):
163157c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
164158_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
165159
166- # %% ../nbs/api/11_clean.ipynb 37
160+ # %% ../nbs/api/11_clean.ipynb 36
167161def _add_jupyter_hooks (src , path ):
168162 if _pre_save_hook_src in src : return
169163 mod = ast .parse (src )
@@ -181,12 +175,12 @@ def _add_jupyter_hooks(src, path):
181175 if src : src += '\n \n '
182176 return src + _pre_save_hook_src
183177
184- # %% ../nbs/api/11_clean.ipynb 41
178+ # %% ../nbs/api/11_clean.ipynb 40
185179def _git_root ():
186180 try : return Path (run ('git rev-parse --show-toplevel' ))
187181 except OSError : return None
188182
189- # %% ../nbs/api/11_clean.ipynb 44
183+ # %% ../nbs/api/11_clean.ipynb 43
190184@call_parse
191185def nbdev_install_hooks ():
192186 "Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments