繁体   English   中英

将 Python 函数返回到 Chrome 控制台

[英]Return Python function onto Chrome console

我正在尝试使用 Python 中的 eel 将 Python 中的函数返回到 google chrome 控制台。

这是我目前的代码:

Python

import eel

eel.init('web')


print("Start")



@eel.expose
def my_python_function(a, b):
    print(a, b)


eel.start('index.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>

在 HTML js 脚本中, eel.my_python_function(1,2) 将打印到 cmd 上。

我尝试了以下尝试将 Python 函数输出到 Chrome 控制台。

取 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>

这在谷歌浏览器上给了我这个输出:

ƒ (callback = null) {
            if(callback != null) {
                eel._call_return_callbacks[call.call] = callback;
            } else {
                return new Promise(function(resolve) {

这是我的尝试 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>

已经调整了console.log(a); 到 console.log(a());

输出是promise

实际输出应该是1 2

https://github.com/ChrisKnott/Eel#callbacks

在 Javascript 中,该语言不允许我们在等待回调时阻塞,除非在异步函数中使用 await。 所以来自 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();

阅读更多: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

你的 python 函数不返回任何尝试使用的东西

Python:

@eel.expose
def my_python_function(a, b):
    return a+ b

JS:

console.log('Calling Python...');
eel.my_python_function(1, 2)().then((r) => {
    console.log(r);
});

我遇到了同样的问题,这是我找到的唯一解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM