How to show Folium map inside a PyQt5 GUI?

本秂侑毒 提交于 2020-08-19 08:42:13

问题


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?


回答1:


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_())



来源:https://stackoverflow.com/questions/58590199/how-to-show-folium-map-inside-a-pyqt5-gui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!