Example #1
0
class QtWebPageRender:
    url: str
    html_str: str
    html_parser: requests_html.HTML

    def __init__(self, qt_app_singleton=None):
        self._qt_app = qt_app_singleton or get_qt_application_singleton()
        self._qt_webpage = QWebEnginePage()
        self._qt_webpage.loadFinished.connect(self._on_load_finished)

    def _on_load_finished(self):
        self._qt_webpage.toHtml(self._update_html)

    def _update_html(self, qt_webpage_to_html: str):
        self.html_str = qt_webpage_to_html
        self.html_parser = requests_html.HTML(url=self.url, html=self.html_str)
        self.find = self.html_parser.find
        self.xpath = self.html_parser.xpath
        self._qt_app.quit()

    def set_url(self, url: str):
        self.url = url
        self._qt_webpage.load(QUrl(url))
        self._qt_app.exec_()

    def set_html(self, html: str, base_url: str = ''):
        self.url = base_url
        self._qt_webpage.setHtml(html, QUrl(base_url))
        self._qt_app.exec_()

    @property
    def text(self):
        return self.html_parser.text
Example #2
0
class MapWidget(QWebEngineView):
    """
    The MapWidget class is a QWebEngineView that houses the leaflet map.
    Since it is a QWidget, it can be added to any QLayout.
    """
    @property
    def page(self):
        return self._page

    @property
    def channel(self):
        return self._channel

    def __init__(self,
                 use_file_absolute_path: bool = True,
                 alternative_base_path: str = ""):
        super().__init__()
        if use_file_absolute_path or len(alternative_base_path) == 0:
            self.base_path = os.path.dirname(os.path.abspath(__file__))
        else:
            self.base_path = alternative_base_path
        self._page = QWebEnginePage()
        self.setPage(self._page)
        self._channel = QWebChannel()
        self._page.setWebChannel(self._channel)
        self._loadPage()
        self.setContextMenuPolicy(Qt.NoContextMenu)

    def _get_page_path(self):
        return os.path.join(self.base_path, 'web', 'map.html')

    def _loadPage(self):
        html_path = self._get_page_path()
        # QEventLoop is used to make the page loading behave syncronously
        init_loop = QEventLoop()
        self._page.loadFinished.connect(init_loop.quit)
        self._page.load(QUrl().fromLocalFile(html_path))
        init_loop.exec_()