Skip to content

Calling JS from Python ​

PY → JS

The other direction is just as simple: register a plain object in the page, then call it from Python with Axon("window name").objectName.method(...).

JS side: register an object ​

docs-js.js
js
// docs-js.js
import { const AxonController: _AxonControllerAxonController } from "axon-gui";

const AxonController: _AxonControllerAxonController.function register(id: string, controller: any): voidregister("DocsJS", {
  value: numbervalue: 7,

  
function mirror(payload: any): {
    mirrored: any;
    at: number;
}
mirror
(payload: anypayload) { // a plain method
return { mirrored: anymirrored: payload: anypayload, at: numberat: var Date: DateConstructor

Enables basic storage and retrieval of dates and times.

Date
.DateConstructor.now(): number

Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

now
() };
}, async function sleepEcho(payload: any, ms?: number): Promise<any>sleepEcho(payload: anypayload, ms: numberms = 300) { // async methods work too await new
var Promise: PromiseConstructor
new <unknown>(executor: (resolve: (value: unknown) => void, reject: (reason?: any) => void) => void) => Promise<unknown>

Creates a new Promise.

@paramexecutor A callback used to initialize the promise. This callback is passed two arguments: a resolve callback used to resolve the promise with a value or the result of another promise, and a reject callback used to reject the promise with a provided reason or error.
Promise
((r: (value: unknown) => voidr) => function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): numbersetTimeout(r: (value: unknown) => voidr, ms: numberms));
return payload: anypayload; }, });

DocsJS is the name Python will use.

Python side: collect the result ​

controller.py
python
from axon import class Axon(web_name: str)Axon

# MainView in Axon("MainView") is the window name; it must match AxonGUI("MainView", ...)
js = class Axon(web_name: str)Axon("MainView").DocsJS

# call and wait for the result (seconds); on timeout it raises instead of hanging forever
result = js.mirror({"hello": "from python"}).result(5)
print(result)          # {'mirrored': {'hello': 'from python'}, 'at': 1732...}

# async methods are called the same way
js.sleepEcho("hi", 200).result(5)

Inside async def you can simply await:

controller.py
result = await js.mirror({"hello": "from python"})

Reading and writing JS attributes ​

controller.py
value = Axon("MainView").DocsJS.value.get(5)     # read
Axon("MainView").DocsJS.value = 99               # write

Hover to see the types ​

Whatever object you pass to AxonController.register() becomes Python's API — method signatures included.

ts
import { const AxonController: _AxonControllerAxonController } from "axon-gui";

const 
const DocsJS: {
    value: number;
    mirror(payload: {
        hello: string;
    }, label?: string): {
        mirrored: boolean;
        payload: {
            hello: string;
        };
        label: string;
        at: number;
    };
}
DocsJS
= {
value: numbervalue: 7,
function mirror(payload: {
    hello: string;
}, label?: string): {
    mirrored: boolean;
    payload: {
        hello: string;
    };
    label: string;
    at: number;
}
mirror
(
payload: {
    hello: string;
}
payload
: { hello: stringhello: string }, label: stringlabel = "docs") {
return { mirrored: booleanmirrored: true,
payload: {
    hello: string;
}
payload
, label: stringlabel, at: numberat: var Date: DateConstructor

Enables basic storage and retrieval of dates and times.

Date
.DateConstructor.now(): number

Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

now
() };
}, }; const AxonController: _AxonControllerAxonController.function register(id: string, controller: any): voidregister("DocsJS",
const DocsJS: {
    value: number;
    mirror(payload: {
        hello: string;
    }, label?: string): {
        mirrored: boolean;
        payload: {
            hello: string;
        };
        label: string;
        at: number;
    };
}
DocsJS
);

Key points ​

  • .result(timeout) or await: without collecting the result the JS return value is dropped and you never see the error either.
  • The window name must match: Axon("MainView") must equal AxonGUI("MainView", ...), otherwise you get web 'MainView' not found.
  • Errors thrown in JS arrive as JSRuntimeError in Python, so try/except works.
  • Python → JS only works while the window is alive; calling before the page finishes loading fails. If you need to wait, use the loadFinished event — see Events.