简体   繁体   English

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

[英]Exclude header comments when generating documentation with sphinx

I am trying to use sphinx to generate documentation for my python package. 我正在尝试使用sphinx为我的python包生成文档。 I have included well documented docstrings within all of my functions. 我在所有函数中都包含有据可查的文档字符串。 I have called sphinx-quickstart to create the template, filled in the template, and called make html to generate the documentation. 我调用了sphinx-quickstart来创建模板,填充了模板,并调用了make html来生成文档。 My problem is that I also have comments within my python module that are also showing up in the rendered documentation. 我的问题是我的python模块中也有注释,这些注释也显示在呈现的文档中。 The comments are not within the functions, but rather as a header at the top of the .py files. 注释不在函数内,而是作为.py文件顶部的标头。 I am required to have these comment blocks and cannot remove them, but I don't want them to show up in my documentation. 我必须具有这些注释框,并且不能删除它们,但是我不希望它们出现在我的文档中。

I'm current using automodule to pull all of the functions out of the module. 我目前正在使用automodule模块将所有功能拉出模块。 I know I can use autofunction to get the individual functions one by one, and this avoids the file headers, but I have a lot of functions and there must be a better way. 我知道我可以使用autofunction功能来逐个获取单个功能,这样可以避免文件头,但是我有很多功能,因此必须有更好的方法。

How can I adjust my conf.py file, or something else to use automodule , but to only pick up the functions and not the comments outside of the functions? 如何调整conf.py文件或其他要使用automodule ,而只选择功能而不是功能之外的注释?

This is what my .py file looks like: 这是我的.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]

This is my the auto-generated documentation looks like: 这是我自动生成的文档,如下所示:

Auto-Generated Documentation

This code is a part of a proj repo.

https://github.com/CurtLH/proj

author: greenbean4me date: 2018-02-07

hammer.hit(nail)

    Hit the nail on the head

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

    nail : str
        Can be any word with any length

    x : str
        First character in the string

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

For a more comprehensive example, check out this example repo on GitHub: https://github.com/CurtLH/proj 有关更全面的示例,请在GitHub上查看以下示例存储库: https : //github.com/CurtLH/proj

As far as I know, there is no way to configure autodoc not to do this. 据我所知,没有办法配置autodoc不这样做。

There is, however, a simple workaround: Adding an empty docstring at the top of your module. 但是,有一个简单的解决方法:在模块顶部添加一个空的文档字符串。

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

https://github.com/CurtLH/proj

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

It's barely noticeable, and will trick autodoc into not displaying your module's real docstring. 这几乎没有引起注意,并且会欺骗autodoc使其不显示模块的真实文档字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM