Pass a python dictionary as an object to Javascript

666 views
Skip to first unread message

pedro...@gmail.com

unread,
Jul 8, 2015, 7:26:11 AM7/8/15
to cefp...@googlegroups.com
I'm developing an application with pyqt.
So far, the UI is a web application inside a QwebView object.
In order to exchange information between front-end and back-end, I used to push a Python object in the DOM with:

self.web.page().mainFrame().addToJavaScriptWindowObject("obj",self.obj)

Given the generic object defined as follow:

class Obj(QtCore.QObject):
def __init__(self):
super(Obj,self).__init__()
self.data = {'foo' : 123, 'bar' : [1,2,3]}

@QtCore.pyqtSlot(result=QtCore.QVariant)
def GetData(self):
return QtCore.QVariant(self.data)

Then, when the page has been loaded, I can get the data of interest just calling

obj.GetData()

And what I get is a pure js object (It does not work if the dictionary contains a tuple..).

This approach worked perfectly, but since I need the well known performance of V8 plus some css specs not implemented yet in QWebView, I'm trying to migrate to CEF.

I changed the Object exposition as follow:

self.jsBindings.SetObject("obj", self.obj)

But when the GetData method is called by javascript, I got a warning stating that returning values is not supported.

The question is the following: Should I definitely change my approach, (in this case suggestion will be appreciated) or there is other method to return a dictionary from python to Js?

Thanks in advance.

Czarek Tomczak

unread,
Jul 8, 2015, 8:25:16 AM7/8/15
to cefp...@googlegroups.com
All communication between js <> python is asynchronous. You can pass data to js/python by using callbacks mechanism. Examples are in the wxpython.py script.




--
You received this message because you are subscribed to the Google Groups "CEF Python" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefpython+...@googlegroups.com.
Visit this group at http://groups.google.com/group/cefpython.
To view this discussion on the web visit https://groups.google.com/d/msgid/cefpython/9e8991f1-f2a0-4e2d-847a-64ddb3a7227c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Czarek Tomczak

unread,
Jul 8, 2015, 8:26:10 AM7/8/15
to cefp...@googlegroups.com
You can *return data to js/python by using callbacks mechanism.

pedro...@gmail.com

unread,
Jul 9, 2015, 6:24:15 AM7/9/15
to cefp...@googlegroups.com
Ok Thanks, I finally succeeded looking at the example you mentioned.
Reply all
Reply to author
Forward
0 new messages