使用sphinx生成文档时排除标题注释

2024-09-28 01:27:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用sphinx为我的python包生成文档。我在我的所有函数中都包含了文档记录良好的docstring。我调用了sphinx-quickstart来创建模板,填充了模板,并调用了make html来生成文档。我的问题是我的python模块中也有注释,这些注释也显示在呈现的文档中。注释不在函数中,而是作为.py文件顶部的头。我必须有这些评论块,不能删除它们,但我不希望它们出现在我的文档中。在

我目前正在使用automodule将所有函数从模块中拉出。我知道我可以使用autofunction逐个获取各个函数,这避免了文件头,但我有很多函数,必须有更好的方法。在

如何调整我的conf.py文件,或者其他什么东西来使用automodule,但只拾取函数而不是函数外部的注释?在

我的.py文件如下:

"""
This code is a part of a proj repo.

https://github.com/CurtLH/proj

author: greenbean4me
date: 2018-02-07
"""

def hit(nail):

    """
    Hit the nail on the head

    This function takes a string and returns the first character in the string

    Parameter
    ----------
    nail : str
       Can be any word with any length

    Return
    -------
    x : str
       First character in the string

    Example
    -------
    >>> x = hit("hello")
    >>> print(x)
    >>> "h"
    """

    return nail[0]

这是我的自动生成的文档如下所示:

^{pr2}$

要获得更全面的示例,请查看GitHub上的repo示例:https://github.com/CurtLH/proj


Tags: 模块文件the函数文档pyhttps模板
1条回答
网友
1楼 · 发布于 2024-09-28 01:27:21

据我所知,没有办法配置autodoc不这样做。在

但是,有一个简单的解决方法:在模块顶部添加一个空的docstring。在

""""""  # <- add this
"""
This code is a part of a proj repo.

https://github.com/CurtLH/proj

author: greenbean4me
date: 2018-02-07
"""

它几乎看不到,并且会欺骗autodoc不显示模块的真实docstring。在

相关问题 更多 >

    热门问题