English
Cross-window interaction
An Axon app can open several windows (one AxonGUI each). Windows can talk to each other: JS in window A can call JS in window B, or hit every window at once.
In one sentence
Windows have no direct channel to each other — every cross-window call is routed through Python, because Python is the only side that knows which windows are alive.
Two names, easily confused
The most common mistake in cross-window code is mixing up the window name and the controller name:
| Name | Where it appears | Example |
|---|---|---|
| Window name | first argument of AxonGUI("charts", url) | "charts" |
| Controller name | id in AxonController.register("Series", {...}) | "Series" |
// window name ↓ controller name ↓
await AxonCross.web("charts").Series.reload("600s");In this page the windows are main and charts, and the controllers inside them are Shell (Python) and Series (JS) — window names are lowercase and point at a page, controller names are capitalised and point at an object inside that page. They do not have to match; keeping them different makes the code easier to read.
Three things you can do
| Goal | JS side | Python side |
|---|---|---|
| List open windows | AxonCross.windows() | AxonCross.windows() |
| Call a JS method in one window | AxonCross.web("charts").Series.reload() | AxonCross.call("charts", "Series", "reload") |
| Hit every window | AxonCross.broadcast({...}) | AxonCross.broadcast("Toasts", "show", "Saved") |
| Emit an event to all windows | AxonCross.emit("theme:changed", "dark") | AxonCross.emit("theme:changed", "dark") |
| Emit an event to one window | AxonCross.emitTo("charts", "theme:changed", "dark") | AxonCross.emit_to("charts", "theme:changed", "dark") |
Calling one specific window
Say the main window wants the charts window to reload its data.
main.py
python
from axon import class AxonGUI(web_name: WebName, url: str, options: Optional[AxonGUIOptions] = None)AxonGUI, class AxonMagicGUIAxonMagicGUI
app = QApplication([])
class AxonMagicGUIAxonMagicGUI()
main = class AxonGUI(web_name: WebName, url: str, options: Optional[AxonGUIOptions] = None)AxonGUI("main", "http://127.0.0.1:5173/")
charts = class AxonGUI(web_name: WebName, url: str, options: Optional[AxonGUIOptions] = None)AxonGUI("charts", "http://127.0.0.1:5173/charts.html") # [!code highlight]charts.html
// inside the charts window: register a JS controller others can call
import { AxonController } from "axon-gui";
AxonController.register("Series", {
reload(range = "1d") {
return loadSeries(range); // returning a Promise is fine
},
});main.html
// inside the main window: call Series.reload() in the charts window
import { AxonCross } from "axon-gui";
await AxonCross.web("charts").Series.reload("600s");Positional arguments are spread just like Axon.xxx; use the packed form Series.reload([args], { kwargs }, timeoutMs) when you need kwargs or a timeout.
The explicit form:
main.html
const value = await AxonCross.call({
web: "charts", // window name
controller: "Series", // controller name
func: "reload",
args: ["600s"],
timeout: 5000,
});Hitting every window
For "everyone refresh" use a broadcast. Each window returns its own result and one failing window does not affect the others.
main.html
import { AxonCross, AxonChannels } from "axon-gui";
const results = await AxonCross.broadcast({
controller: "Toasts",
func: "show",
args: ["Settings saved"],
exclude: [AxonChannels.AxonEnv.web_name], // skip yourself
});
for (const item of results) {
if (!item.ok) console.warn(item.web, "did not get it:", item.error);
}From Python:
main.py
python
from axon import module axon.AxonCrossAxonCross
futures = module axon.AxonCrossAxonCross.broadcast(controller_id: str, func: str, args: Any, kwargs: dict[str, Any] | None = None, timeout: float | None = None, exclude: Iterable[str] = ()) -> dict[str, axon.AxonPYCall.AxonFuture[Any]]broadcast("Toasts", "show", "Settings saved")
# wait for all of them; a broken window only shows up in its own entry
for item in module axon.AxonCrossAxonCross.results(futures: dict[str, axon.AxonPYCall.AxonFuture[Any]], timeout: float | None = None) -> list[dict[str, Any]]results(futures, timeout=5):
if not item["ok"]:
print(f"{item['web']} failed: {item['error']}")Events
An event is "notify only, no return value" — lighter than a call. Write the two directions separately, never mixed:
Direction 1: JS fires → Python listens
Typical case: the user clicks "export" in the charts window and Python in the main window should record it.
charts.html
import { AxonEvent } from "axon-gui";
// the JS side only sends
AxonEvent.dispatchPYEvent({ type: "series:exported", args: ["600s", 1280] }); controller.py
python
from axon import AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。AxonController, AxonEvent · add_listener / dispatch_event / dispatch_js_event / dispatch_js_event_batch / off / on …AxonEvent
@AxonController(web_name: str | list[str] = '*', id: Optional[str] = None, shared: bool = False)
``AxonController`` 的命名空间对象:既能当装饰器用,也挂注册表操作。AxonController("main")
class class ShellShell:
def __init__(self) -> None:
# the Python side only receives
AxonEvent · add_listener / dispatch_event / dispatch_js_event / dispatch_js_event_batch / off / on …AxonEvent.add_listener("series:exported", self._on_exported) # [!code highlight]
def _on_exported(self, range: str, points: int) -> None:
print(f"exported {range} with {points} points")Direction 2: Python fires → JS listens
Typical case: the backend finished loading and tells every window about a theme change, or tells just the charts window to refresh.
controller.py
python
from axon import module axon.AxonCrossAxonCross
module axon.AxonCrossAxonCross.emit(type: str, args: Any, exclude: Iterable[str] = ()) -> list[axon.AxonPYCall.AxonFuture[Any]]emit("theme:changed", "dark") # every window's JS gets this
module axon.AxonCrossAxonCross.emit_to(web_name: str, type: str, args: Any) -> axon.AxonPYCall.AxonFuture[Any]emit_to("charts", "series:reloaded", "600s") # only the charts windowcharts.html
import { AxonEvent } from "axon-gui";
// the JS side only receives
AxonEvent.addListener("theme:changed", (theme) => applyTheme(theme));
AxonEvent.addListener("series:reloaded", (range) => loadSeries(range));How to remember
AxonCross.emit / emitTo are Python → JS only. The other way (JS → Python) uses AxonEvent.dispatchPYEvent({ type, args }). Do not mix them in one block.
Hover to see the types
AxonCross.call() targets one window, windows() lists the open ones, broadcast() returns one result per window.
ts
// 1. open a second window named charts
const const windows: string[]windows = await const Axon: {
DemoController: {
add(a: number, b: number): Promise<number>;
asset_count(): Promise<number>;
blob(size?: number): Promise<string>;
broadcast(text: string): Promise<Array<any>>;
call_js_mirror(payload: any): Promise<any>;
cross_broadcast(text?: string): Promise<Record<string, any>>;
cross_call(text?: string): Promise<any>;
cross_close(): Promise<Array<string>>;
cross_emit(text?: string): Promise<Array<string>>;
cross_open(): Promise<Array<string>>;
delayed(msg: string, ms?: number): Promise<string>;
... 13 more ...;
counter: number;
};
}
Axon 的 JS 侧语法糖。
tsawait Axon.Users.add(1, 2) // 调 Python 方法, 位置参数直接摊开
await Axon.Users.slow([1000], {}, 300) // 需要 kwargs / 超时时用打包写法
await Axon.Users.count // 读 Python 属性 (getVar)
Axon.Users.count = 3 // 写 Python 属性 (setVar, fire-and-forget)
属性读是惰性的: 只有真正 await / .then / .catch / .finally 时才发请求。
取到的 dispatcher 本身是函数, 同时挂了 then/catch/finally,
让 Axon.Users.count 既能当 Promise 用, 又能当方法调用。
Axon.type DemoController: {
add(a: number, b: number): Promise<number>;
asset_count(): Promise<number>;
blob(size?: number): Promise<string>;
broadcast(text: string): Promise<Array<any>>;
call_js_mirror(payload: any): Promise<any>;
cross_broadcast(text?: string): Promise<Record<string, any>>;
cross_call(text?: string): Promise<any>;
cross_close(): Promise<Array<string>>;
cross_emit(text?: string): Promise<Array<string>>;
cross_open(): Promise<Array<string>>;
delayed(msg: string, ms?: number): Promise<string>;
fail(reason?: string): Promise<null>;
... 12 more ...;
counter: number;
}
DemoController.function cross_open(): Promise<Array<string>>cross_open();
// → ["AxonDocs", "charts"]
// 2. call a JS controller inside it (that page registers Peer and DocsJS)
const const reply: anyreply = await const AxonCross: {
windows(): Promise<string[]>;
has(web: string): Promise<boolean>;
web(web: string): any;
call({ web, controller, func, args, kwargs, timeout }: AxonCrossCallOptions): Promise<any>;
broadcast({ controller, func, args, kwargs, timeout, exclude, }: AxonCrossBroadcastOptions): Promise<AxonCrossResult[]>;
emit(type: string, ...args: any[]): Promise<{
count: number;
}>;
emitTo(web: string, type: string, ...args: any[]): Promise<any>;
}
AxonCross.function web(web: string): any直接调另一个窗口,写法跟 Axon.xxx 一样:
AxonCross.web("charts").Charts.reload("600s")
web("charts").Peer.ping("cross-window call from AxonDocs");
// → { web: "charts", echo: "cross-window call from AxonDocs", at: "…" }
// 3. broadcast to every window, collect results per window
await const AxonCross: {
windows(): Promise<string[]>;
has(web: string): Promise<boolean>;
web(web: string): any;
call({ web, controller, func, args, kwargs, timeout }: AxonCrossCallOptions): Promise<any>;
broadcast({ controller, func, args, kwargs, timeout, exclude, }: AxonCrossBroadcastOptions): Promise<AxonCrossResult[]>;
emit(type: string, ...args: any[]): Promise<{
count: number;
}>;
emitTo(web: string, type: string, ...args: any[]): Promise<any>;
}
AxonCross.function broadcast({ controller, func, args, kwargs, timeout, exclude, }: AxonCrossBroadcastOptions): Promise<AxonCrossResult[]>一次打到所有窗口,返回每个窗口自己的结果。
broadcast({ AxonCrossBroadcastOptions.controller: stringcontroller: "DocsJS", AxonCrossBroadcastOptions.func: stringfunc: "note", AxonCrossBroadcastOptions.args?: any[] | undefinedargs: ["broadcast"] });
// 4. emit an event (fire-and-forget)
await const AxonCross: {
windows(): Promise<string[]>;
has(web: string): Promise<boolean>;
web(web: string): any;
call({ web, controller, func, args, kwargs, timeout }: AxonCrossCallOptions): Promise<any>;
broadcast({ controller, func, args, kwargs, timeout, exclude, }: AxonCrossBroadcastOptions): Promise<AxonCrossResult[]>;
emit(type: string, ...args: any[]): Promise<{
count: number;
}>;
emitTo(web: string, type: string, ...args: any[]): Promise<any>;
}
AxonCross.function emit(type: string, ...args: any[]): Promise<{
count: number;
}>
发一个事件给所有窗口(对方用 AxonEvent.addListener(type, fn) 接)。
emit("demo:cross", "event from AxonDocs");