JaWeilBaum / pyqtlet2

pyqtlet2 extends pyqtlet which initially brought Leaflet maps to PyQt5 and PySide6.
Other
37 stars 19 forks source link

i can not change HttpHeader in mapwidget #66

Open khosrooo opened 3 months ago

khosrooo commented 3 months ago

i want to change cache-control :max_age :10000000 but setHttpHeader not work and max-age is always 3600(1hour) do u know another way for changing setHttpHeader???

import os
from PySide2.QtCore import QEventLoop, Qt, QUrl,QByteArray
from PySide2.QtWebChannel import QWebChannel
from PySide2.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineProfile
from PySide2.QtWebEngineCore import QWebEngineUrlRequestInterceptor, QWebEngineUrlRequestInfo

class CacheControlInterceptor(QWebEngineUrlRequestInterceptor):
    def interceptRequest(self, info: QWebEngineUrlRequestInfo):
        request_url = info.requestUrl().toString()
        print(f"Request URL: {request_url}")

        if info.resourceType() == QWebEngineUrlRequestInfo.ResourceTypeImage:
            # Set custom cache control
            info.setHttpHeader(QByteArray(b'Cache-Control'), QByteArray(b'public,max-age=10000000'))

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 = "", cache_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

        # Create a QWebEngineProfile and set the cache path
        self._profile = QWebEngineProfile.defaultProfile()
        if cache_path:
            self._profile.setCachePath(cache_path)

        self._page = QWebEnginePage(self._profile, self)
        self.setPage(self._page)
        self._channel = QWebChannel()
        self._page.setWebChannel(self._channel)

        # Set the interceptor
        self._interceptor = CacheControlInterceptor()
        self._profile.setRequestInterceptor(self._interceptor)

        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 synchronously
        init_loop = QEventLoop()
        self._page.loadFinished.connect(init_loop.quit)
        self._page.load(QUrl().fromLocalFile(html_path))
        init_loop.exec_()