[Python] How to update Folium map without re rendering entire map?

Discussão em 'Python' iniciado por Stack, Fevereiro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have a Folium map placed in PySide6 QWebEngineView. Map coordinates are updated each second and the map is recentered to the new position. However, this is re-rendering entire map with each update and it causes "flashing", which is not user friendly.

    I need to make the map to reposition by smooth dragging/sliding, or even jump will be fine if flashing does not happen. I could not find any real solution for it anywhere.

    This is the code I have in map.py:

    import io
    import sys
    import folium
    from PySide6.QtCore import QTimer
    from PySide6.QtWebEngineWidgets import QWebEngineView
    from PySide6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget


    class MapWebView(QWebEngineView):
    def __init__(self, initial_coordinates: tuple[float, float]):
    super().__init__()
    self.folium_map = folium.Map(
    location=initial_coordinates,
    zoom_start=13,
    zoom_control=False,
    attribution_control=False
    )
    self.data = io.BytesIO()
    self.folium_map.save(self.data, close_file=False)
    self.setHtml(self.data.getvalue().decode())

    def update_map(self, new_coords: tuple[float, float]):
    self.folium_map = folium.Map(
    location=new_coords,
    zoom_start=13,
    zoom_control=False,
    attribution_control=False
    )
    self.data = io.BytesIO()
    self.folium_map.save(self.data, close_file=False)
    self.setHtml(self.data.getvalue().decode())


    class MainWindow(QMainWindow):
    def __init__(self):
    super().__init__()
    self.resize(600, 600)
    self.setCentralWidget(QWidget())
    self.centralWidget().setLayout(QVBoxLayout())

    self.map_webview = MapWebView((47.030780, 8.656176))
    self.centralWidget().layout().addWidget(self.map_webview)

    timer = QTimer(self)
    timer.timeout.connect(self.update_map)
    timer.start(1000)

    def update_map(self):
    current_coords = self.map_webview.folium_map.location
    new_coords = (current_coords[0] + 0.002, current_coords[1] + 0.002)
    self.map_webview.update_map(new_coords)


    if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec())

    Continue reading...

Compartilhe esta Página