Run Python code blocks and display the output directly within Sphinx documentation
Project description
sphinx-exec-directive
Run Python code blocks and display the output directly within Sphinx documentation.
This is based very strongly on matplotlib's plot_directive
extension, but is used for running code instead of generating plots.
Note: This allows arbitrary code execution using exec()
. Don't do silly things with it.
Installation
Install from PyPI:
pip install sphinx-exec-directive
or manually:
git clone https://github.com/yongrenjie/sphinx-exec-directive
cd sphinx-exec-directive
pip install .
Then, inside your Sphinx conf.py
, add sphinx_exec_directive
to your list of extensions (note: underscores not hyphens).
extensions = [
sphinx_exec_directive,
# other extensions...
]
Example usage
A short example will suffice. Putting
.. exec::
print(1 + 1)
into your RST file will generate two literal blocks, one with the 'input' source code, and one with the captured stdout. It looks pretty basic, but it does the job:
Note that objects must be printed to stdout, or they will not be displayed. This behaviour therefore differs slightly from the functionality in a Jupyter notebook (where the last line is evaluated and the result displayed automatically), or the interactive Python console. If nothing is printed to stdout (or only empty space is) then the output literal block will be omitted.
Caching
Outputs are cached by default, UNLESS context preservation has been requested in any part of the same document (see #4 for rationale).
To turn this off on a per-codeblock basis (e.g. if the code depends on the time which it is run at), use the :cache: false
option.
.. exec::
:cache: false
from datetime import datetime
print(datetime.now())
The context
option
Use this to preserve objects between different exec
directives.
Setting this to true
(or True
or 1
or yes
) will keep any objects in the current exec directive "alive" for the next one.
.. exec::
:context: true
x = 5
print(x)
Some other text goes here... Let's print x again...
.. exec::
print(x)
It will work.
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
File details
Details for the file sphinx-exec-directive-0.4.tar.gz
.
File metadata
- Download URL: sphinx-exec-directive-0.4.tar.gz
- Upload date:
- Size: 13.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 611f2735981989f04df4e495e2df00f341ccf2355ea246930d60ce1a75b42cf1 |
|
MD5 | 8de7eb60130ec82491b0269e01189721 |
|
BLAKE2b-256 | 1f7165a31f9cda2aacefd3160f5d7e8a2d0ce44bcee3e4bb10f2512607ca36d2 |
File details
Details for the file sphinx_exec_directive-0.4-py3-none-any.whl
.
File metadata
- Download URL: sphinx_exec_directive-0.4-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00c5762ac344ee649ab77b9b71c626749f3ae60e3abc468e3b470e03038ff5f3 |
|
MD5 | 66b49292fa87eff0e35cdac220479ae4 |
|
BLAKE2b-256 | 224bcda5bcb2c498dc6f57c632888a2ac4f208f2d12a6166c6e3bcb0e540563e |