Sphinx Extension - Directives to auto-evaluate Python code-blocks
Project description
sphinxcontrib-repl is an extension to Sphinx document generator tool. The extension introduces repl and repl-quiet directives to run Python REPL interpreters during Sphinx builds the documentation. The content of the directives will be automatically evaluated line-by-line in the interpreter, and repl blocks will add what would be printed on the interpreter in the output document.
Contents
Installation
Install from PyPI:
pip install sphinxcontrib-repl
Then, inside your Sphinx conf.py, add sphinxcontrib_repl to your list of extensions (note: underscores not hyphens).
extensions = [
"sphinxcontrib_repl",
# other extensions...
]
Basic Usage
To run Python code in the interpreter, list the code in a repl block:
.. repl::
2*3+4
x=5
f"{x=}"
First of such block will invoke a dedicated Python interpreter process, which will continue to run in the background for each rST document until the document is fully parsed. With the interpreter, the above block of code will produce the following document block:
>>> 2*3+4
10
>>> x=5
>>> f"{x=}"
'x=5'
As the interpreter process will run continuously, the variables will carry between blocks. For example, after the above repl block, the variable x may be used in any subsequent repl blocks (unless you delete it):
.. repl::
x+4
will produce:
>>> x+4
9
A REPL block may contain (potentially nested) condition/loop statements:
.. repl::
for i in range(5):
if i>2:
i+1
outputs
>>> for i in range(5):
... if i>2:
... i+1
...
4
5
Note that a trailing empty line to terminate the indented block will be inserted automatically.
Finally, to hide nuisance operations (e.g., importing common libraries), use repl-quiet block:
.. repl-quiet::
import numpy as np
After this block, the Numpy package is loaded onto the interpreter, but the import line will not be printed in the document.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for sphinxcontrib_repl-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ae96358acb100934117c475c80fa1165188d10fe99c493a95c1745e9a806c8b |
|
MD5 | 1f87952d0665e47de040a83a19269224 |
|
BLAKE2b-256 | 3de5f2d9e04eb6de95fe0bc93b6a7cddd12b14adefe6d57ef20cb16fae783a8f |