Example #1
0
    def expose_python_namespace(self, python_namespace, callbacks):
        """ Exposes the given python namespace to Javascript.

        Javascript can access the given list of callbacks as if they were
        methods on the object described by the python namespace.

        python_namespace: str:
            Namespace to expose to the JS world. This creates an object of the
            same name and attaches it to window frame.

        callbacks: [method_name: callable]:
            This list of callbacks is what is exposed to the JS world via the
            given python namespace.

        Usage:
        ------

        For example, doing this::

            expose_python_namespace('python', ['say_hello', say_hello])

        will create a window level object on the JS side which looks like
        this::

            window.python.say_hello == <a function which calls Python land>

        """
        frame = self._page.mainFrame()
        js_wrapper = create_js_object_wrapper(callbacks=callbacks,parent=frame)
        frame.javaScriptWindowObjectCleared.connect(
            lambda: frame.addToJavaScriptWindowObject(
                python_namespace, js_wrapper
            )
        )
Example #2
0
    def expose_python_namespace(self, python_namespace, callbacks):
        """
        Exposes the given python namespace to Javascript, using which Javascript
        can access the given list of callbacks as if they were methods on the
        object described by the python namespace.

        python_namespace: str:
            Namespace to expose to the JS world. This creates an object of the
            same name and attaches it to window frame.

        callbacks: [method_name: callable]:
            This list of callbacks is what is exposed to the JS world via the
            given python namespace.

        Usage:
        ------

        For example, doing this::

            expose_python_namespace('python', ['say_hello', say_hello])

        will create a window level object on the JS side which looks like this::

            window.python.say_hello == <a function which calls Python land>

        """
        frame = self.page().mainFrame()
        js_wrapper = create_js_object_wrapper(callbacks=callbacks, parent=frame)
        frame.javaScriptWindowObjectCleared.connect(
            lambda: frame.addToJavaScriptWindowObject(
                python_namespace, js_wrapper
            )
        )
Example #3
0
    def _js_cleared_signal(self):
        if self.control is not None:
            frame = self.control.page().mainFrame()

            # Since the js `window` object is cleared by the frame which still
            # exists, we need to explicitly delete the exposed objects.
            for exposed_obj in self._exposed_containers:
                exposed_obj.deleteLater()

            self._exposed_containers = exposed_containers = []

            if self.callbacks or self.properties:
                exposed_containers.append(create_js_object_wrapper(
                            self.js_object, self.callbacks, self.properties,
                            parent=frame,
                        ))
                frame.addToJavaScriptWindowObject(
                    self.python_namespace, exposed_containers[-1],
                    )