Skip to content

Commit 3d92516

Browse files
hmellorHarry Mellor
andauthored
Add example scripts to documentation (#4225)
Co-authored-by: Harry Mellor <[email protected]>
1 parent 1543680 commit 3d92516

File tree

6 files changed

+80
-1
lines changed

6 files changed

+80
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ instance/
7070

7171
# Sphinx documentation
7272
docs/_build/
73+
docs/source/getting_started/examples/*.rst
74+
!**/*.template.rst
7375

7476
# PyBuilder
7577
.pybuilder/

docs/source/conf.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
# List of patterns, relative to source directory, that match files and
4949
# directories to ignore when looking for source files.
5050
# This pattern also affects html_static_path and html_extra_path.
51-
exclude_patterns: List[str] = []
51+
exclude_patterns: List[str] = ["**/*.template.rst"]
5252

5353
# Exclude the prompt "$" when copying code
5454
copybutton_prompt_text = r"\$ "
@@ -73,6 +73,13 @@
7373
# so a file named "default.css" will overwrite the builtin "default.css".
7474
# html_static_path = ['_static']
7575

76+
77+
# Generate additional rst documentation here.
78+
def setup(app):
79+
from docs.source.generate_examples import generate_examples
80+
generate_examples()
81+
82+
7683
# Mock out external dependencies here.
7784
autodoc_mock_imports = [
7885
"cpuinfo",

docs/source/generate_examples.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import re
2+
from pathlib import Path
3+
4+
5+
def fix_case(text: str) -> str:
6+
subs = [
7+
("api", "API"),
8+
("llm", "LLM"),
9+
("vllm", "vLLM"),
10+
("openai", "OpenAI"),
11+
("multilora", "MultiLoRA"),
12+
]
13+
for sub in subs:
14+
text = re.sub(*sub, text, flags=re.IGNORECASE)
15+
return text
16+
17+
18+
def underline(title: str, character: str = "=") -> str:
19+
return f"{title}\n{character * len(title)}"
20+
21+
22+
def generate_title(filename: str) -> str:
23+
# Turn filename into a title
24+
title = filename.replace("_", " ").title()
25+
# Handle acronyms and names
26+
title = fix_case(title)
27+
# Underline title
28+
title = underline(title)
29+
return title
30+
31+
32+
def generate_examples():
33+
root_dir = Path(__file__).parent.parent.parent.resolve()
34+
35+
# Source paths
36+
script_dir = root_dir / "examples"
37+
script_paths = sorted(script_dir.glob("*.py"))
38+
39+
# Destination paths
40+
doc_dir = root_dir / "docs/source/getting_started/examples"
41+
doc_paths = [doc_dir / f"{path.stem}.rst" for path in script_paths]
42+
43+
# Generate the example docs for each example script
44+
for script_path, doc_path in zip(script_paths, doc_paths):
45+
script_url = f"https://github.com/vllm-project/vllm/blob/main/examples/{script_path.name}"
46+
# Make script_path relative to doc_path and call it include_path
47+
include_path = '../../../..' / script_path.relative_to(root_dir)
48+
content = (f"{generate_title(doc_path.stem)}\n\n"
49+
f"Source {script_url}.\n\n"
50+
f".. literalinclude:: {include_path}\n"
51+
" :language: python\n"
52+
" :linenos:\n")
53+
with open(doc_path, "w+") as f:
54+
f.write(content)
55+
56+
# Generate the toctree for the example scripts
57+
with open(doc_dir / "examples_index.template.rst") as f:
58+
examples_index = f.read()
59+
with open(doc_dir / "examples_index.rst", "w+") as f:
60+
example_docs = "\n ".join(path.stem for path in script_paths)
61+
f.write(examples_index.replace(r"%EXAMPLE_DOCS%", example_docs))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Examples
2+
=================================
3+
4+
.. toctree::
5+
:maxdepth: 1
6+
:caption: Scripts
7+
8+
%EXAMPLE_DOCS%

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Documentation
6565
getting_started/neuron-installation
6666
getting_started/cpu-installation
6767
getting_started/quickstart
68+
getting_started/examples/examples_index
6869

6970
.. toctree::
7071
:maxdepth: 1
File renamed without changes.

0 commit comments

Comments
 (0)