2012-09-24 37 views
0

我是狮身人面像的新手。 我面临的情况是,我有一个复杂的功能,并在我希望显示的spphinx文档中直接显示该函数的一些示例用法。在sphinx的头函数声明部分包含python源代码

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

    :param status: explanation 

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

    return statstics 

在sphinx部分的某处我想包括一些显示示例输入和示例输出的python代码。但无论我做什么(使用``::)作为文档建议它不会得到语法突出显示。它只是包含在简单的文本中,这使得阅读变得困难。

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

+0

显示你尝试过什么。您的标记可能包含错误。 – delnan

+0

在[this](http://openalea.gforge.inria.fr/doc/openalea/doc/_build/html/source/sphinx/rest_syntax.html#figure-directive)中,他们使用指令'.. code- block :: python'和python代码是缩进的。但这可能是因为代码不使用'>>>/...'提示符。如果你使用这些,那么你应该使用'::'并用一个缩进来编写带有提示符的代码。 – Bakuriu

回答

2

模拟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 
+0

我试了一下,它工作正常,但如何>>>本身显示在文档中? – theAlse

+0

因为这是由设计。使用python交互式shell来编写代码是很常见的做法;我们一直在这里使用它。 :-)不要只用'>>>'来启用语法高亮,用它来说明你的函数的交互使用。 –

+0

谢谢,我现在明白了! – theAlse

0
.. code-block:: language 
     def foo().... 

作品蟒蛇,但要确保你已经安装Pygments来做。

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

pygments_style = 'sphinx' 

,并根据输出写像

options['add_syntax_highlighting'] = True