qwebengineview pyqt5 code example

Example 1: install qwebengineview pyqt5

pip install PyQtWebEngine --no-cache-dir

Example 2: how to use qwebengineview in pyqt5

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from flask import Flask, render_template, request

import threading
import sys

# You can copy past this code and run it
# Just change the index.html to your html file

class MainWindow(QMainWindow):
    # In here i've set the QWebEngineView as CentralWidget
    # You can use a layout to position it where you want
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)

        self.setWindowTitle("Sorted Ware House")
        self.setMinimumSize(900, 550)

        self.browser = QWebEngineView() # create web view
        self.browser.setUrl(QUrl("http://127.0.0.1:5000")) # set root
        self.setCentralWidget(self.browser) # place it in QMainWindow

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()

    app_ = Flask(__name__)
    @app_.route('/')
    def index():
        return render_template('index.html')
    kwargs = {'host': '127.0.0.1', 'port': 5000 , 'threaded' : True, 'use_reloader': False, 'debug':False}
    t1 = threading.Thread(target=app_.run, daemon = True, kwargs=kwargs).start()

    app.exec_()
# For smooth scrolling add '--enable-smooth-scrolling' parameter when
#  running the app as your app's argument.
# like : python app.py --enable-smooth-scrolling