How can I directly input HTML code in QtCore.QUrl without referring to file path?

379 views Asked by At

I am building an app in Python using PyQt5 and I am going to freeze it. Therefor I would like to have my python code independent from any local file path. At some point I am loading an html code with:

self.pageXXX.html_code.load(QtCore.QUrl.fromLocalFile(QtCore.QDir.current().filePath("example.html")))

How can I change this command so that I can paste the HTML code directly in the html.load()?

Something like:

    self.pageXXX.html_code.load('''
                                      HTML code
                                ''')
1

There are 1 answers

0
Carlo Bianchi On

As suggested by @Maurice Meyer, the problem was in the way I set QWebChannel. I just added

<script src="qrc:///qtwebchannel/qwebchannel.js"></script>

at the top of my HTML code and then:

from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView

in my Python code. And the magic happened!