-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (57 loc) · 1.93 KB
/
main.py
File metadata and controls
76 lines (57 loc) · 1.93 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
"""Graph Launcher — entry point."""
import functools
import http.server
import site
import socket
import sys
import threading
# ── Ensure user site-packages are reachable even in elevated terminals ──
try:
_user_site = site.getusersitepackages()
if isinstance(_user_site, str) and _user_site not in sys.path:
sys.path.append(_user_site) # append — stdlib stays first
except Exception:
pass
from backend.api import API
from utils.logger import setup_logger
from utils.paths import get_frontend_dir
import webview
def _find_free_port() -> int:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
class _SilentHandler(http.server.SimpleHTTPRequestHandler):
"""SimpleHTTPRequestHandler with access logs suppressed."""
def log_message(self, fmt, *args):
pass
def _start_file_server(directory: str, port: int) -> None:
handler = functools.partial(_SilentHandler, directory=directory)
http.server.HTTPServer(("127.0.0.1", port), handler).serve_forever()
def main() -> None:
setup_logger()
api = API()
# Serve the frontend over a local HTTP server so WebView2 (which uses
# file:// by default) can load external resources — external images and
# YouTube iframes are blocked from a file:// origin by WebView2.
port = _find_free_port()
threading.Thread(
target=_start_file_server,
args=(str(get_frontend_dir()), port),
daemon=True,
).start()
window = webview.create_window(
title="Graph Launcher",
url=f"http://127.0.0.1:{port}/index.html",
js_api=api,
width=1280,
height=800,
min_size=(960, 640),
resizable=True,
frameless=True,
easy_drag=False,
background_color="#0f0f0f",
)
api.set_window(window)
webview.start(debug="--debug" in sys.argv)
if __name__ == "__main__":
main()