Skip to content

Instantly share code, notes, and snippets.

@holdenweb
Created April 12, 2023 20:19
Show Gist options
  • Save holdenweb/b1bf0bdae76cdbbbd05cccbc4c49cefc to your computer and use it in GitHub Desktop.
Save holdenweb/b1bf0bdae76cdbbbd05cccbc4c49cefc to your computer and use it in GitHub Desktop.
Skeleton flask server with QT interface
'''
qtflask.py: Run a flask server as a subordinate thread to a Qt GUI application
'''
import sys
import threading
from PyQt6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from flask import Flask, jsonify
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Hello World')
self.setGeometry(100, 100, 300, 200)
button = QPushButton('Click me', self)
button.setToolTip('Click me to say Hello')
button.move(100, 70)
button.clicked.connect(self.say_hello)
self.show()
def say_hello(self):
QMessageBox.information(self, 'Message', 'Hello, World!')
def flask_server():
app = Flask(__name__)
@app.route('/api/greeting')
def greeting():
return jsonify({'message': 'Hello, World!'})
app.run()
if __name__ == '__main__':
threading.Thread(target=flask_server, daemon=True).start()
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec())
click==8.1.3
Flask==2.2.3
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
PyQt6==6.5.0
PyQt6-Qt6==6.5.0
PyQt6-sip==13.5.0
Werkzeug==2.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment