2012-09-24 42 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