`python` is a built-in global object for running Python code from Expr.
It is used directly by name. Do not call `python()`.
Synchronous methods run Python and wait for completion. Asynchronous methods create a `python_task` object.
`python` is a predefined global object. It is not created with a constructor.
| Item | Description |
|---|---|
| Syntax | `python` |
| Arguments | not applicable |
| Returns | python object |
value = python.eval('2 + 3 * 5');
`python` currently exposes no public properties.
Returns a display string for the object.
| Item | Description |
|---|---|
| Syntax | `value.to_string()` |
| Arguments | none |
| Returns | string |
Example:
text = value.to_string();
`clone()` is not supported for `python`.
Evaluates a Python expression string and returns the converted result.
| Item | Description |
|---|---|
| Syntax | `python.eval(expression)` |
| Arguments | `expression`: Python expression text |
| Returns | converted Python result |
value = python.eval('2 + 3 * 5'); print(value); // 17
Runs Python code synchronously and returns the Python script result.
The Python code can assign `result` to return a value to Expr.
| Item | Description |
|---|---|
| Syntax | `python.run_code(code)` |
| Arguments | `code`: Python code text |
| Returns | converted Python result |
result = python.run_code('result = 40 + 2'); print(result); // 42
Runs Python code synchronously with parameters from a `map`.
Each map key becomes a Python variable with the converted Expr value.
| Item | Description |
|---|---|
| Syntax | `python.run_code(code, params)` |
| Arguments | `code`: Python code text; `params`: `map` with input values |
| Returns | converted Python result |
result = python.run_code('result = a + b', map('a', 10, 'b', 20)); print(result); // 30
Runs a Python file synchronously and returns the Python script result.
The file path is resolved by the same host path rules used by Expr file paths.
| Item | Description |
|---|---|
| Syntax | `python.run_file(filePath)` |
| Arguments | `filePath`: Python file path |
| Returns | converted Python result |
result = python.run_file('scripts/example.py');
Runs a Python file synchronously with parameters from a `map`.
Each map key becomes a Python variable with the converted Expr value.
| Item | Description |
|---|---|
| Syntax | `python.run_file(filePath, params)` |
| Arguments | `filePath`: Python file path; `params`: `map` with input values |
| Returns | converted Python result |
result = python.run_file('scripts/example.py', map('a', 10, 'b', 20));
Creates a `python_task` object for asynchronous execution of Python code.
The task is configured first. Call `run()` on the returned object to start it.
| Item | Description |
|---|---|
| Syntax | `python.code(code)` |
| Arguments | `code`: Python code text |
| Returns | `python_task` object |
task = python.code('result = 7'); task.run();
Creates a `python_task` object for asynchronous execution of Python code with parameters from a `map`.
| Item | Description |
|---|---|
| Syntax | `python.code(code, params)` |
| Arguments | `code`: Python code text; `params`: `map` with input values |
| Returns | `python_task` object |
task = python.code('result = a + b', map('a', 10, 'b', 20)); task.run();
Creates a `python_task` object for asynchronous execution of a Python file.
The task is configured first. Call `run()` on the returned object to start it.
| Item | Description |
|---|---|
| Syntax | `python.file(filePath)` |
| Arguments | `filePath`: Python file path |
| Returns | `python_task` object |
task = python.file('scripts/example.py'); task.run();
Creates a `python_task` object for asynchronous execution of a Python file with parameters from a `map`.
| Item | Description |
|---|---|
| Syntax | `python.file(filePath, params)` |
| Arguments | `filePath`: Python file path; `params`: `map` with input values |
| Returns | `python_task` object |
task = python.file('scripts/example.py', map('a', 10, 'b', 20)); task.run();
`python` is a protected built-in global name. The name itself cannot be overwritten.
python = 1; // error
`python_task` represents asynchronous Python execution.
It is returned by `python.code(…)` and `python.file(…)`. It is configured first, then started once with `run()`.
`python_task` objects are not created with a public constructor.
| Item | Description |
|---|---|
| Syntax | created by `python.code(…)` or `python.file(…)` |
| Arguments | not applicable |
| Returns | `python_task` object |
task = python.code('result = 123');
Properties are read-only. Use methods to configure or control the task.
| Property | Type | Description |
|---|---|---|
| `started` | boolean | `true` after `run()` has been called. |
| `running` | boolean | `true` while the Python task is running. |
| `completed` | boolean | `true` after the task finishes. |
| `ok` | boolean | `true` when the task completed successfully. |
| `use_subinterpreter` | boolean | Current sub-interpreter setting. |
| `result` | any | Result returned by the Python script. |
| `error` | string or none | Error text when the task failed, otherwise `none()`. |
| `event_count` | number | Number of queued events from Python. |
task = python.code('result = 123'); task.run(); while (!task.completed) {} print(task.ok, ' ', task.result);
Returns a display string for the object.
| Item | Description |
|---|---|
| Syntax | `value.to_string()` |
| Arguments | none |
| Returns | string |
Example:
text = value.to_string();
`clone()` is not supported for `python_task`.
Sets a callback called when the task completes.
The callback receives two arguments: `ok` and `result`.
| Item | Description |
|---|---|
| Syntax | `python_task.on_complete(callback)` |
| Arguments | `callback`: callable object |
| Returns | boolean |
function done(ok, result) { print('done: ', ok, ' ', result); } task = python.code('result = 123').on_complete(done); task.run();
Sets a callback called when Python sends an event.
The callback receives two arguments: event name and event value.
| Item | Description |
|---|---|
| Syntax | `python_task.on_event(callback)` |
| Arguments | `callback`: callable object |
| Returns | boolean |
function event(name, value) { print(name, ': ', value); } task = python.code('import planetcnc\nplanetcnc.send_event("status", 10)').on_event(event); task.run();
Sets whether the task should use a Python sub-interpreter.
This method must be called before `run()`. After the task has started, changing this setting raises an error.
| Item | Description |
|---|---|
| Syntax | `python_task.use_subinterpreter(enabled)` |
| Arguments | `enabled`: boolean value |
| Returns | current sub-interpreter setting |
task = python.code('result = 1'); task.use_subinterpreter(false); task.run();
Starts the task.
A task can be run only once. Calling `run()` again raises an error.
| Item | Description |
|---|---|
| Syntax | `python_task.run()` |
| Arguments | none |
| Returns | boolean, `true` if the task was started |
task = python.code('result = 7'); started = task.run();
Reads and removes the next queued event.
Returns a `map` with `name` and `value`, or `none()` if no event is queued. The task keeps a bounded event queue; high-rate events should be read regularly.
| Item | Description |
|---|---|
| Syntax | `python_task.read_event()` |
| Arguments | none |
| Returns | `map` or `none()` |
task = python.code('import planetcnc\nplanetcnc.send_event("status", 10)'); task.run(); while (!task.completed) {} event = task.read_event(); if (!event.is_none()) { print(event.name, ': ', event.value); }
Sends a command to the running Python script.
Returns `false` if the task has already completed.
| Item | Description |
|---|---|
| Syntax | `python_task.send_command(name, value)` |
| Arguments | `name`: command name; `value`: command value |
| Returns | boolean |
task = python.code('import planetcnc\ncmd = planetcnc.read_command()\nresult = cmd["value"] + 1'); task.run(); task.send_command('value', 41); while (!task.completed) {} print(task.result); // 42
Requests termination of the running Python script.
Returns `false` if the task was not started or has already completed.
| Item | Description |
|---|---|
| Syntax | `python_task.terminate()` |
| Arguments | none |
| Returns | boolean |
task = python.code('import planetcnc\nwhile not planetcnc.terminate():\n pass\nresult = "terminated"'); task.run(); task.terminate(); while (!task.completed) {} print(task.result);
task = python.code('result = 123'); task.run(); while (!task.completed) { } if (task.ok) { print(task.result); } else { print(task.error); }
Previous: session
Next: Collections