17

I have written a single-script (i.e. full-stack) GUI in PyQT which queries an SQL database, performs some functions with the query outputs and then displays these outputs graphically in a QtGui.QApplication. However, I have decided that I would like to convert this into a Web application that I can host on a local server.

Running the code in this tutorial produces something similar to what I am after but it runs in the Python launcher rather than on a web page. What I want to do - to prevent me from having to rewrite all of the graphical widgets in another web-based framework - is to separate the script into a back-end which deals with querying the database and using the query results and a front-end written with PyQt and HTML (as in the linked tutorial) containing PyQt widgets embedded into an HTML script which I can run as a website. Is this possible?

If it is possible to do this, how would I go about writing it (for instance, how would you change the code in the linked tutorial) such that it can be run from a browser/hosted on a server rather than run as an application? There may be some technical blunders in this question as my understanding of web development is limited. Any help will be appreciated.

For ease of answering, here is the joined code from the linked tutorial:

import sys
from PyQt4.QtCore import QSize, Qt
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *

html = \
"""<html>
<head>
<title>Python Web Plugin Test</title>
</head>

<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/widget" width="200" height="200"></object>
<p>This is a Web plugin written in Python.</p>
</body>
</html>
"""

class WebWidget(QWidget):

    def paintEvent(self, event):
        painter = QPainter()
        painter.begin(self)
        painter.setBrush(Qt.white)
        painter.setPen(Qt.black)
        painter.drawRect(self.rect().adjusted(0, 0, -1, -1))
        painter.setBrush(Qt.red)
        painter.setPen(Qt.NoPen)
        painter.drawRect(self.width()/4, self.height()/4,
                         self.width()/2, self.height()/2)
        painter.end()

    def sizeHint(self):
        return QSize(100, 100)

class WebPluginFactory(QWebPluginFactory):

    def __init__(self, parent = None):
        QWebPluginFactory.__init__(self, parent)

    def create(self, mimeType, url, names, values):
        if mimeType == "x-pyqt/widget":
            return WebWidget()

    def plugins(self):
        plugin = QWebPluginFactory.Plugin()
        plugin.name = "PyQt Widget"
        plugin.description = "An example Web plugin written with PyQt."
        mimeType = QWebPluginFactory.MimeType()
        mimeType.name = "x-pyqt/widget"
        mimeType.description = "PyQt widget"
        mimeType.fileExtensions = []
        plugin.mimeTypes = [mimeType]
        print "plugins"
        return [plugin]

if __name__ == "__main__":

    app = QApplication(sys.argv)
    QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True)
    view = QWebView()
    factory = WebPluginFactory()
    view.page().setPluginFactory(factory)
    view.setHtml(html)
    view.show()
    sys.exit(app.exec_())
7
  • 1
    You cannot do what you are asking. That example only works because it is running inside a PyQt application, which enaes you to insert Qt widgets. You will not be able to get it to work in a generic browser. May 4, 2017 at 11:29
  • So, to clarify, it is not possible to embed Qt widgets in an HTML document defined within Python in a generic browser? May 4, 2017 at 11:32
  • 2
    It is the QWebView that is interpreting the HTML and rendering the Qt widgets. A generic browser (aka Chrome, IE, Firefox) will not know how to interpret the HTML that defines the Qt widgets. There is some work being done to allow you to run Qt code written in C++ to run in a browser (basically a Javascript emulator for C++ Qt) but I don't think it's particularly well cooked and obviously only works for code written in C++, not Python, and it's not expecting HTML. May 5, 2017 at 0:05
  • 2
    Alternatively, if you want to drop the "local" part of the "local server" requirement, you could try running a PyQt GUI on Amazon AppStream 2 which allows you to run applications remotely and access them through any modern browser. May 6, 2017 at 12:46
  • 1
    I have a similar case (with PyQt5) and since this question is more than 2 years old I would like to known if there is anything new
    – Stéphane
    Nov 29, 2019 at 9:38

0

Browse other questions tagged or ask your own question.