Skip to content

Commit 72167c3

Browse files
Use importlib.metadata for package version retrieval in docs (#150)
* Use importlib.metadata for package version retrieval in docs * cookiecutter template to use import metadata to retrieve the version * Fixes Requested changes, and preventing logo to overlap texts * Add v before release in cookiecutter site. Add reference issue for css change --------- Co-authored-by: sfmig <[email protected]>
1 parent 7d4d941 commit 72167c3

File tree

7 files changed

+55
-6
lines changed

7 files changed

+55
-6
lines changed

docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-e . # required to build API docs
12
linkify-it-py
23
myst-parser
34
nbsphinx

docs/source/_static/css/custom.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ html[data-theme=light] {
1111
body .bd-article-container {
1212
max-width: 100em !important;
1313
}
14+
/* Custom CSS for the documentation site */
15+
/* prevent logo from being too wide and text clashing with next item */
16+
/* See https://github.com/pydata/pydata-sphinx-theme/issues/1143#issuecomment-2468763375 */
17+
18+
.navbar-header-items__start .navbar-item {
19+
width: 100%;
20+
}
21+
22+
.navbar-item .navbar-brand {
23+
width: 100%;
24+
}
25+
26+
.navbar-brand img {
27+
min-width: 0;
28+
height: auto;
29+
max-height: 100%;
30+
flex-shrink: 1;
31+
}
32+
33+
.navbar-brand p {
34+
flex: 0 1 auto;
35+
}
36+
37+
/* sponsors */
1438

1539
.col {
1640
flex: 0 0 50%;

docs/source/conf.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
# conf.py
22
# Configuration file for the Sphinx documentation builder.
3-
import setuptools_scm
3+
from importlib.metadata import version as get_version
44

55

66
project = "python-cookiecutter"
77
copyright = "2025, University College London"
88
author = "Neuroinformatics Unit"
9+
910
try:
10-
full_version = setuptools_scm.get_version(root="../..", relative_to=__file__)
11+
full_version = get_version(project)
1112
# Splitting the release on '+' to remove the commit hash
1213
release = full_version.split('+', 1)[0]
1314
except LookupError:
@@ -84,7 +85,7 @@
8485
},
8586
],
8687
"logo": {
87-
"text": f"{project}",
88+
"text": f"{project} v{release}",
8889
},
8990
"footer_start": ["footer_start"],
9091
"footer_end": ["footer_end"],

docs/source/contributing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ If you are contributing to the Documentation, Before pushing your changes, you c
5353
the docs:
5454
5555
``` sh
56+
# Must execute from project root directory
5657
pip install -r docs/requirements.txt
5758
sphinx-build docs/source docs/build
5859
```
@@ -61,7 +62,7 @@ Alternatively, you can use the following commands to install the
6162
dependencies and build the docs:
6263
6364
``` sh
64-
pip install -r docs/requirements.txt
65+
pip install -r docs/requirements.txt # Must execute this command from project root directory
6566
cd docs
6667
make html
6768
```

pyproject.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
[project]
2+
name = "python-cookiecutter"
3+
authors = [{ name = "Neuroinformatics Unit" }]
4+
description = "A tool to automatically create a Python project structure ready to release via GitHub and PyPI."
5+
readme = "README.md"
6+
dynamic = ["version"]
7+
8+
license = { text = "BSD-3-Clause" }
9+
10+
[build-system]
11+
requires = [
12+
"setuptools>=64",
13+
"wheel",
14+
"setuptools-scm[toml]>=8",
15+
]
16+
build-backend = "setuptools.build_meta"
17+
18+
[tool.setuptools_scm]
19+
120
[tool.ruff]
221
line-length = 79
322
exclude = ["__init__.py", "build", ".eggs"]

{{cookiecutter.package_name}}/docs/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-e . # required to build API docs
12
linkify-it-py
23
myst-parser
34
nbsphinx

{{cookiecutter.package_name}}/docs/source/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import os
1010
import sys
1111

12-
import setuptools_scm
12+
from importlib.metadata import version as get_version
1313

1414
# Used when building API docs, put the dependencies
1515
# of any class you are documenting here
@@ -24,7 +24,9 @@
2424
copyright = "2022, {{cookiecutter.full_name}}"
2525
author = "{{cookiecutter.full_name}}"
2626
try:
27-
release = setuptools_scm.get_version(root="../..", relative_to=__file__)
27+
full_version = get_version(project)
28+
# Splitting the release on '+' to remove the commit hash
29+
release = full_version.split('+', 1)[0]
2830
except LookupError:
2931
# if git is not initialised, still allow local build
3032
# with a dummy version

0 commit comments

Comments
 (0)