2
2
3
3
# %% auto 0
4
4
__all__ = ['DEFAULT_FM_KEYS' , 'add_links' , 'strip_ansi' , 'hide_' , 'hide_line' , 'filter_stream_' , 'clean_magics' , 'lang_identify' ,
5
- 'rm_header_dash' , 'rm_export' , 'clean_show_doc' , 'remove_widget_state ' , 'exec_show_docs ' , 'insert_warning ' ,
6
- 'add_show_docs' , ' is_frontmatter' , 'nb_fmdict' , 'construct_fm' , 'insert_frontmatter' , 'infer_frontmatter' ]
5
+ 'rm_header_dash' , 'rm_export' , 'clean_show_doc' , 'exec_show_docs ' , 'insert_warning ' , 'add_show_docs ' ,
6
+ 'is_frontmatter' , 'nb_fmdict' , 'construct_fm' , 'insert_frontmatter' , 'infer_frontmatter' ]
7
7
8
8
# %% ../nbs/09_processors.ipynb 2
9
9
import ast
@@ -104,13 +104,6 @@ def clean_show_doc(cell):
104
104
cell .source = '#| echo: false\n ' + cell .source
105
105
106
106
# %% ../nbs/09_processors.ipynb 30
107
- def remove_widget_state (cell ):
108
- "Remove widgets in the output of `cells`"
109
- if cell ['cell_type' ] == 'code' and 'outputs' in cell :
110
- cell ['outputs' ] = [l for l in cell ['outputs' ]
111
- if not ('data' in l and 'application/vnd.jupyter.widget-view+json' in l .data )]
112
-
113
- # %% ../nbs/09_processors.ipynb 32
114
107
_imps = {ast .Import , ast .ImportFrom }
115
108
116
109
def _show_docs (trees ):
@@ -123,7 +116,7 @@ def _do_eval(cell):
123
116
if cell .directives_ .keys () & _exp_dirs or filter_ex (trees , risinstance (_imps )): return True
124
117
if _show_docs (trees ): return True
125
118
126
- # %% ../nbs/09_processors.ipynb 33
119
+ # %% ../nbs/09_processors.ipynb 31
127
120
class exec_show_docs :
128
121
"Execute cells needed for `show_docs` output, including exported cells and imports"
129
122
def __init__ (self , nb ):
@@ -134,13 +127,13 @@ def __call__(self, cell):
134
127
if _do_eval (cell ): self .k .cell (cell )
135
128
if self .k .exc : raise self .k .exc [1 ] from None
136
129
137
- # %% ../nbs/09_processors.ipynb 36
130
+ # %% ../nbs/09_processors.ipynb 34
138
131
def insert_warning (nb ):
139
132
"Insert Autogenerated Warning Into Notebook after the first cell."
140
133
content = "<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->"
141
134
nb .cells .insert (1 , mk_cell (content , False ))
142
135
143
- # %% ../nbs/09_processors.ipynb 40
136
+ # %% ../nbs/09_processors.ipynb 38
144
137
_def_types = (ast .FunctionDef ,ast .AsyncFunctionDef ,ast .ClassDef )
145
138
def _def_names (cell , shown ):
146
139
return [showdoc_nm (o ) for o in concat (cell .parsed_ ()) if isinstance (o ,_def_types ) and o .name not in shown and o .name [0 ]!= '_' ]
@@ -151,7 +144,7 @@ def _get_nm(tree):
151
144
i = tree .value .args [0 ]
152
145
return f'{ i .value .id } .{ i .attr } ' if isinstance (i , ast .Attribute ) else i .id
153
146
154
- # %% ../nbs/09_processors.ipynb 41
147
+ # %% ../nbs/09_processors.ipynb 39
155
148
def add_show_docs (nb ):
156
149
"Add show_doc cells after exported cells, unless they are already documented"
157
150
exports = L (cell for cell in nb .cells if cell .source and _re_exps (cell .source ))
@@ -162,7 +155,7 @@ def add_show_docs(nb):
162
155
code = f'show_doc({ nm } )'
163
156
nb .cells .insert (cell .idx_ + 1 , mk_cell (code ))
164
157
165
- # %% ../nbs/09_processors.ipynb 45
158
+ # %% ../nbs/09_processors.ipynb 43
166
159
_re_title = re .compile (r'^#\s+(.*)[\n\r]+(?:^>\s+(.*))?' , flags = re .MULTILINE )
167
160
_re_fm = re .compile (r'^---.*\S+.*---' , flags = re .DOTALL )
168
161
_re_defaultexp = re .compile (r'^\s*#\|\s*default_exp\s+(\S+)' , flags = re .MULTILINE )
@@ -173,14 +166,14 @@ def _istitle(cell):
173
166
txt = cell .get ('source' , '' )
174
167
return bool (_re_title .search (txt )) if txt else False
175
168
176
- # %% ../nbs/09_processors.ipynb 46
169
+ # %% ../nbs/09_processors.ipynb 44
177
170
def _default_exp (nb ):
178
171
"get the default_exp from a notebook"
179
172
code_src = nb .cells .filter (lambda x : x .cell_type == 'code' ).attrgot ('source' )
180
173
default_exp = first (code_src .filter ().map (_re_defaultexp .search ).filter ())
181
174
return default_exp .group (1 ) if default_exp else None
182
175
183
- # %% ../nbs/09_processors.ipynb 48
176
+ # %% ../nbs/09_processors.ipynb 46
184
177
def nb_fmdict (nb , remove = True ):
185
178
"Infer the front matter from a notebook's markdown formatting"
186
179
md_cells = _celltyp (nb , 'markdown' ).filter (_istitle )
@@ -196,21 +189,21 @@ def nb_fmdict(nb, remove=True):
196
189
return flags
197
190
else : return {}
198
191
199
- # %% ../nbs/09_processors.ipynb 51
192
+ # %% ../nbs/09_processors.ipynb 49
200
193
DEFAULT_FM_KEYS = ['title' , 'description' , 'author' , 'image' , 'categories' , 'output-file' , 'aliases' ]
201
194
202
195
def construct_fm (fmdict :dict , keys = DEFAULT_FM_KEYS ):
203
196
"construct front matter from a dictionary, but only for `keys`"
204
197
if not fmdict : return None
205
198
return '---\n ' + '\n ' .join ([f"{ k } : { fmdict [k ]} " for k in keys if k in fmdict ])+ '\n ---'
206
199
207
- # %% ../nbs/09_processors.ipynb 53
200
+ # %% ../nbs/09_processors.ipynb 51
208
201
def insert_frontmatter (nb , fm_dict :dict , filter_keys :list = DEFAULT_FM_KEYS ):
209
202
"Add frontmatter into notebook based on `filter_keys` that exist in `fmdict`."
210
203
fm = construct_fm (fm_dict , keys = filter_keys )
211
204
if fm : nb .cells .insert (0 , NbCell (0 , dict (cell_type = 'raw' , metadata = {}, source = fm )))
212
205
213
- # %% ../nbs/09_processors.ipynb 54
206
+ # %% ../nbs/09_processors.ipynb 52
214
207
def infer_frontmatter (nb ):
215
208
"Insert front matter if it doesn't exist automatically from nbdev styled markdown."
216
209
if is_frontmatter (nb ): return
0 commit comments