Skip to main content

Sphinx extension exec-to-file

This Sphinx extension aims to generate images or data to be included in Sphinx documents, such as CSV files generated by a script calling an API.

Installation

We recommend the use of environment (a.k.a. "venv").

pip install sphinx-exec-to-file

Add the extension in your Sphinx configuration file conf.py:

extensions = [
	"sphinxcontrib.exec_to_file",
]

Presentation

This extension introduces 2 directives: exec-code and exec-file.

The exec-code directive allows to run a shell command or a code snippet. The language is specified as argument of the directive. To run the code as a shell command the language must be sh, which is the default.

The exec-file directive allows to run a file. The path is given as argument of the directive, relative to the Sphinx build directory. The language is specified as option of the directive. Use python to execute a python script file.

The command/code/file is executed in the Sphinx build directory, i.e. where the 'conf.py' file is located.

The files listed in the capture options capture-file, capture-stdout, and capture-stderr are replaced in the executed command/code/file, so they can be generated at the correct location. E.g., for the capture of assets/file.csv, the replacement search for <file.csv> in the text, and write sphinx-build-dir/assets/file.csv instead, with the actual sphinx-build-dir, so other directives can use the file.

The intended usage for captured files is to be used with other Sphinx directives like csv-table, image, literalinclude.

Usage example

Execute a command

.. exec:code::
	:capture-file:
		assets/file.csv
		assets/second file.csv
	:hide-code:

	prog.exe <file.csv> <second file.csv>

The prog.exe program shall be accessible using the PATH environment variable, or be in the Sphinx build directory, where the command is executed.

Execute a code snippet

.. exec:code:: python
	:capture-stdout: assets/file1.txt
	:capture-stderr: assets/file2.txt

	import sys
	print("hello world")
	print("hello errors", file=sys.stderr)

Execute a file

.. exec:file:: scripts/generate_image.py
	:capture-file: assets/file.png
	:language: python

Show the generated table

.. csv-table::
	:file: assert/file.csv

Show the generated text

.. literalinclude:: assets/file1.txt

Show the generated image

.. image:: assets/file.png

Show how it was generated

exec:code and exec:file inherit from code-block and literalinclude. You can display how the file was generated using their directive options. If you don't want it in your output, use the option :hide-code:.

Extension configuration

You can configure the extension with the following option in your conf.py. The configuration values shown are the default ones.

exec_code_runners

exec_code_runners = {
    None: ["sh", "-c"],  # the default language
    "sh": ["sh", "-c"],
    "python": [executable, "-c"],
}

exec_file_runners

exec_file_runners = {
    None: ["sh"],  # the default language
    "sh": ["sh"],
    "python": [executable],
}

exec_code_file_markers

exec_code_file_markers = "<>"

To use other characters to identify the files to be captured, instead of the default <file.png>. E.g. "{}" would allow to use {file.png}.

Directive options

The exec:code and exec-file directives supports the same options.

They also inherits the options of the code-block and literalinclude directives.

capture-file

The capture-file identifies the list of files to replace in the code/command/file.

The option takes one file path per line. They are replace in the executed text where the same file path is written surrounded by the markers (as configured by exec_code_file_markers).

capture-stdout

Similar to the capture-file option, but capture the standard output of the executed text.

capture-stderr

Similar to the capture-file option, but capture the standard error output of the executed text.

It can have the same value as the capture-stdout option, to capture both outputs in the same file. The apparition order of the outputs is preserved in the captured file.

ignore-exit

By default, the executed code shall return the exit-code status of 0. This option disable this check.

timeout

By default, the executed code can run indefinitely. This option set a timer on the execution of the directive.

hide-code

By default, the code/command/file is shown using Sphinx's code-block and literalinclude directives. THis option disable this, so the directive shows nothing in the built documentation.

Development

Install necessary tools with pip install -e .[dev].

Run tests with pytest or coverage. Run code analysis with autopep8, isort, mypy and pylint. These tools are pre-configured, no options are required to run them, you just have to give the path to analyse.

You can trigger the run of all these tools with precommit:

pip install pre-commit && pre-commit install

To build and distribute the package use:

pip install build twine
python -m build
twine upload dist/*

Metadata

Release files for sphinx-exec-to-file 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sphinx-exec-to-file 1.0.0
File Size Uploaded Provenance Actions
sphinx_exec_to_file-1.0.0.tar.gz 7.8 kB Without provenance Details for sphinx_exec_to_file-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sphinx-exec-to-file 1.0.0
File Interpreter ABI Platform Actions
sphinx_exec_to_file-1.0.0-py3-none-any.whl Python 3 none any Details for sphinx_exec_to_file-1.0.0-py3-none-any.whl

Total release size: 18.0 kB

Release files / sphinx_exec_to_file-1.0.0.tar.gz

Download URL sphinx_exec_to_file-1.0.0.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
adea4c5513e30a036bbe426e9c15e81066f8d7f96d244f93d7842dce6bab4021
BLAKE2b-256 checksum
How to use checksums
68037fdd63edaae8102521ab9f33df5b6d29e23d6c7fc97b5e6fcc4f4decda99
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.0rc1

Release files / sphinx_exec_to_file-1.0.0-py3-none-any.whl

Download URL sphinx_exec_to_file-1.0.0-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e74c5d834b23b3ef9b707d6f87351a7b7d40b1b23982f6e3df59497460a755ea
BLAKE2b-256 checksum
How to use checksums
10fb275344922992e0380178a40863fc0410cc1c91c40fca6714b609a47d1cba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.11.0rc1

Security for sphinx-exec-to-file 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page