How to preserve line breaks when generating python docs using sphinx

前端 未结 6 1677
被撕碎了的回忆
被撕碎了的回忆 2020-12-05 04:10

I am using Sphinx for generating docs for a python project. The output html is not preserving the line breaks which are present in the docstring. Example:

Code

6条回答
  •  星月不相逢
    2020-12-05 04:55

    This answer comes late, but maybe it'll still be useful to others.

    You could use reStructuredText in your docstrings. This would look something like

    :param arg1: arg1 description
    :type arg1: str
    :param arg2: arg2 description
    :type arg2: str
    

    From the looks of your example however it seems you're using the Google Style for docstrings (http://google-styleguide.googlecode.com/svn/trunk/pyguide.html?showone=Comments#Comments).

    Sphinx does not natively support those. There is however an extension named napoleon that parses Google and Numpy style docstrings at https://pypi.python.org/pypi/sphinxcontrib-napoleon.

    To use the extension you have to append 'sphinxcontrib.napoleon' to the extension-list in your Sphinx conf.py (usually doc/source/conf.py), so it becomes something like

    extensions = [                                                                  
    'sphinx.ext.autodoc',                                                       
    'sphinxcontrib.napoleon',                                                   
    'sphinx.ext.doctest',                                                                                                             
    ]
    

提交回复
热议问题