execute js function and get the return value of it to python and vice versa

371 views
Skip to first unread message

Mohamed Ashraf

unread,
Jul 30, 2021, 12:25:43 PM7/30/21
to CEF Python
methods like executeFunction  work asynchronously and so I can not get the return value. I have tried to use websocket but it also works async and I get "undefined". Is there any solution for this or something that can make that bridge possible?

Czarek Tomczak.

unread,
Jul 30, 2021, 2:08:55 PM7/30/21
to CEF Python
You can return value via javascript callback. See the Tutorial.

Mohamed Ashraf

unread,
Jul 30, 2021, 6:49:27 PM7/30/21
to CEF Python
I have already seen the tutorial specifically this example: "examples/snippets/javascript_bindings.py", but what I understand is that you pass some functions forth and back between js and python, and I don't know how can I use that to return the value of js function execution. 
say for example I have the python function like this,  I have bind it to js. when I run this function in js I pass the name of another python function as "function" parameter of exec_py.... then I run the python function inside "eval" and get the return of it as "function_return". the point here, how can I get "function_return"  .

def exec_py(function):
    function_return = eval(function)
    # whatever it will return "null" in js
    return function_return

even I make it like this:
def exec_py(function, js_callback):
    function_return = eval(function)
    js_callback.Call(function_return)

what is the exact js code to get the function_return in syn way.

sorry for the long question, but this part makes me so confused 

Czarek Tomczak.

unread,
Jul 30, 2021, 7:33:25 PM7/30/21
to CEF Python
You do it similarly how you do asynchronous javascript in js. You can pass js callbacks and python callbacks and the communication is bidirectional. Although you have to initiate it from JS to be able to pass callbacks. When you execute the callback you can pass a return value to that callback. However there is no chain of calls and there is no synchronization, so you can't write the code as you were used it, you have to redesign it, possibly write some wrapper code to make it easier. I don't understand your code. Try modifying javascript_bindings.py example.

Mohamed Ashraf

unread,
Jul 31, 2021, 7:01:03 AM7/31/21
to CEF Python
Could you please write js code that includes function that send order to execute python function and get the return back, the scheme of the js function like this:
 function execute_python(python_func_name){ 
              var the_return_from_python = python_func_name // any python function that has bind 
              return the_return_from_python 
 }
and the same for python

Czarek Tomczak.

unread,
Jul 31, 2021, 3:55:20 PM7/31/21
to CEF Python
Declare that function async and create a promise. When calling python binded function pass a js callback and in that callback resolve the promise. After that await for promise. I suggest you read some JS tutorials.

Mohamed Ashraf

unread,
Aug 1, 2021, 8:30:29 AM8/1/21
to CEF Python
I will do. Thanks for your help
Reply all
Reply to author
Forward
0 new messages