斯芬克斯:文字包含块内的按钮

2024-05-23 20:37:25 发布

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

我有一个桌面应用程序和一个用Sphinx编写的用户指南。通过使用与literalinclude块一起导入的代码块来解释API

我希望在每个代码块内部或附近有一个按钮。该按钮用于在我们的应用程序中打开相应的示例

到目前为止,我所取得的(见下文)是代码块上方的一个按钮,每次使用literalinclude语句时都必须手动包含该按钮

如何将按钮放在代码块内部而不是上面?

一个特别漂亮的结果是像Chris Holdgraf's Sphinx copybutton这样的透明按钮。但是现在我已经很高兴拥有这个按钮了,因为它位于代码块的右上角


目前情况的最简单示例

screenshot of result

按钮呈现在代码块上方。但是我想把它放在里面(在右上角)

文件夹结构:

conf.py
index.rst
source_file.py
load_source.py

index.rst的内容:

:load_example:`source_file.py`  

.. literalinclude:: source_file.py

source_file.py的内容:

import numpy as np

print "hello extension"

for i in range(3):   
   print np.sin(i)

load_souce.py扩展文件的内容:

from docutils import nodes

# HTML code to generate button
button_raw = """
<script>
function openScriptFunction() {
   callBackHandler.openScript("%s");
}
</script>

<button onclick="openScriptFunction()">Load %s</button>
"""

def setup(app):
    app.add_role('load_example', load_example_script)

def load_example_script(name, rawtext, text, lineno, inliner, options={}, content=[]):
    node = nodes.raw(rawsource = button_raw%(text, text), text = button_raw%(text, text), format="html")
    return [node], []

这个扩展被extensions = ['load_source']包含在conf.py中



Tags: 代码textpy应用程序示例source内容example