Sphinx support for execution of python code from code blocks or files. Original code by JP Senior.
Project description
sphinx-computron
Sphinx-computron is an extension for Sphinx that allows a document author to insert arbitrary python code samples in code blocks, or run python code from python files on the filesystem. The output is interpreted as ReST, as if the output of the executed code was copy-pasted in-place into the document.
This was written as an alternative to other code execution functions which relied on doctest formats, and attempts to be more flexible, similar to literal-block and code-block statements.
The original author is JP Senior. His version of the package appears to be unmantained so I decided to salvage it by making a hard fork. The name had to be changed to avoid collisions with his version published on PyPI.
The package is available on PyPI as sphinx-computron.
Options
- filename
If specified, will load code from a file (relative to sphinx doc root) and prepend that to the directive’s content before its execution.
- argv
Passes the specified whitespace-separated arguments to the script. If specified: sys.argv = [filename] + argv.split(). If not specified: sys.argv = [filename].
computron-injection
Executing python code and parsing output as ReST:
.. computron-injection:: print('*This is interpreted as ReST!*')
Whatever is written into stdout is collected and the injected in-place into the document. Stderr is not captured.
Executing python code from a file
computron-injection also allows you to import a python file and execute it within a document. The path is specified relative to the source ReST file where the directive is encountered.
Running a Python file in the same directory:
.. computron-injection:: :filename: my_class.py
With arguments ['Hello', 'world!']:
.. computron-injection:: :filename: my_class.py :argv: Hello world!
Activating on Sphinx
To activate the extension, add it to your extensions variable in conf.py for your project.
Activating the extension in sphinx:
extensions.append('sphinx_computron')
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file sphinx_computron-1.0.0.tar.gz
.
File metadata
- Download URL: sphinx_computron-1.0.0.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4325d94b46ac95f92f4440e1c5ee6c90d84321b20870e40c5153faa1d6cbadbe |
|
MD5 | dd0c8960dfa8dd16b70a57d0738eb165 |
|
BLAKE2b-256 | 9a1e51c961025101b53408150aa98552597529055e633ffaa7298e79a3076dfb |
File details
Details for the file sphinx_computron-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: sphinx_computron-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.26.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9233bf69abe752ded8c1d21c175c2a36eda51e5b48123d6c67a14d21e230810 |
|
MD5 | 38c47f0bf0442b10359c9aa0ff5af737 |
|
BLAKE2b-256 | 9d7d34359f9c9a8667f3012a975018eee8ae3af3d426b9f1497e589a05b64576 |