Communicate CEF Browser to js and viceversa

575 views
Skip to first unread message

Francisco José Raga López

unread,
Jan 22, 2017, 7:17:01 AM1/22/17
to CEF Python
Hi.

Is possible return value to cefpython frim js.

The methid is so simple,my js function is:

<script>
var act_yaw;
function GetActualYaw(){
//alert("Alert!!");
return act_yaw;
}

</script>

And in my python code:

self.view.browser.GetMainFrame().ExecuteFunction("GetActualYaw")


How  is possible get the "act_yaw" value in my python code??Or maybe call python function from browser?

regards

Czarek Tomczak

unread,
Feb 12, 2017, 1:25:26 PM2/12/17
to CEF Python
The new examples in the examples/ directory don't have this functionality yet. You can take a look at the unit tests however:
1. Set bindings: https://github.com/cztomczak/cefpython/blob/81a85587af589d591983cfc29f580c8094fd6b6e/unittests/main_test.py#L128
2. JS code: https://github.com/cztomczak/cefpython/blob/81a85587af589d591983cfc29f580c8094fd6b6e/unittests/main_test.py#L25
3. Python code: https://github.com/cztomczak/cefpython/blob/81a85587af589d591983cfc29f580c8094fd6b6e/unittests/main_test.py#L285

There is also the old wxpython.py example with the most feaures tested: https://github.com/cztomczak/cefpython/blob/cefpython55/src/linux/binaries_64bit/deprecated/wxpython.py

I have recently created docs/Tutorial.md document (still under works) and javascript integration topic will be explained in that tutorial in the near future.

Czarek Tomczak

unread,
Feb 12, 2017, 1:27:38 PM2/12/17
to CEF Python

Francisco José Raga López

unread,
Feb 14, 2017, 12:08:54 PM2/14/17
to CEF Python
Thanks for your help,but finally use a hilarious trick,but work fine.
Is so simple,in my js code have this:

var viewChangeHandler = function() {
   
var yaw = view.yaw();
 
var act_yaw=yaw;
 
var d_yaw=yaw/(Math.PI/180)
 console
.log(d_yaw);
 
};


And in my python code:

#CefPython Event      
class ClientHandler():
   
#hilarious method but work
   
def OnConsoleMessage(self, browser, message, source, line):
        source
= config.DEFAULT_URL
       
Geo360Dialog.ActualOrientation(yaw=message)
       
return

Then I obtain the return value.

Francisco José Raga López

unread,
Feb 26, 2017, 6:21:05 AM2/26/17
to CEF Python
Message has been deleted

Felipe Torresini

unread,
Apr 19, 2017, 1:18:23 PM4/19/17
to CEF Python
I wonder if that's equivalent, but in a generic way?

python

isLocal = False

def doStuff():
pass

def connects():
pass

class MyClass():
def __init__(self):
pass
def myMethodGetsClientFn(clientSideMethodName = None):
if clientSideMethodName is not None:
connects()
else:
doStuff()
#interface
class ClientHandler():
# generic method: gets messages from console.log in javascript client side
# browser: is the object wich we must to return from create CreateBrowserSync method
# message: the message sent from javascript
# source: the source from the message (if it comes from a file or from url- the url passed to CreateBrowserSync(settings, url="http:/bla/index.html") )
# line : no idea


def OnConsoleMessage(self, browser, message, source, line):

#source receives from config the url
source = config.DEFAULT_URL
MyClass.myMethodGetsClientFn(fnToSendAsStr = messageFromFunction)

return
js
fnToSendAsStr = function(){

return "fnInPython"+ ":" + "param1" + "param2" + "paramList" ;
};

my_js_method = """
var my_js_callback = function() {

console.log(fnToSendAsStr);
};

Reply all
Reply to author
Forward
0 new messages