Skip to main content

Sphinx directive that adds an interactive Run button to code files via a self-hosted codeapi instance

Project description

sphinxcontrib-coderun

A Sphinx extension that adds an interactive Run button to code files in your documentation. Powered by a self-hosted codeapi instance — you bring your own server, this extension wires it into Sphinx.

How it works

  1. You self-host a codeapi server (supports C, Python, Go, Rust, and many more).
  2. You add .. coderun:: directives to your .rst files.
  3. When a reader clicks Run, the code is sent to your codeapi server, executed in an isolated sandbox, and the output appears inline on the page.

Nothing is shared with third-party services — all execution happens on your own infrastructure.

Install

pip install sphinxcontrib-coderun

Configuration

In your conf.py:

extensions = [..., "sphinxcontrib.coderun"]

# URL of your self-hosted codeapi instance
coderun_url = "https://codapi.example.com"

# Default sandbox (matches a sandbox configured on your codeapi server)
coderun_sandbox = "gcc"

Both settings can be overridden per-directive (see below).

Usage

Replace .. literalinclude:: with .. coderun:: wherever you want a Run button:

.. coderun:: cprogs/hello.c
   :language: c

Override the sandbox or URL for a specific snippet:

.. coderun:: examples/hello.py
   :language: python
   :sandbox: python
   :url: https://codapi.example.com

All standard literalinclude options work — :lines:, :linenos:, :emphasize-lines:, etc.

Self-hosting codeapi

You need a running codeapi instance that the browser can reach. Quickstart:

# Pull the server and a sandbox image
docker pull nalgeon/codapi
docker pull codapi/gcc   # or python, go, etc.

# Run the server
docker run -p 1313:1313 -v /path/to/config:/opt/codapi nalgeon/codapi

For production deployment (Kubernetes, TLS, rate limiting) see the codeapi documentation.

Point coderun_url at the public URL of your server and you're done.

Development

Project structure

sphinxcontrib-coderun/
├── pyproject.toml
├── LICENSE
├── README.md
└── sphinxcontrib/
    ├── __init__.py               # namespace package
    └── coderun/
        ├── __init__.py           # directive + Sphinx setup()
        └── static/
            └── coderun.css       # Run button styling

Local setup

git clone https://github.com/uthcode/sphinxcontrib-coderun
cd sphinxcontrib-coderun
pip install -e .

To test against a Sphinx project, add to its conf.py:

extensions = [..., "sphinxcontrib.coderun"]
coderun_url = "http://localhost:1313"   # local codeapi for dev
coderun_sandbox = "gcc"

Building a distribution

pip install build twine
python -m build
# produces dist/sphinxcontrib_coderun-X.Y.Z.tar.gz
#          dist/sphinxcontrib_coderun-X.Y.Z-py3-none-any.whl

Validate before uploading:

python -m twine check dist/*

Publishing to PyPI

Create an API token at pypi.org/manage/account/token scoped to this project, then:

python -m twine upload dist/*
# Username: __token__
# Password: pypi-AgEI...

Releasing a new version

  1. Bump version in pyproject.toml.
  2. Delete dist/.
  3. Run python -m build.
  4. Run python -m twine upload dist/*.

License

MIT

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

sphinxcontrib_coderun-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinxcontrib_coderun-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file sphinxcontrib_coderun-0.1.0.tar.gz.

File metadata

  • Download URL: sphinxcontrib_coderun-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sphinxcontrib_coderun-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e9822528087cf4bdd59d6321d686a0373c9992742250e2582aae7eb13b021f31
MD5 1b29ed9e12ff480e40375f6752a4b716
BLAKE2b-256 7e69288953d8d810c52e3af86b36c8031cde16ffe6b5f8ccfab530df23453036

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinxcontrib_coderun-0.1.0.tar.gz:

Publisher: publish.yml on uthcode/sphinxcontrib-coderun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sphinxcontrib_coderun-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_coderun-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9c6dcc4db5a28d01f48881e1d38ce7db6f2f4221b80409a3b0ccb73464337120
MD5 f6dc731b82aa679dbab1fa92f1a87a8d
BLAKE2b-256 a2c910aea27f92585654b37edbccdc45344613a7de22c55ebe678041b615b1b5

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinxcontrib_coderun-0.1.0-py3-none-any.whl:

Publisher: publish.yml on uthcode/sphinxcontrib-coderun

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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