Interactive code examples in Docusaurus, MkDocs, Sphinx, Mintlify and WordPress
Readers learn faster when they can change an example and run it. Add one script to your docs site, wrap a code block in a div, and the block becomes an editor that runs the code in a sandbox and shows its output. No ads, no reader sign-in, and 46 languages including Python, Java, C++, Go and Rust.
How it works
- Create a widget key on the Widgets page of your dashboard, restricted to your docs domain. Widget keys are public by design; they can't call the REST API.
- Load the script once for the whole site, with your tool's setting below.
- Wrap each example in
<div class="runcode">with your key and the language. In Markdown and MDX, keep your normal fenced code block inside it: readers see it highlighted while the page loads, and the widget takes the code from it, indentation and all.
Docusaurus, MkDocs and WordPress have free plugins that load the script and write the div for you: in
Docusaurus and MkDocs you only add your key and write runnable after a code block's language.
Their sections below link to the guides.
Each example connects only when it scrolls near the screen, and pages that change without a reload are handled for you. The widget docs cover the options: input, height, themes, read-only code and hidden setup code.
Docusaurus
The simplest way is the free docusaurus-plugin-onlinecompiler: install it, add your widget key
to docusaurus.config.js, and write runnable after a code block's language
(```python runnable). It loads the script only on pages that need it. How to add runnable code examples to Docusaurus walks through it. Without the plugin, add the script in docusaurus.config.js:
// docusaurus.config.js
const config = {
scripts: [{ src: "https://onlinecompiler.io/widget.js", defer: true }],
// ...the rest of your config
};Then wrap a code block in any .md or .mdx page. Use className, start the div at the beginning of a line, and leave a blank line on each side
of the code block, so MDX treats it as Markdown:
<div className="runcode" data-key="YOUR_WIDGET_KEY" data-lang="python-3.14">
```python
name = input("Your name? ")
print(f"Hello, {name}!")
```
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>Don't put code in a JSX template literal such as <pre>{`...`}</pre>:
MDX removes two spaces from each line of a multi-line expression, and tabs entirely, which breaks
Python.
MkDocs and Material for MkDocs
The simplest way is the free mkdocs-onlinecompiler plugin: install it, add your widget key to mkdocs.yml, and write runnable after a code block's language
(```python runnable), even inside admonitions and content tabs. How to add runnable code examples to MkDocs walks
through it. Without the plugin, load the script with extra_javascript, and turn on md_in_html so a code block inside a div is still Markdown:
# mkdocs.yml
extra_javascript:
- https://onlinecompiler.io/widget.js
markdown_extensions:
- md_in_htmlThen add the markdown attribute to the div:
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="python-3.14" markdown>
```python
name = input("Your name? ")
print(f"Hello, {name}!")
```
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>This works with Material's instant navigation and with line numbers turned on. Markdown expands tabs in code blocks to spaces before the widget sees them.
Sphinx
Add the script in conf.py:
# conf.py
html_js_files = [("https://onlinecompiler.io/widget.js", {"defer": "defer"})]reStructuredText can't nest a code block inside raw HTML, so use the raw directive with
the code in a <script type="text/plain">, which needs no escaping of < and &. The widget removes the shared indentation:
.. raw:: html
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="g++-15">
<script type="text/plain">
#include <iostream>
int main() {
std::cout << "Hello from C++\n";
}
</script>
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>Mintlify
Mintlify runs every .js file in your docs folder on each page, so load the script from
one:
// runcode.js, anywhere in your docs folder. Mintlify runs it on every page.
const script = document.createElement("script");
script.src = "https://onlinecompiler.io/widget.js";
script.defer = true;
document.head.appendChild(script);Then wrap a code block in any page, as in Docusaurus:
<div className="runcode" data-key="YOUR_WIDGET_KEY" data-lang="python-3.14">
```python
name = input("Your name? ")
print(f"Hello, {name}!")
```
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>WordPress
The simplest way is the free OnlineCompiler.io Code Runner plugin: it adds a Code runner
block and an [onlinecompiler] shortcode, loads the script only where it is needed and keeps
the code exactly as written. How to add an online
compiler to WordPress walks through it. Without the plugin, in the block editor, add a Custom HTML block. Escape < as < and & as & inside the <pre>, or use <script type="text/plain"> as in the Sphinx
example:
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="php-8.5">
<pre><?php
echo "Hello from PHP\n";</pre>
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>Load the script once for the site, either with a header and footer plugin such as WPCode, or from your theme:
// In your (child) theme's functions.php
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script('onlinecompiler-widget', 'https://onlinecompiler.io/widget.js', [], null, ['strategy' => 'defer']);
});WordPress strips scripts, and may strip other markup, from users who can't post unfiltered HTML. On a single site, Administrators and Editors can; on multisite, only Super Admins can.
Run Python in a WordPress post
For a Python example, set data-lang to python-3.14 in the same kind of block:
<div class="runcode" data-key="YOUR_WIDGET_KEY" data-lang="python-3.14">
<pre>prices = [4.50, 12.00, 7.25]
print("Total:", sum(prices))
print("Most expensive:", max(prices))</pre>
<a data-credit href="https://onlinecompiler.io/">Runs on OnlineCompiler.io</a>
</div>The code runs in a sandbox on OnlineCompiler.io's servers, not on your WordPress host, so the host needs no Python and nothing runs next to your site's files or database. That also means it can't run Python as part of WordPress itself, to build a page or handle a form: it gives readers examples they can edit and run.
Interactive code snippets on static sites and other pages
Hugo, Jekyll, Astro, Eleventy and hand-written HTML pages work the same way: add <script src="https://onlinecompiler.io/widget.js"></script> to your layout, and a <div class="runcode"> around a <pre> or a rendered code block
wherever you want an example. The widget docs have the plain HTML version.
If your platform strips scripts but allows iframes, use the iframe embed,
which carries the code in its URL.
Medium, dev.to and Substack take neither: they embed only the sites on their own lists, so the editor can't go in a post there. Link to the example instead: Share on any language page, such as the Python online compiler, gives a link anyone can open and run for 90 days.
Input, exercises and checks
- Standard input: add a
<pre data-stdin>inside the div for programs that read input; readers can change it in the Input tab. In MDX, write multi-line input as a one-line string with\n, such as<pre data-stdin>{"3\n10 20 30"}</pre>. - Hidden setup code: a
data-templateelement holds code the reader doesn't see, such as imports or a test harness, with##CODE##where their code goes. See templates. - Checking answers: listen for the
runcode:resultevent to compare the output with the answer you expect. See the JavaScript API.
Frequently asked questions
Does the widget work with client-side navigation?
Yes. Docusaurus, Mintlify and Material for MkDocs with instant navigation swap pages without reloading. The widget starts any example added to the page and closes the connection of any example taken off it, so you need no route hooks.
Do readers need an account?
No. Readers edit and run the code without signing in, and there are no ads. Only you need an account, to create the widget key.
Is it safe to put the widget key in my docs?
Yes. Widget keys are made to be public: they only work over the widget's WebSocket and cannot call the REST API. Restrict the key to your docs domain on the Widgets page so other sites can't use it.
Can I run Python in a WordPress post?
Yes, as an example readers can edit and run: install the free OnlineCompiler.io Code Runner plugin and add a Code runner block set to Python, or load the widget script yourself and add a Custom HTML block with a runcode div whose data-lang is python-3.14. The code runs on OnlineCompiler.io's servers, not on your WordPress host, so the host needs no Python, and it can't run Python as part of WordPress itself.
Which languages can the examples use?
Python, C, C++, Java, C#, F#, Go, Rust, PHP, Ruby, Haskell, TypeScript, JavaScript, SQL, DuckDB, Bash, Fortran, Visual Basic, Kotlin, R, Swift, COBOL, Perl, Julia, Pascal, Octave, Assembly, Lua, OCaml, Common Lisp, Erlang, Elixir, Prolog, D, FreeBASIC, Dart, Clojure, Groovy, Scala, Zig, Nim, Crystal, Ada, PowerShell, Racket, Objective-C. Set data-lang to the compiler id from the docs, such as python-3.14 or openjdk-25.
How much does it cost?
Runs from the widget count toward your account's monthly allowance: 100,000 runs a month on the free plan, which allows commercial use.