@@ -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?
@@ -102,12 +97,12 @@ def clean_nb(
10297 nb ['metadata' ]['kernelspec' ]['display_name' ] = nb ["metadata" ]["kernelspec" ]["name" ]
10398 nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
10499
105- # %% ../nbs/api/11_clean.ipynb 27
100+ # %% ../nbs/api/11_clean.ipynb 26
106101def _reconfigure (* strms ):
107102 for s in strms :
108103 if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
109104
110- # %% ../nbs/api/11_clean.ipynb 28
105+ # %% ../nbs/api/11_clean.ipynb 27
111106def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
112107 if not f_out : f_out = f_in
113108 if isinstance (f_in , (str ,Path )): f_in = Path (f_in ).open ()
@@ -120,15 +115,15 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False):
120115 warn (f'{ warn_msg } ' )
121116 warn (e )
122117
123- # %% ../nbs/api/11_clean.ipynb 29
118+ # %% ../nbs/api/11_clean.ipynb 28
124119def _nbdev_clean (nb , path = None , clear_all = None ):
125120 cfg = get_config (path = path )
126121 clear_all = clear_all or cfg .clear_all
127122 allowed_metadata_keys = cfg .get ("allowed_metadata_keys" ).split ()
128123 allowed_cell_metadata_keys = cfg .get ("allowed_cell_metadata_keys" ).split ()
129124 return clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
130125
131- # %% ../nbs/api/11_clean.ipynb 30
126+ # %% ../nbs/api/11_clean.ipynb 29
132127@call_parse
133128def nbdev_clean (
134129 fname :str = None , # A notebook name or glob to clean
@@ -144,15 +139,15 @@ def nbdev_clean(
144139 if fname is None : fname = get_config ().nbs_path
145140 for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
146141
147- # %% ../nbs/api/11_clean.ipynb 33
142+ # %% ../nbs/api/11_clean.ipynb 32
148143def clean_jupyter (path , model , ** kwargs ):
149144 "Clean Jupyter `model` pre save to `path`"
150145 if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
151146 get_config .cache_clear () # Allow config changes without restarting Jupyter
152147 jupyter_hooks = get_config (path = path ).jupyter_hooks
153148 if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
154149
155- # %% ../nbs/api/11_clean.ipynb 36
150+ # %% ../nbs/api/11_clean.ipynb 35
156151_pre_save_hook_src = '''
157152def nbdev_clean_jupyter(**kwargs):
158153 try: from nbdev.clean import clean_jupyter
@@ -162,7 +157,7 @@ def nbdev_clean_jupyter(**kwargs):
162157c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
163158_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
164159
165- # %% ../nbs/api/11_clean.ipynb 37
160+ # %% ../nbs/api/11_clean.ipynb 36
166161def _add_jupyter_hooks (src , path ):
167162 if _pre_save_hook_src in src : return
168163 mod = ast .parse (src )
@@ -180,12 +175,12 @@ def _add_jupyter_hooks(src, path):
180175 if src : src += '\n \n '
181176 return src + _pre_save_hook_src
182177
183- # %% ../nbs/api/11_clean.ipynb 41
178+ # %% ../nbs/api/11_clean.ipynb 40
184179def _git_root ():
185180 try : return Path (run ('git rev-parse --show-toplevel' ))
186181 except OSError : return None
187182
188- # %% ../nbs/api/11_clean.ipynb 44
183+ # %% ../nbs/api/11_clean.ipynb 43
189184@call_parse
190185def nbdev_install_hooks ():
191186 "Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments