Skip to content

从 Python 调 JS ​

PY → JS

反方向也一样简单:网页里注册一个普通对象,Python 里用 Axon("窗口名").对象名.方法名(...) 调它。

JS 侧:注册一个对象 ​

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) { // 普通方法
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 方法也可以 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 就是 Python 侧用的名字。

Python 侧:取结果 ​

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

# Axon("MainView") 里的 MainView 是窗口名,要和 AxonGUI("MainView", ...) 一致
js = class Axon(web_name: str)Axon("MainView").DocsJS

# 调用并等结果(单位秒);超时会抛异常,不会永远挂着
result = js.mirror({"hello": "from python"}).result(5)   # [!code highlight]
print(result)          # {'mirrored': {'hello': 'from python'}, 'at': 1732...}

# async 方法一样调
js.sleepEcho("hi", 200).result(5)

在 async def 里也可以直接 await:

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

读写 JS 的属性 ​

controller.py
value = Axon("MainView").DocsJS.value.get(5)     # 读
Axon("MainView").DocsJS.value = 99               # 写

悬停看类型 ​

AxonController.register() 传进去的对象就是你暴露给 Python 的 API,方法签名会被保留。

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
);

要点 ​

  • .result(timeout) 或 await:不取结果的话,JS 侧的返回会被丢掉,你还看不到错误。
  • 窗口名必须对:Axon("MainView") 里的名字要和 AxonGUI("MainView", ...) 一样,否则报 web 'MainView' not found。
  • JS 侧抛的错在 Python 是 JSRuntimeError,可以被 try/except 接住。
  • Python → JS 只能在窗口活着的时候调;页面还没加载完时调用会失败,需要的话用 loadFinished 事件等一等(见事件)。