-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_frontend.py
More file actions
68 lines (48 loc) · 1.92 KB
/
run_frontend.py
File metadata and controls
68 lines (48 loc) · 1.92 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
import asyncio
import socket
import sys
import os
# Ensure we're in the repo root directory
repo_root = os.path.dirname(os.path.abspath(__file__))
os.chdir(repo_root)
def _is_interactive() -> bool:
return 'spyder_kernels' in sys.modules or 'ipykernel' in sys.modules
def _enable_interactive_asyncio_patch() -> None:
import nest_asyncio
nest_asyncio.apply()
patched_asyncio_run = asyncio.run
def asyncio_run_compat(main, *, debug=None, loop_factory=None):
return patched_asyncio_run(main, debug=debug)
asyncio.run = asyncio_run_compat
def _select_port(preferred: int = 8080, max_tries: int = 10) -> int:
for port in range(preferred, preferred + max_tries):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if sock.connect_ex(('127.0.0.1', port)) != 0:
return port
raise RuntimeError(f'No free port found in range {preferred}-{preferred + max_tries - 1}')
if _is_interactive():
_enable_interactive_asyncio_patch()
from nicegui import ui
from GUI.page_run import render_run_page
from GUI.page_results import render_results_page
from GUI.page_figures import render_figures_page
from GUI.page_settings import render_settings_page
# Define the routes
@ui.page('/', title='Run | FTT')
def run_page():
render_run_page()
@ui.page('/results', title='Results | FTT')
def results_page():
render_results_page()
@ui.page('/figures', title='Figures | FTT')
def figures_page():
render_figures_page()
@ui.page('/settings', title='Settings | FTT')
def settings_page():
render_settings_page()
# Select an available port and start the server
selected_port = _select_port(8080)
if selected_port != 8080:
print(f'Port 8080 is in use, starting frontend on port {selected_port} instead.')
ui.run(title="FTT", port=selected_port, favicon='GUI/images/ftt_favicon.png', reload=False)