-
Notifications
You must be signed in to change notification settings - Fork 517
Expand file tree
/
Copy pathquarto.py
More file actions
372 lines (325 loc) · 13.8 KB
/
quarto.py
File metadata and controls
372 lines (325 loc) · 13.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
"""Install and interact with Quarto from nbdev"""
# AUTOGENERATED! DO NOT EDIT! File to edit: ../nbs/api/14_quarto.ipynb.
# %% ../nbs/api/14_quarto.ipynb #6a35c7c4-748f-4c82-a9bf-c780a8d83e90
from __future__ import annotations
import subprocess,sys,shutil,ast,warnings,traceback
from os import system
from contextlib import contextmanager
from .config import *
from .doclinks import *
from fastcore.utils import *
from fastcore.script import call_parse
from fastcore.shutil import rmtree,move,copytree
from fastcore.meta import delegates
from .serve import proc_nbs,_proc_file
from . import serve_drv
import yaml
# %% auto #0
__all__ = ['BASE_QUARTO_URL', 'install_quarto', 'install', 'IndentDumper', 'nbdev_sidebar', 'refresh_quarto_yml',
'nbdev_proc_nbs', 'nbdev_readme', 'nbdev_contributing', 'nbdev_docs', 'prepare', 'fs_watchdog',
'nbdev_preview']
# %% ../nbs/api/14_quarto.ipynb #aae2d2be-ad03-4536-bf70-c4575f39cea3
def _sprun(cmd):
try: subprocess.check_output(cmd, shell=True)
except subprocess.CalledProcessError as cpe: sys.exit(cpe.returncode)
# %% ../nbs/api/14_quarto.ipynb #75e4b6a1
BASE_QUARTO_URL='https://www.quarto.org/download/latest/'
def _install_linux():
from os import uname
machine = 'arm' if uname().machine in ('arm64', 'aarch64', 'armv8', 'armv8l') else 'amd'
system(f'curl -LO {BASE_QUARTO_URL}quarto-linux-{machine}64.deb')
system(f'sudo dpkg -i quarto-linux-{machine}64.deb && rm quarto-linux-{machine}64.deb')
def _install_mac():
system(f'curl -LO {BASE_QUARTO_URL}quarto-macos.pkg')
system('sudo installer -pkg quarto-macos.pkg -target / && rm quarto-macos.pkg')
@call_parse
def install_quarto():
"Install latest Quarto on macOS or Linux, prints instructions for Windows"
if sys.platform not in ('darwin','linux'):
return print('Please visit https://quarto.org/docs/get-started/ to install quarto')
print("Installing or upgrading quarto -- this requires root access.")
system('sudo touch .installing')
try:
installing = Path('.installing')
if not installing.exists(): return print("Cancelled. Please download and install Quarto from quarto.org.")
if 'darwin' in sys.platform: _install_mac()
elif 'linux' in sys.platform: _install_linux()
finally: system('sudo rm -f .installing')
# %% ../nbs/api/14_quarto.ipynb #7580d48f-e10e-4bb0-937e-90645b5dfd08
@call_parse
def install():
"Install Quarto and the current library"
install_quarto.__wrapped__()
d = get_config().lib_path
if (d/'__init__.py').exists(): system(f'pip install -e "{d.parent}[dev]"')
# %% ../nbs/api/14_quarto.ipynb #b93f6def
def _pre(p,b=True): return ' ' * (len(p.parts)) + ('- ' if b else ' ')
def _sort(a):
x,y = a
if y.startswith('index.'): return x,'00'
return a
_def_file_re = r'\.(?:ipynb|qmd|html)$'
@delegates
def _nbglob_docs(
path:str=None, # Path to notebooks
file_glob:str=None, # Only include files matching glob
file_re:str=_def_file_re, # Only include files matching regex
**kwargs):
return nbglob(path, file_glob=file_glob, file_re=file_re, **kwargs)
# %% ../nbs/api/14_quarto.ipynb #37ab90f4
def _recursive_parser(
dir_dict: dict, # Directory structure as a dict.
contents: list, # `contents` list from `sidebar.yaml` template dict.
dirpath: Path, # Directory path.
section = None, # `section` mapping.
set_index: bool = True): # If `True`, `index` file will be set to href.
for name, val in dir_dict.items():
if type(val) is str:
if re.search(r'index\..*', re.sub(r'^\d+_', '', val)) and set_index and section:
section.update({'href': str(dirpath/val)})
else:
contents.append(str(dirpath/val))
elif type(val) is dict:
name = re.sub(r'^\d+_', '', name)
section = {'section': name, 'contents': []}
contents.append(section)
_recursive_parser(val, section['contents'], dirpath/name, section=section)
class IndentDumper(yaml.Dumper):
def increase_indent(self, flow=False, indentless=False):
return super(IndentDumper, self).increase_indent(flow, False)
# %% ../nbs/api/14_quarto.ipynb #d879c7e1
@call_parse
@delegates(_nbglob_docs)
def nbdev_sidebar(
path:str=None, # Path to notebooks
printit:bool=False, # Print YAML for debugging
force:bool=False, # Create sidebar even if custom_sidebar=false in pyproject.toml
skip_folder_re:str=r'(?:^[_.]|^www\$)', # Skip folders matching regex
**kwargs):
"Create sidebar.yml"
if not force and get_config().custom_sidebar: return
path = get_config().nbs_path if not path else Path(path)
def _f(a,b): return Path(a),b
files = nbglob(path, func=_f, skip_folder_re=skip_folder_re, **kwargs).sorted(key=_sort)
lastd, res = Path(), []
# Parse directory structure to dict.
# dir => dict(), file => file.
parsed_struct = {'website': {'sidebar': {'contents': []}}}
_contents = parsed_struct['website']['sidebar']['contents']
dir_struct = dict()
for dabs, name in files:
drel = dabs.relative_to(path)
_dir = dir_struct
for subdir in drel.parts:
_dir = _dir.setdefault(subdir, dict())
_dir[name] = name
_recursive_parser(dir_struct, _contents, Path())
yml_path = path/'sidebar.yml'
yml = yaml.dump(parsed_struct, Dumper=IndentDumper, sort_keys=False)
if printit: return print(yml)
yml_path.write_text(yml)
# %% ../nbs/api/14_quarto.ipynb #aabf2f15
_quarto_yml="""project:
type: website
format:
html:
theme: cosmo
css: styles.css
toc: true
keep-md: true
commonmark: default
website:
twitter-card: true
open-graph: true
repo-actions: [issue]
navbar:
background: primary
search: true
sidebar:
style: floating
metadata-files: [nbdev.yml, sidebar.yml]"""
# %% ../nbs/api/14_quarto.ipynb #faabf6e6
_nbdev_yml="""project:
output-dir: {doc_path}
website:
title: "{title}"
site-url: "{doc_host}{doc_baseurl}"
description: "{description}"
repo-branch: {branch}
repo-url: "{git_url}"
"""
# %% ../nbs/api/14_quarto.ipynb #38124450
def refresh_quarto_yml():
"Generate `_quarto.yml` from `pyproject.toml`."
cfg = get_config()
ny = cfg.nbs_path/'nbdev.yml'
vals = {k:cfg[k] for k in ['title', 'description', 'branch', 'git_url', 'doc_host', 'doc_baseurl']}
vals['doc_path'] = cfg.doc_path.name
if 'title' not in vals: vals['title'] = vals['lib_name']
ny.write_text(_nbdev_yml.format(**vals))
qy = cfg.nbs_path/'_quarto.yml'
if 'custom_quarto_yml' in cfg: print("NB: `_quarto.yml` is no longer auto-updated. Remove `custom_quarto_yml` from `pyproject.toml`")
if qy.exists() and not str2bool(cfg.get('custom_quarto_yml', True)): qy.unlink()
if not qy.exists(): qy.write_text(_quarto_yml)
# %% ../nbs/api/14_quarto.ipynb #975d370e
def _ensure_quarto():
if shutil.which('quarto'): return
print("Quarto is not installed. We will download and install it for you.")
install.__wrapped__()
# %% ../nbs/api/14_quarto.ipynb #6b880922
def _pre_docs(path=None, n_workers:int=defaults.cpus, **kwargs):
cfg = get_config()
path = Path(path) if path else cfg.nbs_path
_ensure_quarto()
refresh_quarto_yml()
import nbdev.doclinks
nbdev.doclinks._build_modidx()
nbdev_sidebar.__wrapped__(path=path, **kwargs)
cache = proc_nbs(path, n_workers=n_workers, **kwargs)
return cache,cfg,path
# %% ../nbs/api/14_quarto.ipynb #e3806fa9
@call_parse
@delegates(proc_nbs)
def nbdev_proc_nbs(**kwargs):
"Process notebooks in `path` for docs rendering"
_pre_docs(**kwargs)[0]
# %% ../nbs/api/14_quarto.ipynb #c5c7bb0e
def _doc_mtime_not_older(readme_path, readme_nb_path):
if not readme_nb_path.exists():
print(f"Could not find {readme_nb_path}")
return True
return readme_path.exists() and readme_path.stat().st_mtime>=readme_nb_path.stat().st_mtime
# %% ../nbs/api/14_quarto.ipynb #7b70b5c8
class _SidebarYmlRemoved:
"Context manager for `nbdev-readme` to avoid rendering whole docs website"
def __init__(self,path): self._path=path
def __enter__(self):
self._yml_path = self._path/'sidebar.yml'
self._moved=False
if self._yml_path.exists():
self._yml_path.rename(self._path/'sidebar.yml.bak')
self._moved=True
def __exit__(self, exc_type, exc_value, exc_tb):
if self._moved: (self._path/'sidebar.yml.bak').rename(self._yml_path)
# %% ../nbs/api/14_quarto.ipynb #49661bbf
def _copytree(a,b):
if sys.version_info.major >=3 and sys.version_info.minor >=8: copytree(a, b, dirs_exist_ok=True)
else:
from distutils.dir_util import copy_tree
copy_tree(a, b)
# %% ../nbs/api/14_quarto.ipynb #caeaa153
def _save_cached_readme(cache, cfg):
tmp_doc_path = cache/cfg.doc_path.name
readme = tmp_doc_path/'README.md'
if readme.exists():
readme_path = cfg.config_path/'README.md'
if readme_path.exists(): readme_path.unlink() # py37 doesn't have `missing_ok`
move(readme, cfg.config_path)
_rdmi = tmp_doc_path/((cache/cfg.readme_nb).stem + '_files') # Supporting files for README
if _rdmi.exists(): _copytree(_rdmi, cfg.config_path/_rdmi.name)
# %% ../nbs/api/14_quarto.ipynb #45d6bb5d
@call_parse
def nbdev_readme(
path:str=None, # Path to notebooks
chk_time:bool=False): # Only build if out of date
"Create README.md from readme_nb (index.ipynb by default)"
cfg = get_config()
path = Path(path) if path else cfg.nbs_path
if chk_time and _doc_mtime_not_older(cfg.config_path/'README.md', path/cfg.readme_nb): return
with _SidebarYmlRemoved(path): # to avoid rendering whole website
cache = proc_nbs(path)
_sprun(f'cd "{cache}" && quarto render "{cache/cfg.readme_nb}" -o README.md -t gfm --no-execute')
_save_cached_readme(cache, cfg)
# %% ../nbs/api/14_quarto.ipynb #ef3f1e1f
def _save_cached_contributing(cache, cfg, contrib_nb):
"Move CONTRIBUTING.md (and any `_files` assets) from the Quarto build cache to the repo root."
tmp_doc_path = cache / cfg.doc_path.name
contrib_file = tmp_doc_path / 'CONTRIBUTING.md'
if contrib_file.exists():
final_path = cfg.config_path / 'CONTRIBUTING.md'
if final_path.exists(): final_path.unlink() # py37 doesn't have `missing_ok`
move(contrib_file, final_path)
assets_folder = tmp_doc_path / (Path(contrib_nb).stem + '_files') # Supporting files for CONTRIBUTING
if assets_folder.exists(): _copytree(assets_folder, cfg.config_path / assets_folder.name)
# %% ../nbs/api/14_quarto.ipynb #729a0fa1
@call_parse
def nbdev_contributing(
path:str=None, # Path to notebooks
chk_time:bool=False # Only build if out-of-date
):
"""Create CONTRIBUTING.md from contributing_nb (defaults to 'contributing.ipynb' if present). Skips if the file doesn't exist."""
cfg = get_config()
path = Path(path) if path else cfg.nbs_path
contrib_nb_name = cfg.get('contributing_nb', 'contributing.ipynb')
contrib_nb_path = path / contrib_nb_name
if not contrib_nb_path.exists(): return
if chk_time and _doc_mtime_not_older(cfg.config_path / 'CONTRIBUTING.md' , contrib_nb_path): return
with _SidebarYmlRemoved(path): # to avoid rendering whole website
cache = proc_nbs(path)
_sprun(f'cd "{cache}" && quarto render "{cache/contrib_nb_name}" -o CONTRIBUTING.md -t gfm --no-execute')
_save_cached_contributing(cache, cfg, contrib_nb_name)
# %% ../nbs/api/14_quarto.ipynb #37d16049
@call_parse
@delegates(_nbglob_docs)
def nbdev_docs(
path:str=None, # Path to notebooks
n_workers:int=defaults.cpus, # Number of workers
**kwargs):
"Create Quarto docs and README.md"
cache,cfg,path = _pre_docs(path, n_workers=n_workers, **kwargs)
nbdev_readme.__wrapped__(path=path, chk_time=True)
nbdev_contributing.__wrapped__(path=path, chk_time=True)
_sprun(f'cd "{cache}" && quarto render --no-cache')
shutil.rmtree(cfg.doc_path, ignore_errors=True)
move(cache/cfg.doc_path.name, cfg.config_path)
# %% ../nbs/api/14_quarto.ipynb #23886f9c
@call_parse
def prepare():
"Export, test, and clean notebooks, and render README if needed"
import nbdev.test, nbdev.clean
nbdev_export.__wrapped__()
nbdev.test.nbdev_test.__wrapped__()
nbdev.clean.nbdev_clean.__wrapped__()
refresh_quarto_yml()
nbdev_readme.__wrapped__(chk_time=True)
nbdev_contributing.__wrapped__(chk_time=True)
# %% ../nbs/api/14_quarto.ipynb #5f701608
@contextmanager
def fs_watchdog(func, path, recursive:bool=True):
"File system watchdog dispatching to `func`"
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class _ProcessHandler(FileSystemEventHandler): dispatch=func
observer = Observer()
observer.schedule(_ProcessHandler, path, recursive=True)
observer.start()
try: yield
except KeyboardInterrupt: pass
finally:
observer.stop()
observer.join()
# %% ../nbs/api/14_quarto.ipynb #b6ddeadd
@call_parse
@delegates(_nbglob_docs)
def nbdev_preview(
path:str=None, # Path to notebooks
port:int=None, # The port on which to run preview
host:str=None, # The host on which to run preview
no_browser:bool=False, # Do not open a browser
n_workers:int=defaults.cpus, # Number of workers
**kwargs):
"Preview docs locally"
os.environ['QUARTO_PREVIEW']='1'
cache,cfg,path = _pre_docs(path, n_workers=n_workers, **kwargs)
xtra = []
if port: xtra += ['--port', str(port)]
if host: xtra += ['--host', host]
if no_browser: xtra += ['--no-browser']
def _f(e):
res = _proc_file(Path(e.src_path), cache, path)
if res:
try: serve_drv.main(res)
except: traceback.print_exc()
os.chdir(cache)
xtra = xtra or []
with fs_watchdog(_f, path): subprocess.run(['quarto','preview']+xtra)