简体   繁体   中英

Return Python function onto Chrome console

I am trying to return a function from Python onto google chrome console using eel in Python.

This is the current code I have:

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>

In the HTML js script, eel.my_python_function(1,2) will print out onto the cmd.

I have tried the following to try get the Python function to be outputted onto the Chrome console.

Take 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

<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); to console.log(a());

The output was promise

The real output should be 1 2

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();

Read more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

Your python function does not return anything try to use

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);
});

I have encountered the same issue and is the only solution I find

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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