108 lines
3.0 KiB
Python
108 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
GTK Wrapper for Flask Application
|
|
Runs the Flask app in a separate thread and displays it in a GTK window using WebKit
|
|
"""
|
|
|
|
import sys
|
|
import threading
|
|
import time
|
|
import gi
|
|
import os
|
|
from pathlib import Path
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
gi.require_version('WebKit2', '4.1')
|
|
|
|
from gi.repository import Gtk, WebKit2, GLib
|
|
|
|
# Import the Flask app
|
|
from app import app as flask_app
|
|
|
|
|
|
class FlaskWindow(Gtk.ApplicationWindow):
|
|
def __init__(self, application):
|
|
super().__init__(application=application)
|
|
|
|
self.flask_thread = None
|
|
self.flask_port = 5000
|
|
self.flask_host = '127.0.0.1'
|
|
self.flask_url = f'http://{self.flask_host}:{self.flask_port}/'
|
|
|
|
# Configure window
|
|
self.set_title('Shooting Range Management System')
|
|
self.set_default_size(1200, 800)
|
|
self.set_position(Gtk.WindowPosition.CENTER)
|
|
|
|
# Create WebKit view
|
|
self.webview = WebKit2.WebView()
|
|
self.webview.get_settings().set_property('enable-developer-extras', True)
|
|
|
|
# Create container
|
|
container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
container.pack_start(self.webview, True, True, 0)
|
|
|
|
self.add(container)
|
|
self.connect('destroy', self.on_destroy)
|
|
|
|
# Start Flask server in background
|
|
self.start_flask_server()
|
|
|
|
# Load the Flask app in WebView after a short delay to ensure server is ready
|
|
GLib.timeout_add(2000, self.load_flask_url)
|
|
|
|
def start_flask_server(self):
|
|
"""Start Flask development server in a separate thread"""
|
|
def run_flask():
|
|
# Change to app directory to ensure relative paths work
|
|
app_dir = Path(__file__).parent
|
|
os.chdir(app_dir)
|
|
|
|
# Run Flask app
|
|
flask_app.run(
|
|
host=self.flask_host,
|
|
port=self.flask_port,
|
|
debug=False,
|
|
use_reloader=False,
|
|
threaded=True
|
|
)
|
|
|
|
self.flask_thread = threading.Thread(target=run_flask, daemon=True)
|
|
self.flask_thread.start()
|
|
print(f"Flask server starting on {self.flask_url}")
|
|
|
|
def load_flask_url(self):
|
|
"""Load the Flask application in the WebView"""
|
|
try:
|
|
self.webview.load_uri(self.flask_url)
|
|
print(f"Loading {self.flask_url}")
|
|
return False # Don't repeat
|
|
except Exception as e:
|
|
print(f"Error loading Flask URL: {e}")
|
|
return False
|
|
|
|
def on_destroy(self, widget):
|
|
"""Handle window close event"""
|
|
print("Closing application...")
|
|
Gtk.main_quit()
|
|
|
|
|
|
class FlaskGTKApp(Gtk.Application):
|
|
def __init__(self):
|
|
super().__init__(application_id='com.example.flaskgtk')
|
|
self.connect('activate', self.on_activate)
|
|
|
|
def on_activate(self, app):
|
|
window = FlaskWindow(app)
|
|
window.show_all()
|
|
window.present()
|
|
|
|
|
|
def main():
|
|
app = FlaskGTKApp()
|
|
return app.run(sys.argv)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|