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

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:

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.

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 processes

Currently simple Haskell code can be run: to do this, use :process: haskell as follows. You will need to have runghc installed and available in your PATH.

.. exec::
   :process: haskell

   main :: IO ()
   main = print $ take 10 fibs
     where
       fibs = 0 : scanl (+) 1 fibs

I plan to generalise this (see #7).

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 between exec blocks

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

Uploaded Source

Built Distribution

sphinx_exec_directive-0.5-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for sphinx-exec-directive-0.5.tar.gz
Algorithm Hash digest
SHA256 c28410cfce9891f133cd749e3760d10dc8fabe8d32eff1888069d045b1e3c996
MD5 72fc39a7cfdba1273872a63c2cff36b4
BLAKE2b-256 fab74d50e88fa4efec6ffe409f2cb691e50ef131bbce9091512ca9bec1346b3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sphinx_exec_directive-0.5-py3-none-any.whl
  • Upload date:
  • Size: 14.3 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 fcccfa764461f0db9ee7c9b26befa0f2bf6da638c30ba9f1d5c37e629e38a562
MD5 f557a6483d1da275ef10d1c9d69eb57b
BLAKE2b-256 7836687595b316e3c48cc18215f4865c8d54c3e3e26d026459371d42fc5efeb0

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