How to call Javascript function from python? [question]
PieceOfGood opened this issue · 8 comments
The only example I found here. True, it is about the opposite, but in the question discussed there, there is just an example that should work for me
sciter.Window().call_function('func_name')
but it does not work.
I tried to wrap it in a frame in the same way as you did in your example pysciter.py
and then call from the instance context
self.call_function('func_name')
but it does not work too.
Could you add to the minimal example a button that calls a function from a javascript to python, which calls a javascript function with an alert in window?
sciter.Window().call_function('func_name')
This creates a new empty window, calls a function, and destroys that window.
From Javascript to Python: use an event handler (frame is also an event handler) with @script
in Python to implement such function. In JS call it with Window.this.xcall
.
From Python: frame.call_function
if you are outside of a handler or self.call_function
in you're inside of it.
See pysciter.py and pysciter.htm in https://github.com/sciter-sdk/pysciter/tree/master/examples. It's for TIScript but the idea is the same. Also see minimal.htm, it contains both JS and TIS.
main.py
file:
import os
import sciter
ROOT_DIR = os.path.abspath(".")
HTML_PATH = os.path.join(ROOT_DIR, "main.html")
class Frame(sciter.Window):
def __init__(self):
super().__init__(ismain=True, uni_theme=True)
@sciter.script
def ScriptCallTest(self):
print("ScriptCallTest()")
self.call_function("test")
if __name__ == '__main__':
frame = Frame()
frame.load_file(HTML_PATH)
frame.expand()
frame.run_app()
main.html
file:
<html>
<head>
<title>Title</title>
</head>
<body>
<button id="test">Test</button>
</body>
<script type="module">
const view = Window.this;
document.$("#test").addEventListener("click", () => {
view.xcall("ScriptCallTest");
console.log("<--- call test --->");
});
function test() {
console.log("---> test called <---");
view.modal("Call Test!");
}
</script>
</html>
Please correct if something is wrong in the example, but I do not understand what else is required of me to work as I expect.
And when I start the pysciter.py
from example — nothing happens.
It works in <script>
but not in <script type="module">
. See https://github.com/c-smile/sciter-js-sdk/blob/main/demos/integration/res/default.htm#L21 and https://github.com/c-smile/sciter-js-sdk/blob/main/demos/integration/res/default.htm#L130
@c-smile Andrew, is this expected?
It works in
<script>
but not in<script type="module">
.
Yes, that's by JS [modules] design.
Each module has its own namespace. Functions defined there appear in module context only.
Use explicit globalThis.xxx
declaration like this:
globalThis.test = function test() {
...
}
when you need to place function in global context from modules.
Thank you very much!