-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdesktop_app.py
More file actions
120 lines (96 loc) · 2.72 KB
/
desktop_app.py
File metadata and controls
120 lines (96 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
NeuroView Desktop Application
Launches the NeuroView brain viewer as a native desktop application.
"""
import os
import sys
import threading
import time
import webview
import uvicorn
from pathlib import Path
# Get the directory where this script is located
if getattr(sys, 'frozen', False):
# Running as compiled executable
BASE_DIR = Path(sys._MEIPASS)
APP_DIR = Path(os.path.dirname(sys.executable))
else:
# Running as script
BASE_DIR = Path(__file__).parent
APP_DIR = BASE_DIR
# Import the FastAPI app
sys.path.insert(0, str(BASE_DIR / 'backend'))
from server import app
# Server configuration
HOST = '127.0.0.1'
PORT = 8000
SERVER_URL = f'http://{HOST}:{PORT}'
class ServerThread(threading.Thread):
"""Run uvicorn server in a background thread."""
def __init__(self):
super().__init__(daemon=True)
self.server = None
def run(self):
config = uvicorn.Config(
app,
host=HOST,
port=PORT,
log_level="warning",
access_log=False
)
self.server = uvicorn.Server(config)
self.server.run()
def stop(self):
if self.server:
self.server.should_exit = True
def wait_for_server(timeout=10):
"""Wait for the server to be ready."""
import urllib.request
start = time.time()
while time.time() - start < timeout:
try:
urllib.request.urlopen(f'{SERVER_URL}/', timeout=1)
return True
except:
time.sleep(0.1)
return False
def get_html_path():
"""Get the path to the HTML file."""
html_file = BASE_DIR / 'brain_viewer.html'
if html_file.exists():
return str(html_file)
# Fallback to current directory
return str(Path(__file__).parent / 'brain_viewer.html')
def main():
print("Starting NeuroView...")
# Start the backend server
server_thread = ServerThread()
server_thread.start()
# Wait for server to be ready
print("Waiting for server...")
if not wait_for_server():
print("Error: Server failed to start")
sys.exit(1)
print("Server ready!")
# Create the webview window
html_path = get_html_path()
print(f"Loading: {html_path}")
# Create window with the HTML file
window = webview.create_window(
title='NeuroView - Neural Imaging System',
url=html_path,
width=1400,
height=900,
min_size=(1000, 700),
resizable=True,
frameless=False,
easy_drag=False,
text_select=False
)
# Start the GUI (this blocks until window is closed)
webview.start(debug=False)
# Cleanup
print("Shutting down...")
server_thread.stop()
if __name__ == '__main__':
main()