@@ -55,20 +55,25 @@ def _clean_cell_output_id(lines):
55
55
return _skip_or_sub (lines ) if isinstance (lines ,str ) else [_skip_or_sub (o ) for o in lines ]
56
56
57
57
# %% ../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
58
63
def _clean_cell_output (cell , clean_ids ):
59
64
"Remove `cell` output execution count and optionally ids from text reprs"
60
65
outputs = cell .get ('outputs' , [])
61
66
for o in outputs :
62
67
if 'execution_count' in o : o ['execution_count' ] = None
63
68
data = o .get ('data' , {})
64
69
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' ])
69
74
o .get ('metadata' , {}).pop ('tags' , None )
70
75
71
- # %% ../nbs/api/11_clean.ipynb 12
76
+ # %% ../nbs/api/11_clean.ipynb 13
72
77
def _clean_cell (cell , clear_all , allowed_metadata_keys , clean_ids ):
73
78
"Clean `cell` by removing superfluous metadata or everything except the input if `clear_all`"
74
79
if 'execution_count' in cell : cell ['execution_count' ] = None
@@ -79,7 +84,7 @@ def _clean_cell(cell, clear_all, allowed_metadata_keys, clean_ids):
79
84
cell ['metadata' ] = {} if clear_all else {
80
85
k :v for k ,v in cell ['metadata' ].items () if k in allowed_metadata_keys }
81
86
82
- # %% ../nbs/api/11_clean.ipynb 13
87
+ # %% ../nbs/api/11_clean.ipynb 14
83
88
def clean_nb (
84
89
nb , # The notebook to clean
85
90
clear_all = False , # Remove all cell metadata and cell outputs?
@@ -97,12 +102,12 @@ def clean_nb(
97
102
nb ['metadata' ]['kernelspec' ]['display_name' ] = nb .metadata .kernelspec .name
98
103
nb ['metadata' ] = {k :v for k ,v in nb ['metadata' ].items () if k in metadata_keys }
99
104
100
- # %% ../nbs/api/11_clean.ipynb 24
105
+ # %% ../nbs/api/11_clean.ipynb 27
101
106
def _reconfigure (* strms ):
102
107
for s in strms :
103
108
if hasattr (s ,'reconfigure' ): s .reconfigure (encoding = 'utf-8' )
104
109
105
- # %% ../nbs/api/11_clean.ipynb 25
110
+ # %% ../nbs/api/11_clean.ipynb 28
106
111
def process_write (warn_msg , proc_nb , f_in , f_out = None , disp = False ):
107
112
if not f_out : f_out = f_in
108
113
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):
115
120
warn (f'{ warn_msg } ' )
116
121
warn (e )
117
122
118
- # %% ../nbs/api/11_clean.ipynb 26
123
+ # %% ../nbs/api/11_clean.ipynb 29
119
124
def _nbdev_clean (nb , path = None , clear_all = None ):
120
125
cfg = get_config (path = path )
121
126
clear_all = clear_all or cfg .clear_all
122
127
allowed_metadata_keys = cfg .get ("allowed_metadata_keys" ).split ()
123
128
allowed_cell_metadata_keys = cfg .get ("allowed_cell_metadata_keys" ).split ()
124
129
return clean_nb (nb , clear_all , allowed_metadata_keys , allowed_cell_metadata_keys , cfg .clean_ids )
125
130
126
- # %% ../nbs/api/11_clean.ipynb 27
131
+ # %% ../nbs/api/11_clean.ipynb 31
127
132
@call_parse
128
133
def nbdev_clean (
129
134
fname :str = None , # A notebook name or glob to clean
@@ -139,15 +144,15 @@ def nbdev_clean(
139
144
if fname is None : fname = get_config ().nbs_path
140
145
for f in globtastic (fname , file_glob = '*.ipynb' , skip_folder_re = '^[_.]' ): _write (f_in = f , disp = disp )
141
146
142
- # %% ../nbs/api/11_clean.ipynb 30
147
+ # %% ../nbs/api/11_clean.ipynb 34
143
148
def clean_jupyter (path , model , ** kwargs ):
144
149
"Clean Jupyter `model` pre save to `path`"
145
150
if not (model ['type' ]== 'notebook' and model ['content' ]['nbformat' ]== 4 ): return
146
151
get_config .cache_clear () # Allow config changes without restarting Jupyter
147
152
jupyter_hooks = get_config (path = path ).jupyter_hooks
148
153
if jupyter_hooks : _nbdev_clean (model ['content' ], path = path )
149
154
150
- # %% ../nbs/api/11_clean.ipynb 33
155
+ # %% ../nbs/api/11_clean.ipynb 37
151
156
_pre_save_hook_src = '''
152
157
def nbdev_clean_jupyter(**kwargs):
153
158
try: from nbdev.clean import clean_jupyter
@@ -157,7 +162,7 @@ def nbdev_clean_jupyter(**kwargs):
157
162
c.ContentsManager.pre_save_hook = nbdev_clean_jupyter''' .strip ()
158
163
_pre_save_hook_re = re .compile (r'c\.(File)?ContentsManager\.pre_save_hook' )
159
164
160
- # %% ../nbs/api/11_clean.ipynb 34
165
+ # %% ../nbs/api/11_clean.ipynb 38
161
166
def _add_jupyter_hooks (src , path ):
162
167
if _pre_save_hook_src in src : return
163
168
mod = ast .parse (src )
@@ -175,12 +180,12 @@ def _add_jupyter_hooks(src, path):
175
180
if src : src += '\n \n '
176
181
return src + _pre_save_hook_src
177
182
178
- # %% ../nbs/api/11_clean.ipynb 38
183
+ # %% ../nbs/api/11_clean.ipynb 42
179
184
def _git_root ():
180
185
try : return Path (run ('git rev-parse --show-toplevel' ))
181
186
except OSError : return None
182
187
183
- # %% ../nbs/api/11_clean.ipynb 41
188
+ # %% ../nbs/api/11_clean.ipynb 45
184
189
@call_parse
185
190
def nbdev_install_hooks ():
186
191
"Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"
0 commit comments