Skip to content

Commit 3acb976

Browse files
authored
Merge pull request #1293 from xl0/kernelspec
nbdev_clean: Convert nb to AttrDict before processing
2 parents 29c9652 + 47f0aa3 commit 3acb976

33 files changed

+42
-47
lines changed

nbdev/clean.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def clean_nb(
9393
clean_ids=True, # Remove ids from plaintext reprs?
9494
):
9595
"Clean `nb` from superfluous metadata"
96+
assert isinstance(nb, AttrDict)
9697
metadata_keys = {"kernelspec", "jekyll", "jupytext", "doc", "widgets"}
9798
if allowed_metadata_keys: metadata_keys.update(allowed_metadata_keys)
9899
cell_metadata_keys = {"hide_input"}
@@ -113,7 +114,7 @@ def process_write(warn_msg, proc_nb, f_in, f_out=None, disp=False):
113114
if isinstance(f_in, (str,Path)): f_in = Path(f_in).open()
114115
try:
115116
_reconfigure(f_in, f_out)
116-
nb = loads(f_in.read())
117+
nb = dict2nb(loads(f_in.read()))
117118
proc_nb(nb)
118119
write_nb(nb, f_out) if not disp else sys.stdout.write(nb2str(nb))
119120
except Exception as e:
@@ -128,7 +129,7 @@ def _nbdev_clean(nb, path=None, clear_all=None):
128129
allowed_cell_metadata_keys = cfg.get("allowed_cell_metadata_keys").split()
129130
return clean_nb(nb, clear_all, allowed_metadata_keys, allowed_cell_metadata_keys, cfg.clean_ids)
130131

131-
# %% ../nbs/api/11_clean.ipynb 31
132+
# %% ../nbs/api/11_clean.ipynb 30
132133
@call_parse
133134
def nbdev_clean(
134135
fname:str=None, # A notebook name or glob to clean
@@ -144,15 +145,15 @@ def nbdev_clean(
144145
if fname is None: fname = get_config().nbs_path
145146
for f in globtastic(fname, file_glob='*.ipynb', skip_folder_re='^[_.]'): _write(f_in=f, disp=disp)
146147

147-
# %% ../nbs/api/11_clean.ipynb 34
148+
# %% ../nbs/api/11_clean.ipynb 33
148149
def clean_jupyter(path, model, **kwargs):
149150
"Clean Jupyter `model` pre save to `path`"
150151
if not (model['type']=='notebook' and model['content']['nbformat']==4): return
151152
get_config.cache_clear() # Allow config changes without restarting Jupyter
152153
jupyter_hooks = get_config(path=path).jupyter_hooks
153154
if jupyter_hooks: _nbdev_clean(model['content'], path=path)
154155

155-
# %% ../nbs/api/11_clean.ipynb 37
156+
# %% ../nbs/api/11_clean.ipynb 36
156157
_pre_save_hook_src = '''
157158
def nbdev_clean_jupyter(**kwargs):
158159
try: from nbdev.clean import clean_jupyter
@@ -162,7 +163,7 @@ def nbdev_clean_jupyter(**kwargs):
162163
c.ContentsManager.pre_save_hook = nbdev_clean_jupyter'''.strip()
163164
_pre_save_hook_re = re.compile(r'c\.(File)?ContentsManager\.pre_save_hook')
164165

165-
# %% ../nbs/api/11_clean.ipynb 38
166+
# %% ../nbs/api/11_clean.ipynb 37
166167
def _add_jupyter_hooks(src, path):
167168
if _pre_save_hook_src in src: return
168169
mod = ast.parse(src)
@@ -180,12 +181,12 @@ def _add_jupyter_hooks(src, path):
180181
if src: src+='\n\n'
181182
return src+_pre_save_hook_src
182183

183-
# %% ../nbs/api/11_clean.ipynb 42
184+
# %% ../nbs/api/11_clean.ipynb 41
184185
def _git_root():
185186
try: return Path(run('git rev-parse --show-toplevel'))
186187
except OSError: return None
187188

188-
# %% ../nbs/api/11_clean.ipynb 45
189+
# %% ../nbs/api/11_clean.ipynb 44
189190
@call_parse
190191
def nbdev_install_hooks():
191192
"Install Jupyter and git hooks to automatically clean, trust, and fix merge conflicts in notebooks"

nbs/api/01_config.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@
794794
],
795795
"metadata": {
796796
"kernelspec": {
797-
"display_name": "Python 3 (ipykernel)",
797+
"display_name": "python3",
798798
"language": "python",
799799
"name": "python3"
800800
}

nbs/api/02_maker.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@
799799
],
800800
"metadata": {
801801
"kernelspec": {
802-
"display_name": "Python 3 (ipykernel)",
802+
"display_name": "python3",
803803
"language": "python",
804804
"name": "python3"
805805
}

nbs/api/03_process.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@
656656
],
657657
"metadata": {
658658
"kernelspec": {
659-
"display_name": "Python 3 (ipykernel)",
659+
"display_name": "python3",
660660
"language": "python",
661661
"name": "python3"
662662
}

nbs/api/04_export.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@
248248
],
249249
"metadata": {
250250
"kernelspec": {
251-
"display_name": "Python 3 (ipykernel)",
251+
"display_name": "python3",
252252
"language": "python",
253253
"name": "python3"
254254
}

nbs/api/05_doclinks.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@
809809
],
810810
"metadata": {
811811
"kernelspec": {
812-
"display_name": "Python 3",
812+
"display_name": "python3",
813813
"language": "python",
814814
"name": "python3"
815815
}

nbs/api/06_sync.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@
236236
],
237237
"metadata": {
238238
"kernelspec": {
239-
"display_name": "Python 3 (ipykernel)",
239+
"display_name": "python3",
240240
"language": "python",
241241
"name": "python3"
242242
}

nbs/api/07_merge.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@
635635
"split_at_heading": true
636636
},
637637
"kernelspec": {
638-
"display_name": "Python 3 (ipykernel)",
638+
"display_name": "python3",
639639
"language": "python",
640640
"name": "python3"
641641
}

nbs/api/08_showdoc.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@
13651365
],
13661366
"metadata": {
13671367
"kernelspec": {
1368-
"display_name": "Python 3 (ipykernel)",
1368+
"display_name": "python3",
13691369
"language": "python",
13701370
"name": "python3"
13711371
}

nbs/api/09_frontmatter.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@
275275
],
276276
"metadata": {
277277
"kernelspec": {
278-
"display_name": "Python 3 (ipykernel)",
278+
"display_name": "python3",
279279
"language": "python",
280280
"name": "python3"
281281
}

0 commit comments

Comments
 (0)