在sphinx的头函数声明部分包含python源代码

2024-10-02 08:24:57 发布

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

我是斯芬克斯的新手。 我面临的情况是,我有一个复杂的函数,在sphinx文档中,我想展示一些直接使用该函数的示例。在

def myComplicatedFunction(status):
    """Here I have a brief description

    :param status: explanation

    :returns: explanation
    """
    <<< code >>>

    return statstics

在sphinx部分的某个地方,我想包括一些python代码,它显示示例输入和示例输出。但是无论我做什么(使用``)作为文档,它都不能得到语法高亮显示。它只是作为一个简单的文本,使阅读困难。在

我应该如何在sphinx中包含python代码示例?


Tags: 函数代码文档示例heredefhavestatus
2条回答
.. code-block:: language
      def foo()....

适用于python,但请确保安装了pygments。在

在somethingconf.py配置文件这两个语句很有趣:

^{pr2}$

根据输出,写下

options['add_syntax_highlighting'] = True

模拟Python交互会话;以>>>开头的块将自动视为Python源代码:

def myComplicatedFunction(status):
    """Here I have a brief description

    :param status: explanation

    :returns: explanation

    >>> myComplicatedFunction('status value')
    {'foo': 2, 'bar': -400}

    """
    # Code

    return statistics

相关问题 更多 >

    热门问题