-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_docs.py
102 lines (83 loc) · 2.85 KB
/
prepare_docs.py
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
"""
Generate docs directory for GitHub Pages.
"""
import argparse
import pathlib
import subprocess
import sys
root_dir = pathlib.Path(__file__).parent.parent
docs_dir = root_dir.joinpath("docs")
readme_template = """\
# Project Webpage for ISCB Diversity Analysis
<!-- make sure to edit this content in utils/prepare_docs.py and not docs/readme.md -->
More information at <https://github.com/greenelab/iscb-diversity>.
See also the study corresponding to this analysis at <https://greenelab.github.io/iscb-diversity-manuscript/>.
## Notebooks
See the following rendered notebooks:
{notebook_list_md}
"""
def parse_args():
parser = argparse.ArgumentParser(
description="Generate docs directory for GitHub Pages."
)
parser.add_argument(
"--nbconvert", action="store_true", help="Convert .ipynb files to docs/*.html"
)
parser.add_argument(
"--nbviewer", action="store_true", help="Use links to https://nbviewer.jupyter.org/ for Jupyter notebooks"
)
parser.add_argument(
"--repo",
default="greenelab/iscb-diversity",
help="GitHub repository to use for hyperlinks",
)
parser.add_argument(
"--readme",
action="store_true",
help="Regenerate docs/readme.md (the GitHub Pages homepage)",
)
args = parser.parse_args()
if len(sys.argv) == 1:
# print help when no arguments are specified
parser.print_help()
return args
def get_ipynb_paths():
ipynb_paths = sorted(root_dir.glob("*.ipynb"))
ipynb_paths = [path.relative_to(root_dir) for path in ipynb_paths]
return ipynb_paths
def render_jupyter_notebooks():
ipynb_paths = get_ipynb_paths()
args = [
"jupyter",
"nbconvert",
"--output-dir=docs",
*ipynb_paths,
]
subprocess.run(args, cwd=root_dir)
def get_nbviewer_md_list(args):
nbviewer_md_list = []
if not args.nbviewer:
return nbviewer_md_list
ipynb_paths = get_ipynb_paths()
for path in ipynb_paths:
nbviewer_url = f"https://nbviewer.jupyter.org/github/{args.repo}/blob/master/{path}"
md = f"- [{path.stem}]({nbviewer_url})"
nbviewer_md_list.append(md)
return nbviewer_md_list
def get_notebook_list_md(args):
notebook_md_list = get_nbviewer_md_list(args)
html_paths = sorted(docs_dir.glob("**/*.html"))
for path in html_paths:
path = path.relative_to(docs_dir)
notebook_md_list.append(f"- [{path.stem}]({path})")
notebook_md_list.sort()
return "\n".join(notebook_md_list)
if __name__ == "__main__":
args = parse_args()
assert docs_dir.is_dir()
if args.nbconvert:
render_jupyter_notebooks()
if args.readme:
notebook_list_md = get_notebook_list_md(args)
readme = readme_template.format(notebook_list_md=notebook_list_md)
docs_dir.joinpath("readme.md").write_text(readme)