将Python函数返回到Chrome cons

2024-04-26 17:47:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python中的eel将一个函数从Python返回到googlechrome控制台。你知道吗

这是我的当前代码:

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\函数(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>

这给了我谷歌chrome的输出:

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

这是我的第二次尝试

<script type="text/javascript" src="/eel.js"></script>
<script>
  console.log('Calling Python...');
  a = eel.my_python_function(1, 2);
  console.log(a());

</script>

已经调整了控制台.log(a) ;至控制台.log(a())

输出是promise

实际输出应该是1 2


Tags: 函数textsrclogmyhtmltypecallback
1条回答
网友
1楼 · 发布于 2024-04-26 17:47:07

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. So the equivalent code from the Javascript side would be:

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

相关问题 更多 >