Anki offers a JS → Python bridge via pycmd
(internal name bridgeCommand
) that allows sending string messages to the Python side.
See the add-on hook webview_did_receive_js_message
for instructions.
You can directly use the return value of JS code with AnkiWebView.evalWithCallback
. For example, the DeckBrowser gets the page y offset and uses the return value in its callback: