docs(create_rstfiles): order the notebook galleries by when a notebook was added - #2792
Conversation
jdhughes-dev
commented
Aug 5, 2026
- The notebook galleries were built from an alphabetical list, so a new notebook landed wherever its name fell rather than at the end
- Notebooks are now ordered by the commit that added them, and one that is not in the history yet sorts last
- Notebooks added in the same commit keep their alphabetical order, so most of the galleries are unchanged
- The order falls back to alphabetical when the history is not available, as in a shallow clone, rather than failing the documentation build
…k was added The galleries were built from an alphabetical list, so a new notebook landed wherever its name happened to fall and the reader had no way to tell what had just been added. Notebooks are now ordered by the commit that added them, and a notebook that is not in the history yet, as when it is added in the same pull request, sorts last. Notebooks added in the same commit keep their alphabetical order, so the majority of the galleries are unchanged. The order falls back to alphabetical when the history is not available, as in a shallow clone, rather than failing the documentation build.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #2792 +/- ##
===========================================
+ Coverage 55.5% 72.8% +17.2%
===========================================
Files 644 662 +18
Lines 124135 133001 +8866
===========================================
+ Hits 68947 96828 +27881
+ Misses 55188 36173 -19015 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Updates the docs gallery generation script to order tutorial/example notebook listings by the commit timestamp when each notebook was first added (with graceful fallback to alphabetical ordering when Git history is unavailable).
Changes:
- Introduces an
added_order()sort key that queriesgit log --diff-filter=Afor each notebook’s “added” timestamp. - Uses that sort key when building
tutorials.rstandexamples.rstnotebook lists, instead of purely alphabetical ordering. - Preserves alphabetical ordering as a tie-breaker and provides a fallback when history cannot be queried.
Suppressed comments (1)
.docs/create_rstfiles.py:104
- This sorts all notebook files and only then filters by name, which does extra
added_order()work (and extragit logcalls) for notebooks that will be discarded. Filtering first and then sorting only the relevant subset avoids that overhead.
filenames = [
path.name
for path in sorted(nbs_path.rglob("*.py"), key=added_order)
if "example" in path.name
]
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import subprocess | ||
| from pathlib import Path | ||
|
|
||
| project_root_path = Path(__file__).parent.parent | ||
|
|
||
|
|
||
| def added_order(path): | ||
| """Sort key that puts notebooks in the order they were added | ||
|
|
||
| Notebooks added in the same commit keep their alphabetical order, and one | ||
| that is not in the history yet sorts last. The order falls back to | ||
| alphabetical when the history is not available, as in a shallow clone. | ||
| """ | ||
| try: | ||
| added = subprocess.run( | ||
| ["git", "log", "--diff-filter=A", "--format=%at", "-1", "--", str(path)], | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=project_root_path, | ||
| timeout=10, | ||
| check=False, | ||
| ).stdout.strip() | ||
| except (OSError, subprocess.SubprocessError): | ||
| added = "" | ||
| return (int(added) if added else 2**31, path.name) |
There was a problem hiding this comment.
All three fixed in c2be913.
The pathspec is now relative to the repository, so git log gets what it expects rather than an absolute path. The sentinel is float("inf") instead of 2**31, which was going to stop sorting an unreleased notebook last in 2038. The key is cached with lru_cache, which matters because the tutorials and the examples each sort the same notebooks.
| filenames = [ | ||
| path.name | ||
| for path in sorted(nbs_path.rglob("*.py"), key=added_order) | ||
| if "tutorial" in path.name | ||
| ] |
There was a problem hiding this comment.
Fixed in c2be913, in both places. The notebooks are filtered by name before they are sorted.
Together with caching the key that is 84 calls to git log rather than 174, and the whole of create_rstfiles.py runs in about a second.
…rtable The sort key ran git log on an absolute path, which is not a pathspec git accepts everywhere and would have silently fallen back to alphabetical order. It also used a sentinel that stops sorting an unreleased notebook last in 2038, sorted every notebook before filtering by name, and ran once for the tutorials and again for the examples. The pathspec is now relative to the repository, the sentinel is infinity, the notebooks are filtered before they are sorted, and the key is cached. That is 84 calls to git log rather than 174.