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.1.tar.gz (5.4 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.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sphinxcontrib_coderun-0.1.1.tar.gz
  • Upload date:
  • Size: 5.4 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.1.tar.gz
Algorithm Hash digest
SHA256 5e0b73d9f8b94499c8daa7896bb5281d3f7c6208b2ca7e83fc085e0c23a1aa7a
MD5 8e3579fd1af5d25d89fbb0819baf77b8
BLAKE2b-256 7d25d201f348882ee7bf5bc2ad54aedf4d83dcc77e2477dc9ff3374ea930bc81

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinxcontrib_coderun-0.1.1.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.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinxcontrib_coderun-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e80f60c3b5bdb70a379fa79b6ee13b19b8986d9f6e44654de320766b0f79150b
MD5 d3e82093693106bb0d512fed258afbff
BLAKE2b-256 e3552845c429aa9d2dcfa47c07a78834ea06242e6efbd4ae477284cbb8f76841

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinxcontrib_coderun-0.1.1-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