@@ -55,20 +55,25 @@ 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
5863def _clean_cell_output (cell , clean_ids ):
5964 "Remove `cell` output execution count and optionally ids from text reprs"
6065 outputs = cell .get ('outputs' , [])
6166 for o in outputs :
6267 if 'execution_count' in o : o ['execution_count' ] = None
6368 data = o .get ('data' , {})
6469 data .pop ("application/vnd.google.colaboratory.intrinsic+json" , None )
65- if clean_ids :
66- for k in data :
67- if k .startswith ('text ' ): data [k ] = _clean_cell_output_id (data [k ])
68- if 'text' in o : o ['text' ] = _clean_cell_output_id (o ['text' ])
70+ for k in data :
71+ 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 ])
73+ if 'text' in o and clean_ids : o ['text' ] = _clean_cell_output_id (o ['text' ])
6974 o .get ('metadata' , {}).pop ('tags' , None )
7075
71- # %% ../nbs/api/11_clean.ipynb 12
76+ # %% ../nbs/api/11_clean.ipynb 13
7277def _clean_cell (cell , clear_all , allowed_metadata_keys , clean_ids ):
7378 "Clean `cell` by removing superfluous metadata or everything except the input if `clear_all`"
7479 if 'execution_count' in cell : cell ['execution_count' ] = None
@@ -79,7 +84,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids):
7984 cell ['metadata' ] = {} if clear_all else {
8085 k :v for k ,v in cell ['metadata' ].items () if k in allowed_metadata_keys }
8186
82- # %% ../nbs/api/11_clean.ipynb 13
87+ # %% ../nbs/api/11_clean.ipynb 14
8388def clean_nb (
8489 nb , # The notebook to clean
8590 clear_all = False , # Remove all cell metadata and cell outputs?
@@ -97,12 +102,12 @@ def clean_nb(
97102 nb ['metadata' ]['kernelspec' ]['display_name' ] = nb .metadata .kernelspec .name
98103 nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
99104
100- # %% ../nbs/api/11_clean.ipynb 24
105+ # %% ../nbs/api/11_clean.ipynb 27
101106def _reconfigure (* strms ):
102107 for s in strms :
103108 if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
104109
105- # %% ../nbs/api/11_clean.ipynb 25
110+ # %% ../nbs/api/11_clean.ipynb 28
106111def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
107112 if not f_out : f_out = f_in
108113 if isinstance (f_in , (str ,Path )): f_in = Path (f_in ).open ()
@@ -115,15 +120,15 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False):
115120 warn (f'{ warn_msg } ' )
116121 warn (e )
117122
118- # %% ../nbs/api/11_clean.ipynb 26
123+ # %% ../nbs/api/11_clean.ipynb 29
119124def _nbdev_clean (nb , path = None , clear_all = None ):
120125 cfg = get_config (path = path )
121126 clear_all = clear_all or cfg .clear_all
122127 allowed_metadata_keys = cfg .get ("allowed_metadata_keys" ).split ()
123128 allowed_cell_metadata_keys = cfg .get ("allowed_cell_metadata_keys" ).split ()
124129 return clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
125130
126- # %% ../nbs/api/11_clean.ipynb 27
131+ # %% ../nbs/api/11_clean.ipynb 31
127132@call_parse
128133def nbdev_clean (
129134 fname :str = None , # A notebook name or glob to clean
@@ -139,15 +144,15 @@ def nbdev_clean(
139144 if fname is None : fname = get_config ().nbs_path
140145 for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
141146
142- # %% ../nbs/api/11_clean.ipynb 30
147+ # %% ../nbs/api/11_clean.ipynb 34
143148def clean_jupyter (path , model , ** kwargs ):
144149 "Clean Jupyter `model` pre save to `path`"
145150 if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
146151 get_config .cache_clear () # Allow config changes without restarting Jupyter
147152 jupyter_hooks = get_config (path = path ).jupyter_hooks
148153 if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
149154
150- # %% ../nbs/api/11_clean.ipynb 33
155+ # %% ../nbs/api/11_clean.ipynb 37
151156_pre_save_hook_src = '''
152157def nbdev_clean_jupyter(**kwargs):
153158 try: from nbdev.clean import clean_jupyter
@@ -157,7 +162,7 @@ def nbdev_clean_jupyter(**kwargs):
157162c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
158163_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
159164
160- # %% ../nbs/api/11_clean.ipynb 34
165+ # %% ../nbs/api/11_clean.ipynb 38
161166def _add_jupyter_hooks (src , path ):
162167 if _pre_save_hook_src in src : return
163168 mod = ast .parse (src )
@@ -175,12 +180,12 @@ def _add_jupyter_hooks(src, path):
175180 if src : src += '\n \n '
176181 return src + _pre_save_hook_src
177182
178- # %% ../nbs/api/11_clean.ipynb 38
183+ # %% ../nbs/api/11_clean.ipynb 42
179184def _git_root ():
180185 try : return Path (run ('git rev-parse --show-toplevel' ))
181186 except OSError : return None
182187
183- # %% ../nbs/api/11_clean.ipynb 41
188+ # %% ../nbs/api/11_clean.ipynb 45
184189@call_parse
185190def nbdev_install_hooks ():
186191 "Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments