Skip to content

Runnable code

Let readers run JavaScript and Python in the page, and fill in their own values once.

2 min read

Updated

Add run to a code block, and readers get a Run button that runs it in the page and shows the output.

JavaScript
const total = [3, 4, 5].reduce((sum, n) => sum + n, 0);
console.log(`Total: ${total}`);
total * 2;

Run shows what the code logs, then the value of its last line. The code runs in a sandbox that can’t reach your page or your reader’s data, stops after 10 seconds, and has a Stop button.

Put run right after the language, or after a file name:

MDX
```js run
console.log('Hello');
```
```js client.js run
console.log('Hello from a file');
```
runflagpath

Makes the block runnable. In JavaScript and Python, readers get Run. In JavaScript and TypeScript, they also get Open in StackBlitz and Open in CodeSandbox, which open the code as a small project. Packages the code imports are added to it.

norunflagpath

Keeps one block from running, when its language runs everywhere.

JavaScript can use top-level await. Python loads the first time a reader runs it. TypeScript isn’t run in the page, so a TypeScript block only gets the two Open buttons.

Run can’t load packages. If a JavaScript block imports one, Run says “Imports need packages: open the example in StackBlitz or CodeSandbox.”

To make every block in a language runnable, list it in docs.json:

docs.json
{
"specly": { "run": ["python"] }
}

JavaScript and Python are the languages that run in the page.

An example with a placeholder, like an API key, is easier to use with a real value. List the placeholders in docs.json:

docs.json
{
"specly": { "placeholders": ["YOUR_API_KEY"] }
}

Any block that holds one of those names gets a Your values button:

Terminal
curl https://<slug>.specly.xyz/api/example \
-H "Authorization: Bearer YOUR_API_KEY"

A reader chooses Your values and types a value once. Copy and Run then use it in every example on the site. It’s kept in that reader’s tab, and the page doesn’t change for anyone else. Open in StackBlitz and Open in CodeSandbox never include it.

specly.placeholdersstring[]path

The names to replace: 1 to 64 characters each, and up to 30 names.

Filling in values from your own sign-in (specly.personalization.endpoint) needs your docs on your own domain, which is coming.

Was this page helpful?

Report a problem

What’s the problem?

The docs team sees this. Other readers don’t.