desktop_app_install

This commit is contained in:
2025-11-08 19:24:05 +01:00
parent 88cfd9c05f
commit 2b5ec1d9b1
4 changed files with 270 additions and 0 deletions
+140
View File
@@ -0,0 +1,140 @@
# GTK Wrapper for Flask Application
This wrapper embeds your Flask web application in a native GTK window using WebKit2, providing a desktop application experience.
## Installation
### Prerequisites
- Python 3.6+
- GTK 3.0
- WebKit2 4.0
- PyGObject
### Automatic Setup (Linux)
Run the provided setup script:
```bash
bash setup_gtk_wrapper.sh
```
This will install all required system packages and Python dependencies.
### Manual Setup
If the automated script doesn't work for your system, install the packages manually:
**Ubuntu/Debian:**
```bash
sudo apt-get update
sudo apt-get install -y python3-gi gir1.2-gtk-3.0 gir1.2-webkit2-4.1 libgtk-3-0 libwebkit2gtk-4.1-0
pip3 install flask
```
**Fedora/RHEL:**
```bash
sudo dnf install python3-gobject gtk3 webkit2-gtk3
pip3 install flask
```
**Arch Linux:**
```bash
sudo pacman -S python-gobject gtk3 webkit2gtk
pip3 install flask
```
**macOS (via Homebrew):**
```bash
brew install gtk+3 webkit2gtk python3
pip3 install flask
```
## Usage
Run the GTK wrapper with:
```bash
python3 gtk_wrapper.py
```
Or make it executable and run directly:
```bash
chmod +x gtk_wrapper.py
./gtk_wrapper.py
```
## Features
- **Native GTK Window**: Runs your Flask app in a native GTK 3 window
- **WebKit2 Rendering**: Uses WebKit2 for modern web rendering
- **Background Flask Server**: Automatically starts your Flask app in a background thread
- **Developer Mode**: Developer tools are enabled for debugging (inspect element)
- **Responsive UI**: Window is resizable and responsive
## Configuration
You can customize the wrapper by editing `gtk_wrapper.py`:
### Window Size
```python
self.set_default_size(1200, 800) # Change to desired width, height
```
### Window Title
```python
self.set_title('Your App Title')
```
### Flask Server Port
```python
self.flask_port = 5000 # Change to desired port
```
### Developer Tools
To disable developer tools (inspect element), comment out:
```python
self.webview.get_settings().set_property('enable-developer-extras', True)
```
## Troubleshooting
### "No module named 'gi'"
Install PyGObject: `pip3 install PyGObject`
### "gi.repository.Gtk" not found
Install GTK bindings: `sudo apt-get install python3-gi gir1.2-gtk-3.0`
### "gi.repository.WebKit2" not found
Install WebKit2 bindings: `sudo apt-get install gir1.2-webkit2-4.0`
### Flask server won't start
- Check if port 5000 is already in use
- Ensure you're running from the correct directory
- Check Flask app for syntax errors
### Page shows "Unable to reach" error
- Wait a few seconds for the Flask server to start
- Check the terminal for Flask startup messages
- Ensure all Flask dependencies are installed
## File Structure
```
TV_APP_V1.0.0/
├── app.py # Your Flask application
├── gtk_wrapper.py # GTK wrapper (this file)
├── setup_gtk_wrapper.sh # Automated setup script
├── GTK_WRAPPER_README.md # This file
├── templates/ # Flask templates
├── static/ # Flask static files
└── locales/ # Language files
```
## Notes
- The Flask app runs in debug mode disabled to prevent auto-reloading conflicts with GTK
- The app uses threading to run Flask in the background without blocking GTK events
- All Flask features (routing, sessions, file uploads, etc.) work normally
## Support
For issues with the Flask app itself, see the main app documentation.
For GTK/WebKit issues, refer to the respective project documentation.
Binary file not shown.
+107
View File
@@ -0,0 +1,107 @@
#!/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())
+23
View File
@@ -0,0 +1,23 @@
#!/bin/bash
# Setup script for GTK Wrapper dependencies
echo "Installing GTK wrapper dependencies..."
# Update package list
sudo apt-get update
# Install GTK3 and WebKit2 development libraries
echo "Installing GTK3 and WebKit2 libraries..."
sudo apt-get install -y \
python3-gi \
gir1.2-gtk-3.0 \
gir1.2-webkit2-4.0 \
libgtk-3-0 \
libwebkit2gtk-4.0-37
# Install pip packages if needed
echo "Checking Python dependencies..."
pip3 install flask
echo "Setup complete! You can now run the GTK wrapper with:"
echo " python3 gtk_wrapper.py"