how to run flask with pyqt5

Solutions on MaxInterview for how to run flask with pyqt5 by the best coders in the world

showing results for - "how to run flask with pyqt5"
Hans
13 Nov 2017
1from PyQt5.QtWidgets import QLabel, QVBoxLayout, QMainWindow, QApplication, QWidget
2from flask import Flask, render_template
3
4from threading import Thread
5import sys
6
7# You can copy and paste this code for test and run it
8
9class MainWindow(QMainWindow):
10    def __init__(self, *args, **kwargs):
11        super(MainWindow, self).__init__(*args, **kwargs)
12        self.setWindowTitle("Sorted Ware House")
13        widget = QWidget()
14        label = QLabel("Flask is running...")
15        self.layout = QVBoxLayout()
16        self.layout.addWidget(label)
17        widget.setLayout(self.layout)
18        self.setCentralWidget(widget)
19
20#   Creating instance of QApplication
21if __name__ == "__main__":
22    app = QApplication(sys.argv)
23    window = MainWindow()
24    window.show()
25    app_ = Flask(__name__)
26
27#   setting our root
28    @app_.route('/')
29    def index():
30        return render_template('index.html')
31
32#   Preparing parameters for flask to be given in the thread
33#   so that it doesn't collide with main thread
34    kwargs = {'host': '127.0.0.1', 'port': 5000, 'threaded': True, 'use_reloader': False, 'debug': False}
35
36#   running flask thread
37    flaskThread = Thread(target=app_.run, daemon=True, kwargs=kwargs).start()
38
39    app.exec_()
40