import sys
from PyQt5 import QtCore, QtWidgets, QtGui, QtWebEngineWidgets


def init_gui(application, port=5000, width=300, height=400,
             window_title="PyFladesk", icon="appicon.png"):

    ROOT_URL = 'http://localhost:{}'.format(port)

    # open links in browser from http://stackoverflow.com/a/3188942/1103397 :D
    # thanks to https://github.com/marczellm/qhangups/blob/cfed73ee4383caed1568c0183a9906180f01cb00/qhangups/WebEnginePage.py
    def link_clicked(url, typ, ismainframe):
        ready_url = url.toEncoded().data().decode()
        is_clicked = typ == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked
        is_not_internal = ROOT_URL not in ready_url
        if is_clicked and is_not_internal:
            QtGui.QDesktopServices.openUrl(url)
            return False
        return True

    def run_app():
        application.run(port=port, threaded=True)

    # Application Level
    qtapp = QtWidgets.QApplication(sys.argv)
    webapp = QtCore.QThread()
    webapp.__del__ = webapp.wait
    webapp.run = run_app
    webapp.start()
    qtapp.aboutToQuit.connect(webapp.terminate)

    # Main Window Level
    window = QtWidgets.QMainWindow()
    window.resize(width, height)
    window.setWindowTitle(window_title)
    window.setWindowIcon(QtGui.QIcon(icon))

    # WebView Level
    window.webView = QtWebEngineWidgets.QWebEngineView(window)
    window.setCentralWidget(window.webView)

    # WebPage Level
    page = QtWebEngineWidgets.QWebEnginePage()
    page.acceptNavigationRequest = link_clicked
    page.load(QtCore.QUrl(ROOT_URL))
    window.webView.setPage(page)

    window.show()

    return qtapp.exec_()

@@ -73,21 +73,72 @@
 s%0A%0A%0A
+class ApplicationThread(QtCore.QThread):%0A    
 def 
+__
 init_
-gui(
+_(self, 
 appl
@@ -159,333 +159,285 @@
 5000
-, width=300, height=400,%0A             window_title=%22PyFladesk%22, icon=%22
+):%0A        super(ApplicationThread, self).__init__()%0A        self.application = 
 app
+l
 ic
-on.png%22):%0A%0A    ROOT_URL = 'http://localhost:%7B%7D'.format(
+ation%0A        self.port = 
 port
-)
 %0A%0A    
-# open links in browser from http://stackoverflow.com/a/3188942/1103397 :D%0A    # thanks to https://github.com/marczellm/qhangups/blob/cfed73ee4383caed1568c0183a9906180f01cb00/qhangups/
+def __del__(self):%0A        self.wait()%0A%0A    def run(self):%0A        self.application.run(port=self.port, threaded=True)%0A%0A%0Aclass WebPage(QtWebEngineWidgets.Q
 WebE
@@ -449,11 +449,10 @@
 Page
-.py
+):
 %0A   
@@ -460,37 +460,232 @@
 def 
-link_clicked(url, typ
+__init__(self, root_url):%0A        super(WebPage, self).__init__()%0A        self.root_url = root_url%0A%0A    def home(self):%0A        self.load(QtCore.QUrl(self.root_url))%0A%0A    def acceptNavigationRequest(self, url, kind
 , is
+_
 main
+_
 fram
@@ -700,191 +700,190 @@
     
-ready_url = url.toEncoded().data().decode()%0A        is_clicked = typ == QtWebEngineWidgets.QWebEnginePage.NavigationTypeLinkClicked%0A        is_not_internal = ROOT_URL not in ready_url
+%22%22%22Open external links in browser and internal links in the webview%22%22%22%0A        ready_url = url.toEncoded().data().decode()%0A        is_clicked = kind == self.NavigationTypeLinkClicked
 %0A   
@@ -909,22 +909,37 @@
 and 
-is_not_interna
+self.root_url not in ready_ur
 l:%0A 
@@ -1029,82 +1029,256 @@
 urn 
-True%0A%0A    def run_app():%0A        application.run(port=port, threaded=True)
+super(WebPage, self).acceptNavigationRequest(url, kind, is_main_frame)%0A%0A%0Adef init_gui(application, port=5000, width=300, height=400,%0A             window_title=%22PyFladesk%22, icon=%22appicon.png%22, argv=None):%0A    if argv is None:%0A        argv = sys.argv
 %0A%0A  
@@ -1334,20 +1334,16 @@
 ication(
-sys.
 argv)%0A  
@@ -1357,82 +1357,44 @@
 p = 
-QtCore.QThread()%0A    webapp.__del__ = webapp.wait%0A    webapp.run = run_app
+ApplicationThread(application, port)
 %0A   
@@ -1657,31 +1657,24 @@
 w Level%0A    
-window.
 webView = Qt
@@ -1741,23 +1741,16 @@
 lWidget(
-window.
 webView)
@@ -1786,140 +1786,72 @@
 e = 
-Qt
 Web
-EngineWidgets.QWebEnginePage()%0A    page.acceptNavigationRequest = link_clicked%0A    page.load(QtCore.QUrl(ROOT_URL))%0A    window.
+Page('http://localhost:%7B%7D'.format(port))%0A    page.home()%0A    
 webV
@@ -1887,17 +1887,16 @@
 .show()%0A
-%0A
     retu

Improve readability