Your browser does not support JavaScript!
Upvotes Anonymous 2 User 0

CEFPython: How to Call into Python from JavaScript (wxPython)

It is easy to pass data from your JavaScript code (running in the Chromium embedded browser) to your Python code. The below code is adapted from the wxPython example provided with CEFPython.

First we create a Python class and a method to be called from the JavaScript:
class JavascriptExternal:

    def MyPythonMethod(self, json_obj):
        # Handle the JSON data, which is the Python dict: {foo: 'bar'}
We register this class (after the browser initialization code) to expose it to the JavaScript:
self.javascriptExternal = JavascriptExternal(self.browser)
jsBindings.SetObject("external", self.javascriptExternal)
A global object named external will now exist on the window object in the browser. To call our Python method from JavaScript, we do the following:
external.MyPythonMethod({foo: 'bar'})
It's that simple.
Was this helpful?



Leave a Comment

I agree to the Terms of Service
Design © 2015, Downranked, LLC.,
Original user code contributions under MIT License