Skip to main content

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

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:

Example sphinx-exec-directive output

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.

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.

Example sphinx-exec-directive output using context flag

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

sphinx-exec-directive-0.1.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

sphinx_exec_directive-0.1-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file sphinx-exec-directive-0.1.tar.gz.

File metadata

  • Download URL: sphinx-exec-directive-0.1.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.2

File hashes

Hashes for sphinx-exec-directive-0.1.tar.gz
Algorithm Hash digest
SHA256 057e0f752733d08b51b1ea748a56ac5e50b4a80dc9ee0ea27a2e49bf0a3cbbd7
MD5 bbf678593012436de81dd856c7545514
BLAKE2b-256 de8782b07bf892f48ea8039251821d6d73522f37f517f59ec35df28e2c4f77d2

See more details on using hashes here.

File details

Details for the file sphinx_exec_directive-0.1-py3-none-any.whl.

File metadata

  • Download URL: sphinx_exec_directive-0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.2

File hashes

Hashes for sphinx_exec_directive-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 505e98f5110e2d1fc52955707880ce1c5ca0f5ce9d21ecffc8ec5e9501a70ecf
MD5 d6876b3b34dcbf83c86b32e4f40c4861
BLAKE2b-256 52d42d993f0d3fcdea16862fb1e7d6c00d1ca4c654ed2aafff6db320c9979c18

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page