-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun_gui_reactflow.py
More file actions
127 lines (106 loc) · 3.69 KB
/
Copy pathrun_gui_reactflow.py
File metadata and controls
127 lines (106 loc) · 3.69 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
121
122
123
124
125
126
127
#!/usr/bin/env python
"""
ReactFlow Node Editorの起動スクリプト
バックエンド(FastAPI)とフロントエンド(Vite)を起動し、ブラウザを自動で開く
使用方法:
python run_gui_reactflow.py
python run_gui_reactflow.py --config path/to/config.json
"""
import argparse
import os
import subprocess
import sys
import time
import webbrowser
import socket
from pathlib import Path
# 設定
BACKEND_PORT = 8000
FRONTEND_PORT = 5173
FRONTEND_URL = f"http://localhost:{FRONTEND_PORT}"
# パス設定
PROJECT_ROOT = Path(__file__).parent
FRONTEND_DIR = PROJECT_ROOT / "src" / "gui" / "reactflow" / "frontend"
def is_port_open(port: int, host: str = "localhost") -> bool:
"""指定ポートが開いているかチェック"""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.settimeout(1)
result = sock.connect_ex((host, port))
return result == 0
def wait_for_port(port: int, timeout: int = 30) -> bool:
"""ポートが開くまで待機"""
start = time.time()
while time.time() - start < timeout:
if is_port_open(port):
return True
time.sleep(0.5)
return False
def main():
parser = argparse.ArgumentParser(description="ReactFlow Node Editorの起動")
parser.add_argument("--config", type=str, default=None, help="設定ファイルのパス(デフォルトはconfig.json)")
args = parser.parse_args()
# 設定ファイルのパスを環境変数に設定
env = os.environ.copy()
if args.config:
config_path = Path(args.config).resolve()
if not config_path.exists():
print(f"Error: Config file not found: {config_path}")
sys.exit(1)
env["NODE_EDITOR_CONFIG"] = str(config_path)
print(f"Using config: {config_path}")
processes = []
try:
# バックエンド起動
print(f"Starting backend on port {BACKEND_PORT}...")
backend_cmd = [
sys.executable,
"-m",
"uvicorn",
"src.gui.reactflow.backend.main:app",
"--host",
"0.0.0.0",
"--port",
str(BACKEND_PORT),
]
backend_proc = subprocess.Popen(
backend_cmd,
cwd=PROJECT_ROOT,
env=env,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
if sys.platform == "win32"
else 0,
)
processes.append(backend_proc)
# フロントエンド起動
print(f"Starting frontend on port {FRONTEND_PORT}...")
npm_cmd = "npm.cmd" if sys.platform == "win32" else "npm"
frontend_proc = subprocess.Popen(
[npm_cmd, "run", "dev"],
cwd=FRONTEND_DIR,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP
if sys.platform == "win32"
else 0,
)
processes.append(frontend_proc)
# 少し待ってからブラウザを開く(サービス起動中でもブラウザ側でリトライされる)
time.sleep(5)
print(f"Opening browser: {FRONTEND_URL}")
webbrowser.open(FRONTEND_URL)
print("\nPress Ctrl+C to stop all services...")
# プロセス終了を待機
for proc in processes:
proc.wait()
except KeyboardInterrupt:
print("\nShutting down...")
finally:
# 全プロセスを終了
for proc in processes:
if proc.poll() is None:
proc.terminate()
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
proc.kill()
print("All services stopped.")
if __name__ == "__main__":
main()