sphinx支持从代码块或文件执行python代码
sphinx_execute_code的Python项目详细描述
Sphinx执行代码
sphinx执行代码是sphinx的一个扩展,它允许文档作者 在代码块中插入任意python代码示例,或运行python代码 从文件系统上的python文件。
这是作为其他代码执行函数的替代而编写的 依赖于DOCTREST格式,并尝试更灵活,类似于 文字块和代码块语句。
警告
此模块允许您运行任意代码,应谨慎对待。
选项
现在的选项(从0.2版起)是:
- 行号
- 如果指定,将显示行号
- 输出语言
- 为指定语言(如javascript、bash)自定义pygments lexer
- 隐藏代码
- 如果指定,将隐藏代码块并仅显示结果
- 隐藏标题
- 如果指定,则隐藏“代码”和“结果”标题 文字块
- 文件名
- 如果指定,将从文件加载代码(相对于sphinx doc root) 忽略内容。
执行代码
运行'execute_code'作为指令允许管理员嵌入精确的 python代码,就好像它是粘贴在普通代码块中一样。
执行python代码并显示结果输出:
.. execute_code:: :linenos: print 'python highlight code' class Foo(object): def __init__(self): self.bar = 'baz' def out(self): print self.bar f = Foo() f.out()
输出语言
定制输出语法有助于简化文档 任何其他pygments lexer-例如ini、javascript
我们可以定制输出语言解析器(用于json/javascript):
.. execute_code:: :output_language: javascript print "'{foo-bar-baz}'"
隐藏代码
您可能希望隐藏正在执行的示例代码 (避免突出显示/etc)并仅显示结果。
我们还可以隐藏代码输入,仅显示执行的代码结果:
.. execute_code:: :hide_code: print 'This should not print the example code'
抑制输出头
抑制代码和结果头的“头”输出:
.. execute_code:: :hide_headers: foo = 32 print 'This will hide the Code and Results text - and foo is %d' % foo
从文件执行python代码
execute_代码还允许您导入一个python文件并执行 它在文档中。
从.py中的filename运行python文件示例:
.. execute_code:: :filename: tests/example_class.py
此函数还支持参数“hide_filename”:
.. execute_code:: :filename: tests/example_class.py :hide_filename:
安装
从源安装:
$ git clone git@github.com:jpsenior/sphinx-execute-code.git $ python setup.py install
从PYPI安装:
$ pip install sphinx-execute-code
在狮身人面像上激活
要激活扩展,请将其添加到conf.py中的extensions变量中 为你的项目。
在狮身人面像中激活扩展:
extensions.append('sphinx_execute_code')