Skip to content

Commit

Permalink
add autobuild script and instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecuoco committed Nov 10, 2023
1 parent 5d00b5f commit a80a204
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ To remove the build folder (including `cached` executables), you can run:
```bash
jb clean --all wiki/
```

Alternatively, you can run the following script to build the book and automatically watch for changes!

```bash
python jb-serve.py wiki
```
52 changes: 52 additions & 0 deletions jb-serve.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import subprocess
from pathlib import Path
import os

import click
from livereload import Server

@click.command()
@click.argument("pathsource", default=".", type=Path)
@click.option("-o", "--outputdir", default="_build/html", type=Path, show_default=True)
@click.option("-p", "--port", default=8002, type=click.INT, show_default=True)
def main(pathsource: Path, outputdir: Path, port: int):
"""
Script to serve a jupyter-book site, which rebuilds when files have
changed and live-reloads the site. Basically `mkdocs serve`
but for jupyter-book. Use by calling `python jb-serve.py [OPTIONS] [PATH_SOURCE]`.
\b
Args
----
PATHSOURCE: Directory in `jb build <dir>`
outputdir: Directory where HTML output is generated. `jb` defaults to `_build/html`
port: Port to host the webserver. Default is 8002
\b
Refs
----
+ https://github.com/executablebooks/sphinx-autobuild/issues/99#issuecomment-722319104
+ mkdocs docs on github
"""

def build():
subprocess.run(["jb", "clean", pathsource])
subprocess.run(["jb", "build", pathsource])

# Build if not exists upon startup
if not os.path.exists(outputdir):
build()

server = Server()

# Globbing for all supported file types under jupyter-book
# Ignore unrelated files
server.watch(str(pathsource / "docs/*"), build)
server.watch(str(pathsource / "images/*"), build)
server.watch(str(pathsource / "_config.yml"), build)
server.watch(str(pathsource / "_toc.yml"), build)

server.serve(root=outputdir, port=port)

if __name__ == "__main__":
main()

0 comments on commit a80a204

Please sign in to comment.