Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - Customize templates for `sphinx-apidoc`

I've recently tried using sphinx-apidoc from Sphinx to help generate Sphinx specific reStructuredText from the API of a Python project.

However, the result I'm getting is:

Default look of <code>sphinx-api</code> result

Anyone know if I can customize the template sphinx-api uses for its output? Specifically, I'd like to:

  • Get rid of all the "Submodules", "Subpackages" and "Module contents" headings, and
  • Have the results from docstring in my __init__.py files appear directly under the packages, so that if I click a package name, the first thing I see is the package documentation. At the moment, this documentation is placed under the slightly weird "Module contents" heading at the very end of each package section.

The "Submodules" and "Subpackages" headings are redundant I think, since the normal headings for packages/modules is "xxx.yyy package" and "xxx.yyy.zzz module".

The structure I would like for the above small example is

  • orexplore.components package
    • orexplore.components.mbg120 module
  • orexplore.simulators package
    • orexplore.simulators.test package
      • orexplore.simulators.test.mbg120 module
    • orexplore.simulators.mbg120 module

Where clicking the packages, the first thing I'd see on the page would be the package documentation.

Or maybe even just

  • orexplore.components
    • orexplore.components.mbg120
  • orexplore.simulators
    • orexplore.simulators.test
      • orexplore.simulators.test.mbg120
  • orexplore.simulators.mbg120

if there was some way to visually distinguish packages/modules (color? emblem?) instead of the quite wordy " package" and " module".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I implemented better-apidoc, a patched version of the sphinx-apidoc script that adds full support for templates.

It adds a -t/--template option, allowing to pass a template directory that must contain template files package.rst and module.rst. See package.rst and module.rst for an example. These render to e.g. http://qnet.readthedocs.io/en/latest/API/qnet.algebra.operator_algebra.html.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...