docs(public): showcase foreign v2, OKF and EKC
Rewrite the public foreign reference, add bilingual complex Python notebook and OKF/EKC pages, and expand both homepages with executable foreign, notebook, knowledge and preflight examples.
This commit is contained in:
1 parent
81f9ea0072
commit
e6f05c7748
12 files changed
+1015
-20
No files matched your search
+134
-9
@@ -1,20 +1,145 @@
|
||||
---
|
||||
title: lib/foreign — external languages
|
||||
description: Run local interpreters from Marqdo (English)
|
||||
title: lib/foreign — foreign kernels v2
|
||||
description: Structured execution, JSON calls, parallel fan-out, persistent kernels and an explicit state bridge
|
||||
import foreign:lib/foreign.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
Import lib/foreign.mq.md. Bind a code fence like a formula: empty-RHS assign then a ```lang block. The value type is code. Call run code=`name` to execute via your local interpreter. run_lang runs an inline source string.
|
||||
`lib/foreign` runs code fences and inline sources with local interpreters. v2 keeps the Markdown surface unchanged while adding structured records, JSON in/out, parallel fan-out, persistent Python/Node kernels and an explicit state bridge.
|
||||
|
||||
`hi` =
|
||||
```python
|
||||
print("hello-from-python")
|
||||
The same file is still a document: a bound fence is readable Markdown, and `foreign.*` turns it into an executable unit.
|
||||
|
||||
---
|
||||
|
||||
Probe before you run.
|
||||
|
||||
**node = > foreign.probe lang="node"**
|
||||
|
||||
> print text=[ok](`node`)
|
||||
> print text=[version](`node`)
|
||||
|
||||
`probe` returns `{lang, cmd, path, ok, version, error}`. Omit `lang` to inspect every known language.
|
||||
|
||||
---
|
||||
|
||||
A structured record, not just stdout.
|
||||
|
||||
`marker` =
|
||||
```node
|
||||
console.log("hello from a foreign kernel");
|
||||
```
|
||||
|
||||
Default command is python (Windows) or python3 (Unix). Override with set_cmd, env MARQDO_FOREIGN_PYTHON, or the command box in live view. Failures ask you to check that configuration.
|
||||
**record = > foreign.invoke code=`marker`**
|
||||
|
||||
**`out` = [foreign.run] code=`hi`**
|
||||
> print text=[ok](`record`)
|
||||
> print text=[stdout](`record`)
|
||||
> print text=[code](`record`)
|
||||
|
||||
> print text=`out`
|
||||
`invoke` never throws on a process failure. Read `ok`, `code`, `stdout`, `stderr`, `ms`, `timed_out`, `truncated`, `result` and `error` from one record.
|
||||
|
||||
---
|
||||
|
||||
JSON in, JSON out.
|
||||
|
||||
`double` =
|
||||
```node
|
||||
let input = "";
|
||||
process.stdin.on("data", chunk => { input += chunk; });
|
||||
process.stdin.on("end", () => {
|
||||
console.log(JSON.stringify(JSON.parse(input) * 2));
|
||||
});
|
||||
```
|
||||
|
||||
**single = > foreign.call code=`double` input=21**
|
||||
|
||||
> print text=`single`
|
||||
|
||||
The input is encoded as JSON on stdin; the last stdout value is parsed as JSON. Use `output="json"` with `invoke` when you also want the raw record.
|
||||
|
||||
---
|
||||
|
||||
Parallel fan-out.
|
||||
|
||||
`numbers` =
|
||||
|
||||
| value |
|
||||
|-------|
|
||||
| 1 |
|
||||
| 2 |
|
||||
| 3 |
|
||||
| 4 |
|
||||
|
||||
**doubled = > foreign.call_many code=`double` inputs=`numbers` concurrency=4**
|
||||
|
||||
> print text=`doubled`
|
||||
|
||||
`call_many` applies one program to every input, runs independently, preserves input order, and returns parsed JSON results. `run_many` does the same for full invoke-option maps.
|
||||
|
||||
---
|
||||
|
||||
A persistent kernel.
|
||||
|
||||
**session = > foreign.session_open lang="node"**
|
||||
|
||||
**first = > foreign.session_eval id=`session` source="let total = 0; for (let i = 1; i <= 5; i += 1) total += i;"**
|
||||
|
||||
**second = > foreign.session_eval id=`session` source="total * 2"**
|
||||
|
||||
> print text=[result](`second`)
|
||||
|
||||
Variables created in the first evaluation survive into the second. The kernel is not a shell: use `session_eval`, `session_get`, `session_names`, then `session_close`.
|
||||
|
||||
---
|
||||
|
||||
The explicit state bridge.
|
||||
|
||||
**> foreign.session_set id=`session` name="rate" value=0.08**
|
||||
|
||||
**rate = > foreign.session_get id=`session` name="rate"**
|
||||
|
||||
> print text=`rate`
|
||||
|
||||
**> foreign.session_close id=`session`**
|
||||
|
||||
This is a JSON copy, not shared memory. The kernel receives a value; Marqdo receives a value. There is no implicit mirroring and no hidden global namespace.
|
||||
|
||||
---
|
||||
|
||||
Notebook sessions.
|
||||
|
||||
A document can declare named kernels and a cell order without new Markdown markers:
|
||||
|
||||
```text
|
||||
`内核表` =
|
||||
|
||||
| 名称 | 语言 | 命令 | 说明 |
|
||||
|------|------|------|------|
|
||||
| 分析 | python | | shared analysis |
|
||||
| 复核 | python | | isolated review |
|
||||
|
||||
`会话表` =
|
||||
|
||||
| 单元 | 内核 | 说明 |
|
||||
|------|------|------|
|
||||
| 准备数据 | 分析 | write state |
|
||||
| 计算指标 | 分析 | read state |
|
||||
| 独立复核 | 复核 | isolated kernel |
|
||||
```
|
||||
|
||||
Live `view` then adds Run All, Step, kernel switching, a journal and per-cell state. See the complete Python walkthrough in [`features/19-foreign-notebook.mq.md`](../features/19-foreign-notebook.mq.md).
|
||||
|
||||
---
|
||||
|
||||
Safety defaults.
|
||||
|
||||
| Boundary | Default |
|
||||
|---|---|
|
||||
| Execution | requires the host execution capability |
|
||||
| Shell | no shell; arguments are split explicitly |
|
||||
| Output | bounded, with truncated |
|
||||
| Timeout | process group killed on Unix / taskkill /T on Windows |
|
||||
| CWD | file sandbox root; no implicit escape |
|
||||
| Parallelism | capped fan-out; each item isolated |
|
||||
|
||||
Read the full API in [../../lib/foreign.mq.md](../../lib/foreign.mq.md) and the design in [../../doc/design/foreign-kernel.md](../../doc/design/foreign-kernel.md).
|
||||
Reference in new issue
Block a user