Search code examples
javascriptpyqtpyqt5qwebviewqwebkit

PyQt5 QWebView: Load .html file that loads .js file


I am trying to show a leaflet map with QWebView (inspired from here). The structure of my folder looks as follows:

webkit_leaflet/
├── map.html
├── map.js
└── map.py

When I run map.py with all the content from map.html and map.js included, then the code works.

from PyQt5 import QtWidgets, QtWebKitWidgets
import sys


# Create application
app = QtWidgets.QApplication(sys.argv)

# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')

# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)

# Create QWebView
view = QtWebKitWidgets.QWebView()

# include code from map.html and map.js

view.setHtml('''
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <style>
        body { padding: 0; margin: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>
    <script>
        var map = L.map('map').setView([42.35, -71.08], 13);
        L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
        {
            maxZoom: 18,
            attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
                '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
                'Imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
            id: 'examples.map-i86nkdio',
        }).addTo(map);
    </script>
</body>
</html>

''')

# Add QWebView to the layout
layout.addWidget(view)

# Show window, run app
win.show()
app.exec_()

However, If I try to load map.html with QtCore.QUrl() nothing happens.

from PyQt5 import QtCore, QtWidgets, QtWebKitWidgets
import sys


# Create application
app = QtWidgets.QApplication(sys.argv)

# Add window
win = QtWidgets.QWidget()
win.setWindowTitle('QWebView Map Test')

# Add layout
layout = QtWidgets.QVBoxLayout()
win.setLayout(layout)

# Create QWebView
view = QtWebKitWidgets.QWebView()

# load .html file
view.load(QtCore.QUrl('map.html'))

layout.addWidget(view)

win.show()
app.exec_()

Could anybody please tell me how to show the content of the .html file in PyQt5 when I load a JavaScript file from within the external .html file?

Here is the code for map.html:

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
    <script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
    <style>
        body { padding: 0; margin: 0; }
        html, body, #map { height: 100%; }
    </style>
</head>
<body>
    <div id="map"></div>
    <script src="map.js"></script>
</body>
</html>

And also the code for map.js

var map = L.map('map').setView([42.35, -71.08], 13);
L.tileLayer('http://tiles.mapc.org/basemap/{z}/{x}/{y}.png',
{
    maxZoom: 18,
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, ' +
        '<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, ' +
        'Imagery &copy; <a href="http://mapbox.com">Mapbox</a>',
    id: 'examples.map-i86nkdio',
}).addTo(map);

Solution

  • Use QUrl.fromLocalFile to pass the html file. It also seems you need to pass the absolute path to the file.

    import os
    view.load(QtCore.QUrl.fromLocalFile(os.path.abspath('map.html')))