33

it's any way to remove the package and or module name from sphinx doc?

Example: if exist a function called waa inside the module foo.bar, the rst code

.. automodule:: foo.bar
    :members:

will generate

foo.bar.waa()

and i want to output

waa()
barryhunter
  • 20,886
  • 3
  • 30
  • 43
JuanBC
  • 333
  • 3
  • 6

2 Answers2

52

You can change add_module_names to False in the file conf.py:

# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
add_module_names = False

Then foo.bar.my_function() will display as my_function().

Géry Ogam
  • 6,336
  • 4
  • 38
  • 67
Cole
  • 1,699
  • 1
  • 17
  • 21
  • 3
    is there a way to do this per-automodule, rather than at the conf.py level? – 001001 May 19 '20 at 22:49
  • 2
    This does it for the function/module/class itself, but not for arguments with type annotations. See https://stackoverflow.com/q/51394955/965332 – erb Jul 15 '20 at 07:57
  • To do it for specific objects and not globally, see: https://stackoverflow.com/a/74078513/939364 – Gallaecio Oct 15 '22 at 10:08
2

I have a package with submodules and I wanted to still display the submodule name in navigation, as there are multiple duplicate function names across submodules.

For

foo.bar.baz()

I need to display

bar.baz()

This makes

  • Navigation is easier as everything does not start with the same letter from the package name

  • Shorter names are more readable without repeating

For this, I created a custom Jinja filter and then modified autosummary's core templates to use it. The filter is injected through a monkey-patch in Sphinx's conf.py.

An example conf.py:

# Monkey-patch autosummary template context
from sphinx.ext.autosummary.generate import AutosummaryRenderer


def smart_fullname(fullname):
    parts = fullname.split(".")
    return ".".join(parts[1:])


def fixed_init(self, app, template_dir=None):
    AutosummaryRenderer.__old_init__(self, app, template_dir)
    self.env.filters["smart_fullname"] = smart_fullname


AutosummaryRenderer.__old_init__ = AutosummaryRenderer.__init__
AutosummaryRenderer.__init__ = fixed_init

Then here is the example from _templates/autosummary/module.rst:

{{ fullname | smart_fullname | escape | underline}}

Documentation for `{{ fullname }}` module.

.. automodule:: {{ fullname }}

See the full documentation for further examples.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435