`python` is a built-in global object for running Python code from Expr.
It is used directly by name. Do not call `python()`.
Evaluates a Python expression string and returns the converted result.
value = python.eval('2 + 3 * 5'); print(value); // 17
Runs Python code synchronously and returns the Python script result.
result = python.run_code('result = 40 + 2'); print(result); // 42
Runs Python code synchronously with parameters from a `map`.
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.
result = python.run_file('scripts/example.py');
Runs a Python file synchronously with parameters from a `map`.
result = python.run_file('scripts/example.py', map('a', 10, 'b', 20));
Creates a `python_task` object for asynchronous execution of Python code.
task = python.code('result = 7'); task.run();
Creates a `python_task` object for asynchronous Python code execution with parameters.
Creates a `python_task` object for asynchronous execution of a Python file.
Creates a `python_task` object for asynchronous Python file execution with parameters.
`python_task` is returned by `python.code(…)` and `python.file(…)`.
Properties:
| 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. |
| `event_count` | number | Number of queued events from Python. |
Methods:
| Method | Returns | Description |
|---|---|---|
| `on_complete(callback)` | boolean | Sets a callback called with `ok` and `result`. |
| `on_event(callback)` | boolean | Sets a callback called with event name and value. |
| `use_subinterpreter(enabled)` | boolean | Sets sub-interpreter use before `run()`. |
| `run()` | boolean | Starts the task. A task can be run only once. |
| `read_event()` | map or none | Reads the next queued event. |
| `send_command(name, value)` | boolean | Sends a command to the running Python script. |
| `terminate()` | boolean | Requests termination of the running Python script. |
Example:
task = python.code('result = 123'); task.run(); while (!task.completed) {} print(task.result);
Previous: session
Next: Collections