7

I'm trying to show a very simple Folium map inside a Qt GUI. The main code is:

import folium

m = folium.Map(location=[45.5236, -122.6750])
m
m.save('index.html')
folium.Map(
    location=[45.5236, -122.6750],
    tiles='Stamen Toner',
    zoom_start=13
)

When I use the code with Jupyter it's fine but shows anything with Spyder. What I want is to show the map in a QGraphicsView or any other QClass on a simple Qt GUI?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ash
  • 263
  • 3
  • 14

2 Answers2

11

You can save the html in a io.BytesIO() using the save method and then set it to a QWebEngineView using the setHtml() method:

import io
import sys

import folium
from PyQt5 import QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    m = folium.Map(
        location=[45.5236, -122.6750], tiles="Stamen Toner", zoom_start=13
    )

    data = io.BytesIO()
    m.save(data, close_file=False)

    w = QtWebEngineWidgets.QWebEngineView()
    w.setHtml(data.getvalue().decode())
    w.resize(640, 480)
    w.show()

    sys.exit(app.exec_())

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • do you know if adding layer is supported with your method ? I'm currently facing some troubles here : https://stackoverflow.com/questions/71204476/folium-map-embedded-within-pyqt-displays-blank-when-adding-geojson-to-layer – Foussy Feb 21 '22 at 15:31
3

HTML string of the map can be passed to the setHtml:

import sys
import folium
from PyQt5 import QtWidgets, QtWebEngineWidgets

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    m = folium.Map(location=[45.5236, -122.6750], zoom_start=13)
    w = QtWebEngineWidgets.QWebEngineView()
    w.setHtml(m.get_root().render())
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())
nanda
  • 71
  • 6