|
20 | 20 | # import os |
21 | 21 | # import sys |
22 | 22 | # sys.path.insert(0, os.path.abspath('.')) |
| 23 | + |
| 24 | +from pathlib import Path |
| 25 | +import os |
| 26 | + |
23 | 27 | import torch |
24 | 28 | import torchvision |
25 | 29 | import pytorch_sphinx_theme |
|
44 | 48 | 'sphinx.ext.mathjax', |
45 | 49 | 'sphinx.ext.napoleon', |
46 | 50 | 'sphinx.ext.viewcode', |
| 51 | + 'sphinx_gallery.gen_gallery', |
47 | 52 | ] |
48 | 53 |
|
| 54 | +sphinx_gallery_conf = { |
| 55 | + 'examples_dirs': '../../gallery/', # path to your example scripts |
| 56 | + 'gallery_dirs': 'auto_examples', # path to where to save gallery generated output |
| 57 | + 'backreferences_dir': 'gen_modules/backreferences', |
| 58 | + 'doc_module': ('torchvision',), |
| 59 | +} |
| 60 | + |
49 | 61 | napoleon_use_ivar = True |
50 | 62 | napoleon_numpy_docstring = False |
51 | 63 | napoleon_google_docstring = True |
@@ -248,3 +260,42 @@ def handle_item(fieldarg, content): |
248 | 260 |
|
249 | 261 |
|
250 | 262 | TypedField.make_field = patched_make_field |
| 263 | + |
| 264 | + |
| 265 | +def inject_minigalleries(app, what, name, obj, options, lines): |
| 266 | + """Inject a minigallery into a docstring. |
| 267 | +
|
| 268 | + This avoids having to manually write the .. minigallery directive for every item we want a minigallery for, |
| 269 | + as it would be easy to miss some. |
| 270 | +
|
| 271 | + This callback is called after the .. auto directives (like ..autoclass) have been processed, |
| 272 | + and modifies the lines parameter inplace to add the .. minigallery that will show which examples |
| 273 | + are using which object. |
| 274 | +
|
| 275 | + It's a bit hacky, but not *that* hacky when you consider that the recommended way is to do pretty much the same, |
| 276 | + but instead with templates using autosummary (which we don't want to use): |
| 277 | + (https://sphinx-gallery.github.io/stable/configuration.html#auto-documenting-your-api-with-links-to-examples) |
| 278 | +
|
| 279 | + For docs on autodoc-process-docstring, see the autodoc docs: |
| 280 | + https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html |
| 281 | + """ |
| 282 | + |
| 283 | + conf_file_path = Path(__file__).parent.absolute() |
| 284 | + backrefs_path = conf_file_path / sphinx_gallery_conf['backreferences_dir'] / (name + '.examples') |
| 285 | + if not (os.path.isfile(backrefs_path) and os.path.getsize(backrefs_path) > 0): |
| 286 | + # We avoid showing the (empty) minigallery if there's nothing to show, i.e. if the object |
| 287 | + # isn't used in any example. |
| 288 | + # FIXME: this check can be removed once https://github.com/sphinx-gallery/sphinx-gallery/pull/813 |
| 289 | + # is merged and the new sphinx-gallery version (> 0.8.x) is released. |
| 290 | + return |
| 291 | + |
| 292 | + if what in ("class", "function"): |
| 293 | + lines.append(f".. minigallery:: {name}") |
| 294 | + lines.append(f" :add-heading: Examples using ``{name.split('.')[-1]}``:") |
| 295 | + # avoid heading entirely to avoid warning. As a bonud it actually renders better |
| 296 | + lines.append(" :heading-level: 9") |
| 297 | + lines.append("\n") |
| 298 | + |
| 299 | + |
| 300 | +def setup(app): |
| 301 | + app.connect('autodoc-process-docstring', inject_minigalleries) |
0 commit comments