質問

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()
役に立ちましたか?

解決

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().

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top