Skip to main content

Sphinx support for execution of python code from code blocks or files

Project description

sphinx-execute-code

Sphinx-execute-code 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.

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.

Options

Options right now (as of version 0.2) are:

linenos

If specified, will show line numbers

output_language

Customizes pygments lexxer for specified language (Eg, Javascript, bash)

hide_code

If specified, will hide the code block and only show results

hide_headers

If specified, hides the ‘Code’ and ‘Results’ caption headers around the literal blocks

filename

If specified, will load code from a file (relative to sphinx doc root) and ignore content.

execute_code

Running ‘execute_code’ as a directive allows the administrator to embed exact python code as if it was pasted in a normal code-block.

Executing python code and showing the result output:

.. execute_code::
    :linenos:

    print 'python highlight code'

    class Foo(object):
        def __init__(self):
            self.bar = 'baz'
        def out(self):
            print self.bar

    f = Foo()
    f.out()

Output language

Customizing the output syntax can be helpful to make it easy to document any other pygments lexxer - eg ini, javascript

We can customize the output language parser (for JSON/Javascript):

.. execute_code::
    :output_language: javascript

    print "'{foo-bar-baz}'"

Hiding code

You may want to hide the example code that is executing (avoiding highlighting/etc) and display the results only.

We can also hide the code input, showing only the executed code results:

.. execute_code::
    :hide_code:

    print 'This should not print the example code'

Suppressing output headers

Suppressing the ‘Headers’ outputs for Code and Results header:

.. execute_code::
   :hide_headers:

   foo = 32
   print 'This will hide the Code and Results text - and foo is %d' % foo

Executing python code from a file

execute_code also allows you to import a python file and execute it within a document.

Running a Python file from filename from the .py example:

.. execute_code::
   :filename: tests/example_class.py

This function also supports the argument ‘hide_filename’:

.. execute_code::
   :filename: tests/example_class.py
   :hide_filename:

Installation

Installation from source:

$ git clone git@github.com:jpsenior/sphinx-execute-code.git
$ python setup.py install

Installation from pypi:

$ pip install sphinx-execute-code

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_execute_code')

Project details


Release history Release notifications | RSS feed

This version

0.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx_execute_code_python3-0.3.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file sphinx_execute_code_python3-0.3.tar.gz.

File metadata

  • Download URL: sphinx_execute_code_python3-0.3.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for sphinx_execute_code_python3-0.3.tar.gz
Algorithm Hash digest
SHA256 ba75951cee5a92c9b635b1e39e755fe99ef0aa7bf6aea0ace3ffe692ea060d0d
MD5 27db3cccb091e9dad89011d91bdb38f7
BLAKE2b-256 068e845b3838ab2a1f79a4a0cf10a8de7a031b26aecc0aa7e030daf50fd6acfb

See more details on using hashes here.

File details

Details for the file sphinx_execute_code_python3-0.3-py3-none-any.whl.

File metadata

  • Download URL: sphinx_execute_code_python3-0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for sphinx_execute_code_python3-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 dc5dbc13b7c17157472d6fd47e7a6d816c2a2b654caf252cc45af5380a459b5b
MD5 7faa0443a55c4873400453194047cc85
BLAKE2b-256 cfcb27bb85bdef80fc24162e84e9e433e0a33cd98a032a346ebf5fd6b94794e0

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