[英]Return Python function onto Chrome console
I am trying to return a function from Python onto google chrome console using eel in Python.我正在尝试使用 Python 中的 eel 将 Python 中的函数返回到 google chrome 控制台。
This is the current code I have:这是我目前的代码:
Python Python
import eel
eel.init('web')
print("Start")
@eel.expose
def my_python_function(a, b):
print(a, b)
eel.start('index.html')
HTML HTML
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Tutorial</title>
<script type="text/javascript" src="/eel.js"></script>
<script>
console.log('Calling Python...');
eel.my_python_function(1, 2);
</script>
</head>
<body>
<p>test</p>
</body>
</html>
In the HTML js script, eel.my_python_function(1,2) will print out onto the cmd.在 HTML js 脚本中, eel.my_python_function(1,2) 将打印到 cmd 上。
I have tried the following to try get the Python function to be outputted onto the Chrome console.我尝试了以下尝试将 Python 函数输出到 Chrome 控制台。
Take 1取 1
<script type="text/javascript" src="/eel.js"></script>
<script>
console.log('Calling Python...');
a = eel.my_python_function(1, 2);
console.log(a);
</script>
this gave me this output on google chrome:这在谷歌浏览器上给了我这个输出:
ƒ (callback = null) {
if(callback != null) {
eel._call_return_callbacks[call.call] = callback;
} else {
return new Promise(function(resolve) {
and this is my Try 2这是我的尝试 2
<script type="text/javascript" src="/eel.js"></script>
<script>
console.log('Calling Python...');
a = eel.my_python_function(1, 2);
console.log(a());
</script>
Have adjusted the console.log(a);已经调整了console.log(a); to console.log(a());到 console.log(a());
The output was promise
输出是promise
The real output should be 1 2
实际输出应该是1 2
https://github.com/ChrisKnott/Eel#callbacks https://github.com/ChrisKnott/Eel#callbacks
In Javascript, the language doesn't allow us to block while we wait for a callback, except by using await from inside an async function.在 Javascript 中,该语言不允许我们在等待回调时阻塞,除非在异步函数中使用 await。 So the equivalent code from the Javascript side would be:所以来自 Javascript 端的等效代码将是:
// Inside a function marked 'async' we can use the 'await' keyword. async function run() { // The first call returns the function and the second actually execute it let a = await eel.my_python_function()(1,2); // Must prefix call with 'await', otherwise it's the same syntax console.log("Got this from Python: " + a); } run();
Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await阅读更多: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
Your python function does not return anything try to use你的 python 函数不返回任何尝试使用的东西
Python: Python:
@eel.expose
def my_python_function(a, b):
return a+ b
Js: JS:
console.log('Calling Python...');
eel.my_python_function(1, 2)().then((r) => {
console.log(r);
});
I have encountered the same issue and is the only solution I find我遇到了同样的问题,这是我找到的唯一解决方案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.