Skip to main content

Run Python code blocks and display the output directly within Sphinx documentation

Project description

sphinx-exec-directive

sphinx-exec-directive allows you to write code blocks in reStructuredText files, execute them during Sphinx compilation, and display the output directly within the generated documentation.

The 'default' code language is Python, but there is some limited support for other languages right now. Note that for other languages, you will need to have the corresponding executables on $PATH. See: Other Languages.

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. Don't do silly things with it.

Contents

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...
]

Basic usage: Python code blocks

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.

Reading code from a file

Instead of inserting the code literally into the RST sources, you can also put it in a separate file. The file path must be given relative to the top-level Sphinx directory (i.e. the directory which conf.py is in):

.. exec:: subfolder/my_script.py

The same conditions apply; anything you want to display must be printed to stdout.

Other languages

A few other processes are available, for running code in different languages. These all use the :process: PROCESS option, where PROCESS is one of the following. For these to work, you will need to have certain executables present in your PATH.

PROCESS Requirements Description
haskell runghc executable Pipes the code into runghc and captures stdout.
matlab matlab executable Creates a tempfile and runs non-interactive Matlab on it.
shell Pipes the code into sh. Note that the initial working directory will be your home directory.

An example of executing Haskell code can be found here.

I plan to generalise this (see #7) in the future.

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), specify a falsy value for the :cache option.

.. exec::
   :cache: false
   
   from datetime import datetime
   print(datetime.now())

Preserving context

Setting the :context: option to a truthy value will keep any objects in the current exec directive "alive" for the next one. Note that this only works for Python blocks, and is incompatible with caching (see above).

.. 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.6.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

sphinx_exec_directive-0.6-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sphinx-exec-directive-0.6.tar.gz
  • Upload date:
  • Size: 16.0 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

Hashes for sphinx-exec-directive-0.6.tar.gz
Algorithm Hash digest
SHA256 94ca384082eab7aa448886ad37f2cdc61894197df3892595f9b7d16a1ce66565
MD5 19c8fee2f988a17f57c91e0cfda6add2
BLAKE2b-256 55240d9d321186f97a4f9927b3394418ef10227ffda8ea65f1c8e4872ac50667

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sphinx_exec_directive-0.6-py3-none-any.whl
  • Upload date:
  • Size: 15.2 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

Hashes for sphinx_exec_directive-0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 d556b38810722625c45aacddea0604ccc3b2351a73c3ef0c17f9fca3fc614bdb
MD5 f6d049ac69844169f16ce6c87ca485e7
BLAKE2b-256 b32d5ab8fc1239bfa2acce67388115caa9d25e5eba4633426355337b7393ddd6

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