简体   繁体   English

如何在 PyQt5 GUI 中显示 Folium map?

[英]How to show Folium map inside a PyQt5 GUI?

I'm trying to show a very simple Folium map inside a Qt GUI.我试图在 Qt GUI 中展示一个非常简单的 Folium map。 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.当我将代码与 Jupyter 一起使用时,它很好,但在 Spyder 中显示了任何内容。 What I want is to show the map in a QGraphicsView or any other QClass on a simple Qt GUI?我想要的是在一个简单的 Qt GUI 上的 QGraphicsView 或任何其他 QClass 中显示 map?

You can save the html in a io.BytesIO() using the save method and then set it to a QWebEngineView using the setHtml() method:您可以使用 save 方法将 html 保存在 io.BytesIO() 中,然后使用 setHtml() 方法将其设置为 QWebEngineView:

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

在此处输入图像描述

HTML string of the map can be passed to the setHtml:可以将 map 的 HTML 字符串传递给 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_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM