Skip to content

Commit b6cf608

Browse files
committed
Refactor quarto.nbdev_readme; add docs
1 parent 1238c7b commit b6cf608

File tree

3 files changed

+232
-71
lines changed

3 files changed

+232
-71
lines changed

nbdev/_modidx.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,20 @@
231231
'nbdev.qmd.meta': ('api/qmd.html#meta', 'nbdev/qmd.py'),
232232
'nbdev.qmd.tbl_row': ('api/qmd.html#tbl_row', 'nbdev/qmd.py'),
233233
'nbdev.qmd.tbl_sep': ('api/qmd.html#tbl_sep', 'nbdev/qmd.py')},
234-
'nbdev.quarto': { 'nbdev.quarto._copytree': ('api/quarto.html#_copytree', 'nbdev/quarto.py'),
234+
'nbdev.quarto': { 'nbdev.quarto.SidebarYmlRemoved': ('api/quarto.html#sidebarymlremoved', 'nbdev/quarto.py'),
235+
'nbdev.quarto.SidebarYmlRemoved.__enter__': ( 'api/quarto.html#sidebarymlremoved.__enter__',
236+
'nbdev/quarto.py'),
237+
'nbdev.quarto.SidebarYmlRemoved.__exit__': ('api/quarto.html#sidebarymlremoved.__exit__', 'nbdev/quarto.py'),
238+
'nbdev.quarto.SidebarYmlRemoved.__init__': ('api/quarto.html#sidebarymlremoved.__init__', 'nbdev/quarto.py'),
239+
'nbdev.quarto._copytree': ('api/quarto.html#_copytree', 'nbdev/quarto.py'),
235240
'nbdev.quarto._ensure_quarto': ('api/quarto.html#_ensure_quarto', 'nbdev/quarto.py'),
236241
'nbdev.quarto._install_linux': ('api/quarto.html#_install_linux', 'nbdev/quarto.py'),
237242
'nbdev.quarto._install_mac': ('api/quarto.html#_install_mac', 'nbdev/quarto.py'),
238243
'nbdev.quarto._nbglob_docs': ('api/quarto.html#_nbglob_docs', 'nbdev/quarto.py'),
239244
'nbdev.quarto._pre': ('api/quarto.html#_pre', 'nbdev/quarto.py'),
240245
'nbdev.quarto._pre_docs': ('api/quarto.html#_pre_docs', 'nbdev/quarto.py'),
246+
'nbdev.quarto._readme_not_out_of_date': ('api/quarto.html#_readme_not_out_of_date', 'nbdev/quarto.py'),
247+
'nbdev.quarto._save_cached_readme': ('api/quarto.html#_save_cached_readme', 'nbdev/quarto.py'),
241248
'nbdev.quarto._sort': ('api/quarto.html#_sort', 'nbdev/quarto.py'),
242249
'nbdev.quarto._sprun': ('api/quarto.html#_sprun', 'nbdev/quarto.py'),
243250
'nbdev.quarto.fs_watchdog': ('api/quarto.html#fs_watchdog', 'nbdev/quarto.py'),

nbdev/quarto.py

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/14_quarto.ipynb.
22

3-
# %% ../nbs/api/14_quarto.ipynb 2
3+
# %% ../nbs/api/14_quarto.ipynb 3
44
from __future__ import annotations
55
import subprocess,sys,shutil,ast,warnings,traceback
66
from os import system
@@ -18,14 +18,14 @@
1818

1919
# %% auto 0
2020
__all__ = ['BASE_QUARTO_URL', 'install_quarto', 'install', 'nbdev_sidebar', 'refresh_quarto_yml', 'nbdev_proc_nbs',
21-
'nbdev_readme', 'nbdev_docs', 'prepare', 'fs_watchdog', 'nbdev_preview']
21+
'SidebarYmlRemoved', 'nbdev_readme', 'nbdev_docs', 'prepare', 'fs_watchdog', 'nbdev_preview']
2222

23-
# %% ../nbs/api/14_quarto.ipynb 4
23+
# %% ../nbs/api/14_quarto.ipynb 5
2424
def _sprun(cmd):
2525
try: subprocess.check_output(cmd, shell=True)
2626
except subprocess.CalledProcessError as cpe: sys.exit(cpe.returncode)
2727

28-
# %% ../nbs/api/14_quarto.ipynb 6
28+
# %% ../nbs/api/14_quarto.ipynb 7
2929
BASE_QUARTO_URL='https://www.quarto.org/download/latest/'
3030

3131
def _install_linux():
@@ -50,15 +50,15 @@ def install_quarto():
5050
elif 'linux' in sys.platform: _install_linux()
5151
finally: system('sudo rm -f .installing')
5252

53-
# %% ../nbs/api/14_quarto.ipynb 7
53+
# %% ../nbs/api/14_quarto.ipynb 8
5454
@call_parse
5555
def install():
5656
"Install Quarto and the current library"
5757
install_quarto.__wrapped__()
5858
d = get_config().lib_path
5959
if (d/'__init__.py').exists(): system(f'pip install -e "{d.parent}[dev]"')
6060

61-
# %% ../nbs/api/14_quarto.ipynb 9
61+
# %% ../nbs/api/14_quarto.ipynb 10
6262
def _pre(p,b=True): return ' ' * (len(p.parts)) + ('- ' if b else ' ')
6363
def _sort(a):
6464
x,y = a
@@ -75,7 +75,7 @@ def _nbglob_docs(
7575
**kwargs):
7676
return nbglob(path, file_glob=file_glob, file_re=file_re, **kwargs)
7777

78-
# %% ../nbs/api/14_quarto.ipynb 10
78+
# %% ../nbs/api/14_quarto.ipynb 11
7979
@call_parse
8080
@delegates(_nbglob_docs)
8181
def nbdev_sidebar(
@@ -108,7 +108,7 @@ def _f(a,b): return Path(a),b
108108
if printit: return print(yml)
109109
yml_path.write_text(yml)
110110

111-
# %% ../nbs/api/14_quarto.ipynb 13
111+
# %% ../nbs/api/14_quarto.ipynb 14
112112
_quarto_yml="""project:
113113
type: website
114114
@@ -130,7 +130,7 @@ def _f(a,b): return Path(a),b
130130
131131
metadata-files: [nbdev.yml, sidebar.yml]"""
132132

133-
# %% ../nbs/api/14_quarto.ipynb 14
133+
# %% ../nbs/api/14_quarto.ipynb 15
134134
_nbdev_yml="""project:
135135
output-dir: {doc_path}
136136
@@ -142,7 +142,7 @@ def _f(a,b): return Path(a),b
142142
repo-url: "{git_url}"
143143
"""
144144

145-
# %% ../nbs/api/14_quarto.ipynb 15
145+
# %% ../nbs/api/14_quarto.ipynb 16
146146
def refresh_quarto_yml():
147147
"Generate `_quarto.yml` from `settings.ini`."
148148
cfg = get_config()
@@ -156,13 +156,13 @@ def refresh_quarto_yml():
156156
if qy.exists() and not str2bool(cfg.get('custom_quarto_yml', True)): qy.unlink()
157157
if not qy.exists(): qy.write_text(_quarto_yml)
158158

159-
# %% ../nbs/api/14_quarto.ipynb 16
159+
# %% ../nbs/api/14_quarto.ipynb 17
160160
def _ensure_quarto():
161161
if shutil.which('quarto'): return
162162
print("Quarto is not installed. We will download and install it for you.")
163163
install.__wrapped__()
164164

165-
# %% ../nbs/api/14_quarto.ipynb 17
165+
# %% ../nbs/api/14_quarto.ipynb 18
166166
def _pre_docs(path=None, n_workers:int=defaults.cpus, **kwargs):
167167
cfg = get_config()
168168
path = Path(path) if path else cfg.nbs_path
@@ -174,55 +174,66 @@ def _pre_docs(path=None, n_workers:int=defaults.cpus, **kwargs):
174174
cache = proc_nbs(path, n_workers=n_workers, **kwargs)
175175
return cache,cfg,path
176176

177-
# %% ../nbs/api/14_quarto.ipynb 18
177+
# %% ../nbs/api/14_quarto.ipynb 19
178178
@call_parse
179179
@delegates(proc_nbs)
180180
def nbdev_proc_nbs(**kwargs):
181181
"Process notebooks in `path` for docs rendering"
182182
_pre_docs(**kwargs)[0]
183183

184-
# %% ../nbs/api/14_quarto.ipynb 20
184+
# %% ../nbs/api/14_quarto.ipynb 21
185+
def _readme_not_out_of_date(readme_path, readme_nb_path):
186+
if not readme_nb_path.exists():
187+
print(f"Could not find {readme_nb_path}")
188+
return True
189+
return readme_path.exists() and readme_path.stat().st_mtime>=readme_nb_path.stat().st_mtime
190+
191+
# %% ../nbs/api/14_quarto.ipynb 22
192+
class SidebarYmlRemoved:
193+
def __init__(self,path): self._path=path
194+
def __enter__(self):
195+
self._yml_path = self._path/'sidebar.yml'
196+
self._moved=False
197+
if self._yml_path.exists():
198+
self._yml_path.rename(self._path/'sidebar.yml.bak')
199+
self._moved=True
200+
def __exit__(self, exc_type, exc_value, exc_tb):
201+
if self._moved: (self._path/'sidebar.yml.bak').rename(self._yml_path)
202+
203+
# %% ../nbs/api/14_quarto.ipynb 23
185204
def _copytree(a,b):
186205
if sys.version_info.major >=3 and sys.version_info.minor >=8: copytree(a, b, dirs_exist_ok=True)
187206
else:
188207
from distutils.dir_util import copy_tree
189208
copy_tree(a, b)
190209

191-
# %% ../nbs/api/14_quarto.ipynb 21
210+
# %% ../nbs/api/14_quarto.ipynb 24
211+
def _save_cached_readme(cache, cfg):
212+
tmp_doc_path = cache/cfg.doc_path.name
213+
readme = tmp_doc_path/'README.md'
214+
if readme.exists():
215+
readme_path = cfg.config_path/'README.md'
216+
if readme_path.exists(): readme_path.unlink() # py37 doesn't have `missing_ok`
217+
move(readme, cfg.config_path)
218+
_rdmi = tmp_doc_path/((cache/cfg.readme_nb).stem + '_files') # Supporting files for README
219+
if _rdmi.exists(): _copytree(_rdmi, cfg.config_path/_rdmi.name)
220+
221+
# %% ../nbs/api/14_quarto.ipynb 25
192222
@call_parse
193223
def nbdev_readme(
194224
path:str=None, # Path to notebooks
195225
chk_time:bool=False): # Only build if out of date
196226
cfg = get_config()
197-
cfg_path = cfg.config_path
198227
path = Path(path) if path else cfg.nbs_path
199-
idx_path = path/cfg.readme_nb
200-
if not idx_path.exists(): return print(f"Could not find {idx_path}")
201-
readme_path = cfg_path/'README.md'
202-
if chk_time and readme_path.exists() and readme_path.stat().st_mtime>=idx_path.stat().st_mtime: return
203-
204-
yml_path = path/'sidebar.yml'
205-
moved=False
206-
if yml_path.exists():
207-
# move out of the way to avoid rendering whole website
208-
yml_path.rename(path/'sidebar.yml.bak')
209-
moved=True
228+
if chk_time and _readme_not_out_of_date(cfg.config_path/'README.md', path/cfg.readme_nb): return
210229

211-
try:
230+
with SidebarYmlRemoved(path): # to avoid rendering whole website
212231
cache = proc_nbs(path)
213-
idx_cache = cache/cfg.readme_nb
214-
_sprun(f'cd "{cache}" && quarto render "{idx_cache}" -o README.md -t gfm --no-execute')
215-
finally:
216-
if moved: (path/'sidebar.yml.bak').rename(yml_path)
217-
tmp_doc_path = cache/cfg.doc_path.name
218-
readme = tmp_doc_path/'README.md'
219-
if readme.exists():
220-
_rdmi = tmp_doc_path/(idx_cache.stem + '_files')
221-
if readme_path.exists(): readme_path.unlink() # py37 doesn't have `missing_ok`
222-
move(readme, cfg_path)
223-
if _rdmi.exists(): _copytree(_rdmi, cfg_path/_rdmi.name) # Supporting files for README
232+
_sprun(f'cd "{cache}" && quarto render "{cache/cfg.readme_nb}" -o README.md -t gfm --no-execute')
233+
234+
_save_cached_readme(cache, cfg)
224235

225-
# %% ../nbs/api/14_quarto.ipynb 23
236+
# %% ../nbs/api/14_quarto.ipynb 29
226237
@call_parse
227238
@delegates(_nbglob_docs)
228239
def nbdev_docs(
@@ -236,7 +247,7 @@ def nbdev_docs(
236247
shutil.rmtree(cfg.doc_path, ignore_errors=True)
237248
move(cache/cfg.doc_path.name, cfg.config_path)
238249

239-
# %% ../nbs/api/14_quarto.ipynb 25
250+
# %% ../nbs/api/14_quarto.ipynb 31
240251
@call_parse
241252
def prepare():
242253
"Export, test, and clean notebooks, and render README if needed"
@@ -247,7 +258,7 @@ def prepare():
247258
refresh_quarto_yml()
248259
nbdev_readme.__wrapped__(chk_time=True)
249260

250-
# %% ../nbs/api/14_quarto.ipynb 27
261+
# %% ../nbs/api/14_quarto.ipynb 33
251262
@contextmanager
252263
def fs_watchdog(func, path, recursive:bool=True):
253264
"File system watchdog dispatching to `func`"
@@ -263,7 +274,7 @@ class _ProcessHandler(FileSystemEventHandler): dispatch=func
263274
observer.stop()
264275
observer.join()
265276

266-
# %% ../nbs/api/14_quarto.ipynb 28
277+
# %% ../nbs/api/14_quarto.ipynb 34
267278
@call_parse
268279
@delegates(_nbglob_docs)
269280
def nbdev_preview(

0 commit comments

Comments
 (0)