Skip to content

Commit 1e2fab2

Browse files
DOC: disambiguate examples from different branches of same repo (#638)
Previously, only the repo and file names were used to assign files to examples, as initiated by the custom directive. When multiple examples use different branches of the same repo, each would overwrite filenames of previous examples. Now, example ref (commit) is used in the filename to prevent this. Also added ref to the "original source" links for each example.
1 parent 5096ba3 commit 1e2fab2

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

docs/conf.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _grab_external_examples(example_source: Path):
169169
base = f"https://raw.githubusercontent.com/{user}/{repo}/{ref}/{dir}/"
170170
notebooks = fetch_notebook_list(base)
171171
base = f"https://raw.githubusercontent.com/{user}/{repo}/{ref}/"
172-
local_nbs = [(name, copy_nb(base, pth, repo)) for name, pth in notebooks]
172+
local_nbs = [(name, _copy_nb(base, pth, repo, ref)) for name, pth in notebooks]
173173
EXTERNAL_EXAMPLES[ex_name] = local_nbs
174174

175175

@@ -197,7 +197,9 @@ def run(self) -> list[nodes.Node]:
197197
RuntimeError("Unknown configuration key for external example")
198198
heading_text: str = self.options.get("title")
199199
base_repo = f"https://github.com/{this_example['user']}/{this_example['repo']}"
200-
repo_ref = nodes.reference(name="Source repo", refuri=base_repo)
200+
repo_ref = nodes.reference(
201+
name="Source repo", refuri=f"{base_repo}/tree/{this_example['ref']}"
202+
)
201203
ref_text = nodes.Text("Source repo")
202204
repo_ref += ref_text
203205
repo_par = nodes.paragraph()
@@ -260,15 +262,17 @@ def deduplicate(mylist: list[T]) -> list[T]:
260262
return rellinks
261263

262264

263-
def copy_nb(base: str, relpath: str, repo: str) -> str:
265+
def _copy_nb(base: str, relpath: str, repo: str, ref: str) -> str:
264266
"""Create a local copy of external file, modifying relative reference"""
265267
example_dir = Path(__file__).parent / "examples"
266268
repo_local_dir = example_dir / repo
267269
repo_local_dir.mkdir(exist_ok=True)
270+
ref_local_dir = repo_local_dir / ref
271+
ref_local_dir.mkdir(exist_ok=True)
268272
page = requests.get(base + relpath)
269273
if page.status_code != 200:
270274
raise RuntimeError(f"Unable to locate external notebook at {base + relpath}")
271-
filename = repo_local_dir / relpath.rsplit("/", 1)[1]
275+
filename = ref_local_dir / relpath.rsplit("/", 1)[1]
272276
with open(filename, "wb") as f:
273277
f.write(page.content)
274278
return os.path.relpath(filename, start=example_dir)

0 commit comments

Comments
 (0)