cli
Build, preview and migrate sites from the command line
Run octavo-build [path] to build a site, octavo-preview [path] to rebuild and serve it locally, or octavo-migrate-quarto [path] to convert a Quarto project. The path defaults to the current directory. Each command has --help.
Build and preview read octavo.yml. Set output there or pass --output. YAML output is relative to the directory containing octavo.yml. An explicit --output is relative to the working directory and overrides the YAML setting. Builds replace the output directory and can execute Markdown {python} blocks. Read the migration guide before converting Quarto sources.
Preview opens a browser at http://localhost:8000. Use --port, --host, or --no-browser to change this. Refresh the browser after rebuilds. Press Ctrl-C to stop. Restart preview after changing the output directory. The server is for local development, not public hosting.
Building
Output paths are resolved before loading pages. Source and output directories must not overlap in either direction. For example, use sibling docs/ and _site/ directories. Invalid destinations are rejected before executable pages run.
build_site
def build_site(
path:str='.', # Source directory or Python project root
output:str=None, # Output directory, relative to cwd; defaults to octavo.yml or `_docs` for Python projects
):
Build a source directory or Python project and report unresolved links
resolve_output is shared with Site.build; see its path-resolution examples in the site documentation. An explicit CLI output overrides octavo.yml.
Here docs/octavo.yml selects ../_site. Building from the project directory or from docs writes to the same destination. Passing output='preview' from the project directory overrides it with the sibling preview directory.
with tempfile.TemporaryDirectory() as d:
project = Path(d).resolve()
docs = project/'docs'
docs.mkdir()
(docs/'octavo.yml').write_text('output: ../_site')
(docs/'index.md').write_text('# Output paths')
with working_directory(project): build_site('docs')
assert (project/'_site/index.html').exists()
with working_directory(docs): build_site()
assert (project/'_site/index.html').exists()
with working_directory(project): build_site('docs', output='preview')
assert (project/'preview/index.html').exists()
assert not (docs/'preview').exists()
(docs/'index.qmd').write_text(r"""```{python}
raise RuntimeError("page ran before validation")
```""")
with expect_fail(ValueError, contains='sources'): build_site(str(docs), output=str(docs/'public'))
Preview
serve_preview accepts a build command as an argument list. Octavo and nbdev supply their own commands and share the watcher and HTTP server. Each rebuild runs in a new process. A failed build replaces stale output with an error page; the next edit retries.
The watcher includes files below its watch directory, including non-hidden configuration and assets. nbdev watches the whole project, which can contain both sources and output. Callers validate the actual source/output separation before starting serve_preview. The output must not contain the watch directory. Output files, dot-prefixed paths and __pycache__ are ignored. Changes are grouped until no new event arrives for 0.2 seconds.
A failed command must not leave old pages looking current. _rebuild replaces the entire output directory with an error page and returns normally, allowing the next source edit to retry.
with tempfile.TemporaryDirectory() as d:
build_root = Path(d)
build_out = build_root/'_site'
build_out.mkdir()
(build_out/'stale.html').write_text('Old build')
_rebuild(build_root, build_out, [sys.executable, '-c', 'raise SystemExit(1)'])
test_eq([o.name for o in build_out.iterdir()], ['index.html'])
assert 'Build failed' in (build_out/'index.html').read_text()
Build failed; waiting for another edit.
preview_change
def preview_change(
event, root, out
):
Return whether a filesystem event affects preview sources
preview_change accepts resolved source and output paths. Moves check both endpoints, including a move from a hidden temporary file to a visible page. Output files must not trigger another build. Directory modifications are ignored, but directory creation, deletion and moves still count.
from watchdog.events import FileModifiedEvent, FileMovedEvent, DirModifiedEvent, DirDeletedEvent
src = Path('preview-example').resolve()
dest = src/'_site'
cases = [('Page modified', FileModifiedEvent(str(src/'index.md')), True),
('Output modified', FileModifiedEvent(str(dest/'index.html')), False),
('Hidden file modified', FileModifiedEvent(str(src/'.draft.md')), False),
('Bytecode modified', FileModifiedEvent(str(src/'__pycache__/page.pyc')), False),
('Hidden file moved to page', FileMovedEvent(str(src/'.page.tmp'), str(src/'page.md')), True),
('Page moved outside sources', FileMovedEvent(str(src/'page.md'), str(src.parent/'page.md')), True),
('Directory modified', DirModifiedEvent(str(src)), False),
('Directory deleted', DirDeletedEvent(str(src/'posts')), True)]
rebuilds = {name: preview_change(event, src, dest) for name,event,expected in cases}
for name,event,expected in cases: test_eq(rebuilds[name], expected)
rebuilds
{'Page modified': True,
'Output modified': False,
'Hidden file modified': False,
'Bytecode modified': False,
'Hidden file moved to page': True,
'Page moved outside sources': True,
'Directory modified': False,
'Directory deleted': True}
The watcher signals source changes from its background thread. Leaving its context stops and joins that thread. _wait_change consumes a burst of signals and waits for 0.2 seconds of quiet before the next build.
with tempfile.TemporaryDirectory() as d:
watched = Path(d).resolve()
with _watch_changes(watched, watched/'_site') as changed:
(watched/'index.md').write_text('# Preview example')
detected = changed.wait(5)
test_eq(detected, True)
detected
True
serve_preview
def serve_preview(
root, # Directory to watch and run the build command in
out, # Directory served over HTTP
cmd, # Build command as an argv list
port:int=8000, # HTTP port; 0 asks the OS for an available port
host:str='localhost', # Bind address
no_browser:bool=False, # Do not open a browser
):
Serve build output and rerun cmd when source files change
preview_site
def preview_site(
path:str='.', # Source directory or Python project root
output:str=None, # Output directory, relative to cwd; defaults to octavo.yml or `_docs` for Python projects
port:int=8000, # HTTP port; 0 chooses an available port
host:str='localhost', # Bind address
no_browser:bool=False, # Do not open a browser
):
Build and preview a source directory or Python project
preview_site uses Octavo's build command. This call serves docs on port 8001 without opening a browser. It blocks until Ctrl-C and writes builds to _site beside docs.
preview_site('docs', output='_site', port=8001, no_browser=True)
Commands
octavo-build, octavo-preview and octavo-migrate-quarto use fastcore's call_parse. Each function's signature and docments define its command's arguments and help. The optional positional path defaults to the current directory.