Skip to content

About

Docusaurus plugin for runnable, interactive code examples: write ```python runnable and readers edit and run code in 46 languages (Python, JavaScript, C++, Java, Go, SQL) in your docs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Runnable code blocks for Docusaurus: interactive code examples in 46 languages

docusaurus-plugin-onlinecompiler turns the code examples in your Docusaurus docs into interactive ones readers can edit and run, in Python, JavaScript, C++, Java, Go, SQL and 46 languages in all. Add the word runnable to a code block and readers get a live code editor with a Run button in the page, like a code playground, with no account and nothing to install.

A Docusaurus docs page with a runnable Python example and its output after Run

```python runnable
for n in range(1, 4):
    print(n, "squared is", n * n)
```

The code runs on OnlineCompiler.io, a code execution service, in an isolated sandbox with no network access. Nothing runs on your server or in the reader's browser, so the same docs can run C++, Rust or SQL as easily as JavaScript.

From 0.2.0 each editor ends with a small "Runs on OnlineCompiler.io" link, written into the page's HTML; the editor shows it as its credit line, as it did before.

Install

npm install docusaurus-plugin-onlinecompiler

Add it to docusaurus.config.js with your widget key:

export default {
  // ...
  plugins: [['docusaurus-plugin-onlinecompiler', { key: 'YOUR_WIDGET_KEY' }]],
};

Create a free widget key on the Widgets page of your OnlineCompiler.io dashboard (sign in with Google), and set its allowed domain to your docs site's domain so other sites can't use it.

Write runnable examples

Add runnable after the language of any fenced code block, in .md or .mdx pages:

```cpp runnable
#include <iostream>

int main() {
    std::cout << "Hello from C++\n";
}
```
  • The block's language picks the compiler: python runs Python 3.14, cpp runs C++ (G++ 15), js runs Node.js 24, sql runs SQLite. Compiler ids work too: python-3.14, g++-15.
  • lang= picks another one: ```python runnable lang=python-3.14-pandas2 runs Python with pandas 2.
  • Other options still work: ```python title="hello.py" runnable.
  • Code blocks without runnable stay as they are. A runnable block in a language OnlineCompiler.io doesn't run stays plain code, and the build prints a warning naming the language.

Readers can change the code, type the program's input and run it again. Runners work inside Tabs, admonitions and any MDX component, and follow readers as they move between pages.

Options

Option Default What it does
key none Your widget key. Without one, runnable blocks show as plain code and the build warns.
theme "site" "site" follows your site's light or dark mode. "light", "dark" or "auto" (the reader's system setting) fix it.
languages {} Your own code block languages, mapped to compiler ids: { py3: "python-3.14" }.

To keep the key out of the config file, read it from the environment: { key: process.env.ONLINECOMPILER_WIDGET_KEY }.

Supported languages

Python, C, C++ (and C++20), Java, JavaScript (Node.js), TypeScript (Deno), C#, F#, Visual Basic, Go, Rust, PHP, Ruby, Kotlin, Swift, Dart, Scala, Groovy, Clojure, Haskell, OCaml, Elixir, Erlang, Racket, Common Lisp, Prolog, R, Julia, Octave, Fortran, COBOL, Pascal, Ada, D, Nim, Zig, Crystal, Perl, Lua, Bash, PowerShell, Objective-C, FreeBASIC, Assembly (NASM), SQL (SQLite) and DuckDB. Python includes NumPy, pandas, SciPy, scikit-learn and Matplotlib.

The full list of compiler ids is at onlinecompiler.io/docs/compilers.

FAQ

How do I run Python code in Docusaurus docs?

Install this plugin, add your widget key, and write runnable after the language of a Python code block (```python runnable). Readers can then edit the code and run it in the page. The same works for C++, Java, Go, Rust, SQL and every other language in the list above.

How is this different from other ways to run code in Docusaurus?

  • @docusaurus/theme-live-codeblock renders React components (JSX) in the reader's browser. It doesn't run Python, C++ or other languages. It also replaces Docusaurus's code block component, as this plugin does, so a site can use only one of the two: if you list both, the one listed last in themes wins.
  • Python in the browser (themes based on Brython, Pyodide or PyScript) runs Python only, inside the reader's browser. Brython translates Python to JavaScript; Pyodide and PyScript first download a Python runtime into the browser.
  • This plugin sends the code to a server sandbox, so it runs the real compilers and interpreters of 46 languages, including Python with NumPy and pandas. The reader's browser only shows the editor and the output.

Does it slow down my docs?

No. The widget script loads only on pages that have a runnable block, after the page, and each runner connects only when it scrolls into view. Pages without runnable blocks load nothing extra.

Can search engines read the code?

Yes. The code is in the page's HTML as text, so search engines index it and readers see it before the editor loads.

What do readers' runs cost?

Widget keys are free. Runs count towards your OnlineCompiler.io account's allowance; the Free plan includes 100,000 runs a month. Give the key its own monthly limit to cap what a public page can use.

Is the widget key secret?

No. Widget keys are made to sit in public pages: they can run code in the widget but can't call the REST API. Restrict the key to your domain.

What if OnlineCompiler.io can't be reached?

The code is in the page as text, so readers still see it. Only the Run button needs the service.

Does it work with MkDocs or WordPress too?

Yes: mkdocs-onlinecompiler for MkDocs and Material for MkDocs, and the OnlineCompiler.io Code Runner plugin for WordPress. Any other site can use the embeddable code editor directly.

What it sends, and where

On pages with a runnable block, the reader's browser loads the widget script from https://onlinecompiler.io/widget.js. When a runner scrolls into view, the widget connects to api.onlinecompiler.io with your widget key; the browser adds your site's address, which is how the key's allowed domain is checked. When a reader presses Run, the widget sends the code, its input and the language, and shows the result. Readers don't sign in and the plugin sends nothing else about them. See the OnlineCompiler.io privacy policy and terms.

The widget itself, its options and its JavaScript API are documented at onlinecompiler.io/docs/widget.

Requirements

Docusaurus 3 with the classic theme (@docusaurus/preset-classic). Tested with Docusaurus 3.10, with both the Rspack and webpack bundlers.

License

MIT

About

Docusaurus plugin for runnable, interactive code examples: write ```python runnable and readers edit and run code in 46 languages (Python, JavaScript, C++, Java, Go, SQL) in your docs.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages