File: webview.py

package info (click to toggle)
orange-widget-base 4.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: python: 16,556; javascript: 58; makefile: 6
file content (606 lines) | stat: -rw-r--r-- 22,835 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
"""
This module holds our customized WebView that integrates HTML, CSS & JS
into Qt. WebviewWidget provides a somewhat uniform interface (_WebViewBase)
around either WebEngineView (extends QWebEngineView) or WebKitView
(extends QWebView), as available.
"""
import os
from os.path import abspath, dirname, join
import time
import threading
import warnings
import inspect

from collections.abc import Iterable, Mapping, Set, Sequence
from itertools import count
from numbers import Integral, Real
from random import random
from urllib.parse import urljoin
from urllib.request import pathname2url

import numpy as np

from AnyQt.QtCore import Qt, QObject, QFile, QTimer, QUrl, QSize, QEventLoop, \
    pyqtProperty, pyqtSlot, pyqtSignal
from AnyQt.QtGui import QColor
from AnyQt.QtWidgets import QSizePolicy, QWidget, QApplication
from AnyQt import sip

try:
    from AnyQt.QtWebKitWidgets import QWebView
    HAVE_WEBKIT = True
except ImportError:
    HAVE_WEBKIT = False

try:
    from AnyQt.QtWebEngineWidgets import QWebEngineView
    from AnyQt.QtWebEngineCore import QWebEngineScript
    from AnyQt.QtWebChannel import QWebChannel
    HAVE_WEBENGINE = True
except ImportError:
    HAVE_WEBENGINE = False


_WEBVIEW_HELPERS = join(dirname(__file__), '_webview', 'helpers.js')
_WEBENGINE_INIT_WEBCHANNEL = join(dirname(__file__), '_webview', 'init-webengine-webchannel.js')

_ORANGE_DEBUG = os.environ.get('ORANGE_DEBUG')


def _inherit_docstrings(cls):
    """Inherit methods' docstrings from first superclass that defines them"""
    for method in cls.__dict__.values():
        if inspect.isfunction(method) and method.__doc__ is None:
            for parent in cls.__mro__[1:]:
                __doc__ = getattr(parent, method.__name__, None).__doc__
                if __doc__:
                    method.__doc__ = __doc__
                    break
    return cls


class _QWidgetJavaScriptWrapper(QObject):
    def __init__(self, parent):
        super().__init__(parent)
        self.__parent = parent

    @pyqtSlot()
    def load_really_finished(self):
        self.__parent._load_really_finished()

    @pyqtSlot()
    def hideWindow(self):
        w = self.__parent
        while isinstance(w, QWidget):
            if w.windowFlags() & (Qt.Window | Qt.Dialog):
                return w.hide()
            w = w.parent() if callable(w.parent) else w.parent


if HAVE_WEBENGINE:
    class WebEngineView(QWebEngineView):
        """
        A QWebEngineView initialized to support communication with JS code.

        Parameters
        ----------
        parent : QWidget
            Parent widget object.
        bridge : QObject
            The "bridge" object exposed as a global object ``pybridge`` in
            JavaScript. Any methods desired to be accessible from JS need
            to be decorated with ``@QtCore.pyqtSlot(<*args>, result=<type>)``
            decorator.
            Note: do not use QWidget instances as a bridge, use a minimal
            QObject subclass implementing the required interface.
        """

        # Prefix added to objects exposed via WebviewWidget.exposeObject()
        # This caters to this class' subclass
        _EXPOSED_OBJ_PREFIX = '__ORANGE_'

        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            debug = debug or _ORANGE_DEBUG
            if debug:
                port = os.environ.setdefault('QTWEBENGINE_REMOTE_DEBUGGING', '12088')
                warnings.warn(
                    'To debug QWebEngineView, set environment variable '
                    'QTWEBENGINE_REMOTE_DEBUGGING={port} and then visit '
                    'http://127.0.0.1:{port}/ in a Chromium-based browser. '
                    'See https://doc.qt.io/qt-5/qtwebengine-debugging.html '
                    'This has also been done for you.'.format(port=port))
            super().__init__(parent,
                             sizeHint=QSize(500, 400),
                             sizePolicy=QSizePolicy(QSizePolicy.Expanding,
                                                    QSizePolicy.Expanding),
                             **kwargs)
            self.bridge = bridge
            self.debug = debug
            with open(_WEBVIEW_HELPERS, encoding="utf-8") as f:
                self._onloadJS(f.read(),
                               name='webview_helpers',
                               injection_point=QWebEngineScript.DocumentCreation)

            qtwebchannel_js = QFile("://qtwebchannel/qwebchannel.js")
            if qtwebchannel_js.open(QFile.ReadOnly):
                source = bytes(qtwebchannel_js.readAll()).decode("utf-8")
                with open(_WEBENGINE_INIT_WEBCHANNEL, encoding="utf-8") as f:
                    init_webchannel_src = f.read()
                self._onloadJS(source + init_webchannel_src %
                               dict(exposeObject_prefix=self._EXPOSED_OBJ_PREFIX),
                               name='webchannel_init',
                               injection_point=QWebEngineScript.DocumentCreation)
            else:
                warnings.warn(
                    "://qtwebchannel/qwebchannel.js is not readable.",
                    RuntimeWarning)

            self._onloadJS(';window.__load_finished = true;',
                           name='load_finished',
                           injection_point=QWebEngineScript.DocumentReady)

            channel = QWebChannel(self)
            if bridge is not None:
                if isinstance(bridge, QWidget):
                    warnings.warn(
                        "Don't expose QWidgets in WebView. Construct minimal "
                        "QObjects instead.", DeprecationWarning,
                        stacklevel=2)
                channel.registerObject("pybridge", bridge)

            channel.registerObject('__bridge', _QWidgetJavaScriptWrapper(self))

            self.page().setWebChannel(channel)

        def _onloadJS(self, code, name='', injection_point=QWebEngineScript.DocumentReady):
            script = QWebEngineScript()
            script.setName(name or ('script_' + str(random())[2:]))
            script.setSourceCode(code)
            script.setInjectionPoint(injection_point)
            script.setWorldId(script.MainWorld)
            script.setRunsOnSubFrames(False)
            self.page().scripts().insert(script)
            self.loadStarted.connect(
                lambda: self.page().scripts().insert(script))

        def runJavaScript(self, javascript, resultCallback=None):
            """
            Parameters
            ----------
            javascript : str
                javascript code.
            resultCallback : Optional[(object) -> None]
                When the script has been executed the `resultCallback` will
                be called with the result of the last executed statement.
            """
            if resultCallback is not None:
                self.page().runJavaScript(javascript, resultCallback)
            else:
                self.page().runJavaScript(javascript)


if HAVE_WEBKIT:
    class WebKitView(QWebView):
        """
        Construct a new QWebView widget that has no history and
        supports loading from local URLs.

        Parameters
        ----------
        parent: QWidget
            The parent widget.
        bridge: QObject
            The QObject to use as a parent. This object is also exposed
            as ``window.pybridge`` in JavaScript.
        html: str
            The HTML to load the view with.
        debug: bool
            Whether to enable inspector tools on right click.
        **kwargs:
            Passed to QWebView.
        """
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            super().__init__(parent,
                             sizeHint=QSize(500, 400),
                             sizePolicy=QSizePolicy(QSizePolicy.Expanding,
                                                    QSizePolicy.Expanding),
                             **kwargs)

            if isinstance(parent, QWidget) and parent.layout() is not None:
                parent.layout().addWidget(self)  # TODO REMOVE

            self.bridge = bridge
            self.frame = None
            debug = debug or _ORANGE_DEBUG
            self.debug = debug

            if isinstance(bridge, QWidget):
                warnings.warn(
                    "Don't expose QWidgets in WebView. Construct minimal "
                    "QObjects instead.", DeprecationWarning,
                    stacklevel=2)

            def _onload(_ok):
                if _ok:
                    self.frame = self.page().mainFrame()
                    self.frame.javaScriptWindowObjectCleared.connect(
                        lambda: self.frame.addToJavaScriptWindowObject('pybridge', bridge))
                    with open(_WEBVIEW_HELPERS, encoding="utf-8") as f:
                        self.frame.evaluateJavaScript(f.read())

            self.loadFinished.connect(_onload)
            _onload(True)

            history = self.history()
            history.setMaximumItemCount(0)
            settings = self.settings()
            settings.setMaximumPagesInCache(0)
            settings.setAttribute(settings.LocalContentCanAccessFileUrls, True)
            settings.setAttribute(settings.LocalContentCanAccessRemoteUrls, False)

            if debug:
                settings.setAttribute(settings.LocalStorageEnabled, True)
                settings.setAttribute(settings.DeveloperExtrasEnabled, True)
                settings.setObjectCacheCapacities(int(4e6), int(4e6), int(4e6))
                settings.enablePersistentStorage()

        def runJavaScript(self, javascript, resultCallback=None):
            result = self.page().mainFrame().evaluateJavaScript(javascript)
            if resultCallback is not None:
                # Emulate the QtWebEngine's interface and return the result
                # in a event queue invoked callback
                QTimer.singleShot(0, lambda: resultCallback(result))


def _to_primitive_types(d):
    # pylint: disable=too-many-return-statements
    if isinstance(d, QWidget):
        raise ValueError("Don't expose QWidgets in WebView. Construct minimal "
                         "QObjects instead.")
    if isinstance(d, Integral):
        return int(d)
    if isinstance(d, Real):
        return float(d)
    if isinstance(d, (bool, np.bool_)):
        return bool(d)
    if isinstance(d, (str, QObject)):
        return d
    if isinstance(d, np.ndarray):
        return d.tolist()
    if isinstance(d, Mapping):
        return {k: _to_primitive_types(d[k]) for k in d}
    if isinstance(d, Set):
        return {k: 1 for k in d}
    if isinstance(d, (Sequence, Iterable)):
        return [_to_primitive_types(i) for i in d]
    if d is None:
        return None
    if isinstance(d, QColor):
        return d.name()
    raise TypeError(
        'object must consist of primitive types '
        '(allowed: int, float, str, bool, list, '
        'dict, set, numpy.ndarray, ...). Type is: ' + d.__class__)


class _WebViewBase:
    def _evalJS(self, code):
        """Evaluate JavaScript code and return the result of the last statement."""
        raise NotImplementedError

    def onloadJS(self, code):
        """Run JS on document load."""
        raise NotImplementedError

    def html(self):
        """Return HTML contents of the top frame.

        Warnings
        --------
        In the case of Qt WebEngine implementation, this function calls:

            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

        until the page's HTML contents is made available (through IPC).
        """
        raise NotImplementedError

    def exposeObject(self, name, obj):
        """Expose the object `obj` as ``window.<name>`` in JavaScript.

        If the object contains any string values that start and end with
        literal ``/**/``, those are evaluated as JS expressions the result
        value replaces the string in the object.

        The exposure, as defined here, represents a snapshot of object at
        the time of execution. Any future changes on the original Python
        object are not visible in its JavaScript counterpart.

        Parameters
        ----------
        name: str
            The global name the object is exposed as.
        obj: object
            The object to expose. Must contain only primitive types, such as:
            int, float, str, bool, list, dict, set, numpy.ndarray ...
        """
        raise NotImplementedError

    def __init__(self):
        self.__is_init = False
        self.__js_queue = []

    @pyqtSlot()
    def _load_really_finished(self):
        """Call this from JS when the document is ready."""
        self.__is_init = True

    def dropEvent(self, event):
        """Prevent loading of drag-and-drop dropped file"""
        pass

    def evalJS(self, code):
        """
        Evaluate JavaScript code synchronously (or sequentially, at least).

        Parameters
        ----------
        code : str
            The JavaScript code to evaluate in main page frame. The scope is
            not assured. Assign properties to window if you want to make them
            available elsewhere.

        Warnings
        --------
        In the case of Qt WebEngine implementation, this function calls:

            QCoreApplication.processEvents(QEventLoop.ExcludeUserInputEvents)

        until the page is fully loaded, all the objects exposed via
        exposeObject() method are indeed exposed in JS, and the code `code`
        has finished evaluating.
        """
        def _later():
            if not self.__is_init and self.__js_queue:
                return QTimer.singleShot(1, _later)
            if self.__js_queue:
                # '/n' is required when the last line is a comment
                code = '\n;'.join(self.__js_queue)
                self.__js_queue.clear()
                self._evalJS(code)

        # WebView returns the result of the last evaluated expression.
        # This result may be too complex an object to safely receive on this
        # end, so instead, just make it return 0.
        code += ';0;'
        self.__js_queue.append(code)
        QTimer.singleShot(1, _later)

    def svg(self):
        """ Return SVG string of the first SVG element on the page, or
        raise ValueError if not any. """
        html = self.html()
        return html[html.index('<svg '):html.index('</svg>') + 6]

    def setHtml(self, html, base_url=''):
        """Set the HTML content of the current webframe to `html`
        (an UTF-8 string)."""
        super().setHtml(html, QUrl(base_url))

    @staticmethod
    def toFileURL(local_path):
        """Return local_path as file:// URL"""
        return urljoin('file:', pathname2url(abspath(local_path)))

    def setUrl(self, url):
        """Point the current frame to URL url."""
        super().setUrl(QUrl(url))

    def contextMenuEvent(self, event):
        """ Also disable context menu unless debug."""
        if self.debug:
            super().contextMenuEvent(event)


def wait(until: callable, timeout=5000):
    """Process events until condition is satisfied

    Parameters
    ----------
    until: callable
        Returns True when condition is satisfied.
    timeout: int
        Milliseconds to wait until TimeoutError is raised.
    """
    started = time.perf_counter()
    while not until():
        QApplication.instance().processEvents(QEventLoop.ExcludeUserInputEvents)
        if (time.perf_counter() - started) * 1000 > timeout:
            raise TimeoutError()


if HAVE_WEBKIT:

    class _JSObject(QObject):
        """ This class hopefully prevent options data from being
        marshalled into a string-like dumb (JSON) object when
        passed into JavaScript. Or at least relies on Qt to do it as
        optimally as it knows to."""

        def __init__(self, parent, name, obj):
            super().__init__(parent)
            self._obj = dict(obj=obj)

        @pyqtProperty('QVariantMap')
        def pop_object(self):
            return self._obj

    class WebviewWidget(_WebViewBase, WebKitView):
        def __init__(self, parent=None, bridge=None, *, debug=False, **kwargs):
            # WebEngine base WebviewWidget has js_timeout parameter, since
            # user do not know which one will get and passing js_timeout to
            # WebKitView causes error we should remove
            kwargs.pop("js_timeout", None)
            WebKitView.__init__(self, parent, bridge, debug=debug, **kwargs)
            _WebViewBase.__init__(self)

            def load_finished():
                if not sip.isdeleted(self):
                    self.frame.addToJavaScriptWindowObject(
                        '__bridge', _QWidgetJavaScriptWrapper(self))
                    self._evalJS('setTimeout(function(){'
                                 '__bridge.load_really_finished(); }, 100);')

            self.loadFinished.connect(load_finished)

        @pyqtSlot()
        def _load_really_finished(self):
            # _WebViewBase's (super) method not visible in JS for some reason
            super()._load_really_finished()

        def _evalJS(self, code):
            return self.frame.evaluateJavaScript(code)

        def onloadJS(self, code):
            self.frame.loadFinished.connect(
                lambda: WebviewWidget.evalJS(self, code))

        def html(self):
            return self.frame.toHtml()

        def exposeObject(self, name, obj):
            obj = _to_primitive_types(obj)
            jsobj = _JSObject(self, name, obj)
            self.frame.addToJavaScriptWindowObject('__js_object_' + name, jsobj)
            WebviewWidget.evalJS(self, '''
                window.{0} = window.__js_object_{0}.pop_object.obj;
                fixupPythonObject({0}); 0;
            '''.format(name))


elif HAVE_WEBENGINE:
    class IdStore:
        """Generates and stores unique ids.

        Used in WebviewWidget._evalJS below to match scheduled js executions
        and returned results. WebEngine operations are async, so locking is
        used to guard against problems that could occur if multiple executions
        ended at exactly the same time.
        """

        def __init__(self):
            self.id = 0
            self.lock = threading.Lock()
            self.ids = dict()

        def create(self):
            with self.lock:
                self.id += 1
                return self.id

        def store(self, id, value):
            with self.lock:
                self.ids[id] = value

        def __contains__(self, id):
            return id in self.ids

        def pop(self, id):
            with self.lock:
                return self.ids.pop(id, None)


    class _JSObjectChannel(QObject):
        """ This class hopefully prevent options data from being
        marshalled into a string-like dumb (JSON) object when
        passed into JavaScript. Or at least relies on Qt to do it as
        optimally as it knows to."""

        # JS webchannel listens to this signal
        objectChanged = pyqtSignal('QVariantMap')

        def __init__(self, parent):
            super().__init__(parent)
            self._obj = None
            self._id_gen = count()
            self._objects = {}

        def send_object(self, name, obj):
            if isinstance(obj, QObject):
                raise ValueError(
                    "QWebChannel doesn't transmit QObject instances. If you "
                    "need a QObject available in JavaScript, pass it as a "
                    "bridge in WebviewWidget constructor.")
            id = next(self._id_gen)
            value = self._objects[id] = dict(id=id, name=name, obj=obj)
            # Wait till JS is connected to receive objects
            wait(until=lambda: self.receivers(self.objectChanged), timeout=30000)
            self.objectChanged.emit(value)

        @pyqtSlot(int)
        def mark_exposed(self, id):
            del self._objects[id]

        def is_all_exposed(self):
            return len(self._objects) == 0


    _NOTSET = object()


    class WebviewWidget(_WebViewBase, WebEngineView):
        _html = _NOTSET

        def __init__(self, parent=None, bridge=None, *, js_timeout=5000, debug=False, **kwargs):
            WebEngineView.__init__(self, parent, bridge, debug=debug, **kwargs)
            _WebViewBase.__init__(self)

            # Tracks objects exposed in JS via exposeObject(). JS notifies once
            # the object has indeed been exposed (i.e. the new object is available
            # is JS) because the whole thing is async.
            # This is needed to stall any evalJS() calls which may expect
            # the objects being available (but could otherwise be executed before
            # the objects are exposed in JS).
            self._jsobject_channel = jsobj = _JSObjectChannel(self)
            self.page().webChannel().registerObject(
                '__js_object_channel', jsobj)
            self._results = IdStore()
            self.js_timeout = js_timeout

        def _evalJS(self, code):
            wait(until=self._jsobject_channel.is_all_exposed)
            if sip.isdeleted(self):
                return None
            result = self._results.create()
            self.runJavaScript(code, lambda x: self._results.store(result, x))
            wait(until=lambda: result in self._results,
                 timeout=self.js_timeout)
            return self._results.pop(result)

        def onloadJS(self, code):
            self._onloadJS(code, injection_point=QWebEngineScript.Deferred)

        def html(self):
            self.page().toHtml(lambda html: setattr(self, '_html', html))
            wait(until=lambda: self._html is not _NOTSET or sip.isdeleted(self))
            html, self._html = self._html, _NOTSET
            return html

        def exposeObject(self, name, obj):
            obj = _to_primitive_types(obj)
            self._jsobject_channel.send_object(name, obj)

        def setHtml(self, html, base_url=''):
            # TODO: remove once anaconda will provide PyQt without this bug.
            #
            # At least on some installations of PyQt 5.6.0 with anaconda
            # WebViewWidget grabs focus on setHTML which can be quite annoying.
            # For example, if you have a line edit as filter and show results
            # in WebWiew, then WebView grabs focus after every typed character.
            #
            # http://stackoverflow.com/questions/36609489
            # https://bugreports.qt.io/browse/QTBUG-52999
            initial_state = self.isEnabled()
            self.setEnabled(False)
            super().setHtml(html, base_url)
            self.setEnabled(initial_state)