.
This commit is contained in:
@@ -34,6 +34,12 @@ from fastapi import Request
|
|||||||
from fastapi.responses import RedirectResponse
|
from fastapi.responses import RedirectResponse
|
||||||
from starlette.middleware.base import BaseHTTPMiddleware
|
from starlette.middleware.base import BaseHTTPMiddleware
|
||||||
import typer
|
import typer
|
||||||
|
import logging
|
||||||
|
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
# TODO fix ssl
|
||||||
|
VERIFY_SSL = False
|
||||||
|
|
||||||
SOCKET_PATH = "tcp://localhost:9976"
|
SOCKET_PATH = "tcp://localhost:9976"
|
||||||
|
|
||||||
@@ -57,12 +63,14 @@ LOGGED_IN_USER = "None"
|
|||||||
|
|
||||||
|
|
||||||
REFRESH_TRIGGERED = True
|
REFRESH_TRIGGERED = True
|
||||||
|
RELOAD_ANON_FILES = False
|
||||||
SHUTDOWN = False
|
SHUTDOWN = False
|
||||||
|
|
||||||
DELETE_FILES_ON_UPLOAD = True
|
DELETE_FILES_ON_UPLOAD = True
|
||||||
|
|
||||||
DEBUG = False
|
DEBUG = False
|
||||||
|
|
||||||
|
logging.getLogger('niceGUI').setLevel(logging.INFO)
|
||||||
|
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
BASE_SITE_URL = "http://localhost:8000"
|
BASE_SITE_URL = "http://localhost:8000"
|
||||||
@@ -98,6 +106,7 @@ async def read_nng_messages():
|
|||||||
ui.notify("Export done", color="positive")
|
ui.notify("Export done", color="positive")
|
||||||
global REFRESH_TRIGGERED
|
global REFRESH_TRIGGERED
|
||||||
REFRESH_TRIGGERED = True
|
REFRESH_TRIGGERED = True
|
||||||
|
await sub.asend(b"done")
|
||||||
logger.debug("loaded")
|
logger.debug("loaded")
|
||||||
case _:
|
case _:
|
||||||
logger.error("invalid message")
|
logger.error("invalid message")
|
||||||
@@ -188,6 +197,10 @@ async def reload_anonymized_start(progress_bar, queue):
|
|||||||
)
|
)
|
||||||
progress_bar.visible = False
|
progress_bar.visible = False
|
||||||
|
|
||||||
|
for client in Client.instances.values():
|
||||||
|
if not client.has_socket_connection:
|
||||||
|
continue
|
||||||
|
with client:
|
||||||
ui.notify(f"Files reloaded: {len(loaded_files)}, [Series: {len(loaded_series)}] ", color="positive")
|
ui.notify(f"Files reloaded: {len(loaded_files)}, [Series: {len(loaded_series)}] ", color="positive")
|
||||||
# load_series_view(loaded_series, loaded_series_data)
|
# load_series_view(loaded_series, loaded_series_data)
|
||||||
loaded_series_ui.refresh()
|
loaded_series_ui.refresh()
|
||||||
@@ -273,17 +286,21 @@ def load_files(q: Queue, src_path: Path | None = None):
|
|||||||
dataset, output_file = anonymizer.anonymize_file(file, remove_original=True)
|
dataset, output_file = anonymizer.anonymize_file(file, remove_original=True)
|
||||||
# with processed_files:
|
# with processed_files:
|
||||||
# ui.item(f"{datetime.now().strftime('%H:%M:%S')} - {file.name} -> {output_file.name}")
|
# ui.item(f"{datetime.now().strftime('%H:%M:%S')} - {file.name} -> {output_file.name}")
|
||||||
|
try:
|
||||||
|
series_description = dataset.SeriesDescription
|
||||||
|
except AttributeError:
|
||||||
|
series_description = ""
|
||||||
|
|
||||||
loaded_files[output_file] = (
|
loaded_files[output_file] = (
|
||||||
dataset.StudyDescription,
|
dataset.StudyDescription,
|
||||||
dataset.SeriesDescription,
|
series_description,
|
||||||
str(dataset.SeriesInstanceUID),
|
str(dataset.SeriesInstanceUID),
|
||||||
)
|
)
|
||||||
|
|
||||||
loaded_series[dataset.SeriesInstanceUID].append(output_file)
|
loaded_series[dataset.SeriesInstanceUID].append(output_file)
|
||||||
loaded_series_data[dataset.SeriesInstanceUID] = (
|
loaded_series_data[dataset.SeriesInstanceUID] = (
|
||||||
dataset.StudyDescription,
|
dataset.StudyDescription,
|
||||||
dataset.SeriesDescription,
|
series_description
|
||||||
)
|
)
|
||||||
|
|
||||||
q.put_nowait((n + 1) / to_process_len)
|
q.put_nowait((n + 1) / to_process_len)
|
||||||
@@ -333,12 +350,15 @@ def reload_anonymized(q: Queue):
|
|||||||
|
|
||||||
|
|
||||||
def clear_anonymized_files():
|
def clear_anonymized_files():
|
||||||
|
global RELOAD_ANON_FILES
|
||||||
for file in ANON_PATH.iterdir():
|
for file in ANON_PATH.iterdir():
|
||||||
if file.is_file():
|
if file.is_file():
|
||||||
file.unlink()
|
file.unlink()
|
||||||
|
|
||||||
logger.debug("Deleted all files in ANON_PATH")
|
logger.debug("Deleted all files in ANON_PATH")
|
||||||
|
|
||||||
|
RELOAD_ANON_FILES = True
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@@ -380,6 +400,7 @@ def upload_files(q, rqst):
|
|||||||
f"{BASE_SITE_URL}/api/atlas/upload_dicom",
|
f"{BASE_SITE_URL}/api/atlas/upload_dicom",
|
||||||
# data=data,
|
# data=data,
|
||||||
files=files,
|
files=files,
|
||||||
|
verify=VERIFY_SSL
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.debug(f"Resp: {resp}")
|
logger.debug(f"Resp: {resp}")
|
||||||
@@ -447,9 +468,11 @@ def login() -> Optional[RedirectResponse]:
|
|||||||
global rqst, LOGIN_SUCCESS, LOGGED_IN_USER
|
global rqst, LOGIN_SUCCESS, LOGGED_IN_USER
|
||||||
rqst = requests.session()
|
rqst = requests.session()
|
||||||
try:
|
try:
|
||||||
rsp = rqst.get(LOGIN_URL)
|
rsp = rqst.get(LOGIN_URL, verify=VERIFY_SSL)
|
||||||
except requests.exceptions.ConnectionError:
|
except requests.exceptions.ConnectionError as e:
|
||||||
ui.notify("Connection error!", color="negative")
|
ui.notify("Connection error!", color="negative")
|
||||||
|
logger.debug(e)
|
||||||
|
print(traceback.format_exc())
|
||||||
return
|
return
|
||||||
|
|
||||||
token = (
|
token = (
|
||||||
@@ -474,6 +497,7 @@ def login() -> Optional[RedirectResponse]:
|
|||||||
data=data,
|
data=data,
|
||||||
# headers=header,
|
# headers=header,
|
||||||
# cookies=cookies
|
# cookies=cookies
|
||||||
|
verify=VERIFY_SSL
|
||||||
)
|
)
|
||||||
|
|
||||||
print(rsp.content)
|
print(rsp.content)
|
||||||
@@ -543,7 +567,9 @@ def main_page():
|
|||||||
|
|
||||||
# typer_app()
|
# typer_app()
|
||||||
# Create a queue to communicate with the heavy computation process
|
# Create a queue to communicate with the heavy computation process
|
||||||
|
logger.debug("PRE QUEUE")
|
||||||
queue = Manager().Queue()
|
queue = Manager().Queue()
|
||||||
|
logger.debug("POST QUEUE")
|
||||||
# Update the progress bar on the main process
|
# Update the progress bar on the main process
|
||||||
ui.timer(
|
ui.timer(
|
||||||
0.1,
|
0.1,
|
||||||
@@ -551,6 +577,7 @@ def main_page():
|
|||||||
queue.get() if not queue.empty() else progressbar.value
|
queue.get() if not queue.empty() else progressbar.value
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
logger.debug("POST TImER")
|
||||||
|
|
||||||
# Create a queue to communicate with the heavy computation process
|
# Create a queue to communicate with the heavy computation process
|
||||||
upload_queue = Manager().Queue()
|
upload_queue = Manager().Queue()
|
||||||
@@ -561,6 +588,7 @@ def main_page():
|
|||||||
upload_queue.get() if not upload_queue.empty() else upload_progressbar.value
|
upload_queue.get() if not upload_queue.empty() else upload_progressbar.value
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
logger.debug("POST TImER")
|
||||||
|
|
||||||
upload_progress = ui.row().classes("w-full place-content-center bg-blue-900")
|
upload_progress = ui.row().classes("w-full place-content-center bg-blue-900")
|
||||||
|
|
||||||
@@ -622,11 +650,14 @@ def main_page():
|
|||||||
ui.run_javascript("window.close()")
|
ui.run_javascript("window.close()")
|
||||||
|
|
||||||
async def watch_for_refresh():
|
async def watch_for_refresh():
|
||||||
global REFRESH_TRIGGERED
|
global REFRESH_TRIGGERED, RELOAD_ANON_FILES
|
||||||
while not app.is_stopped:
|
while not app.is_stopped:
|
||||||
if REFRESH_TRIGGERED:
|
if REFRESH_TRIGGERED:
|
||||||
REFRESH_TRIGGERED = False
|
REFRESH_TRIGGERED = False
|
||||||
await load_files_start(anon_progress, queue)
|
await load_files_start(anon_progress, queue)
|
||||||
|
if RELOAD_ANON_FILES:
|
||||||
|
RELOAD_ANON_FILES = False
|
||||||
|
await reload_anonymized_start(anon_progress, queue)
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
asyncio.create_task(watch_for_refresh())
|
asyncio.create_task(watch_for_refresh())
|
||||||
@@ -651,32 +682,48 @@ def launch_app(
|
|||||||
work_dir: Path = typer.Option(WORK_DIR, help="Working directory"),
|
work_dir: Path = typer.Option(WORK_DIR, help="Working directory"),
|
||||||
nng: bool = typer.Option(True, help="Use nng"),
|
nng: bool = typer.Option(True, help="Use nng"),
|
||||||
native_mode: bool = typer.Option(False, help="Use native mode"),
|
native_mode: bool = typer.Option(False, help="Use native mode"),
|
||||||
|
debug: bool = typer.Option(False, help="Debug mode")
|
||||||
):
|
):
|
||||||
global WORK_DIR, EXPORT_PATH, PROCESS_PATH, ANON_PATH
|
global WORK_DIR, EXPORT_PATH, PROCESS_PATH, ANON_PATH, DEBUG
|
||||||
|
|
||||||
WORK_DIR = work_dir
|
WORK_DIR = work_dir
|
||||||
EXPORT_PATH = WORK_DIR / Path("export/")
|
EXPORT_PATH = WORK_DIR / Path("export/")
|
||||||
PROCESS_PATH = WORK_DIR / Path("to_process/")
|
PROCESS_PATH = WORK_DIR / Path("to_process/")
|
||||||
ANON_PATH = WORK_DIR / Path("anon/")
|
ANON_PATH = WORK_DIR / Path("anon/")
|
||||||
|
DEBUG = debug
|
||||||
|
|
||||||
logger.debug(f"Work dir: {WORK_DIR}")
|
logger.debug(f"Work dir: {WORK_DIR}")
|
||||||
|
|
||||||
if nng:
|
if nng:
|
||||||
|
socket_error = False
|
||||||
try:
|
try:
|
||||||
with pynng.Pair0(listen=SOCKET_PATH) as sub:
|
with pynng.Pair0(listen=SOCKET_PATH) as sub:
|
||||||
pass
|
pass
|
||||||
except pynng.exceptions.AddressInUse:
|
except pynng.exceptions.AddressInUse:
|
||||||
logger.debug("Address in use")
|
logger.debug("Address in use")
|
||||||
with pynng.Pair0(dial=SOCKET_PATH) as sub:
|
try:
|
||||||
|
with pynng.Pair0(dial=SOCKET_PATH, send_timeout=4000, recv_timeout=4000) as sub:
|
||||||
sub.send(b"loaded")
|
sub.send(b"loaded")
|
||||||
|
msg = sub.recv()
|
||||||
|
print(msg)
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
except pynng.exceptions.Timeout:
|
||||||
|
logger.error(f"Socket timeout: {SOCKET_PATH}")
|
||||||
|
# Happens when the socket is locked
|
||||||
|
socket_error = True
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not socket_error:
|
||||||
app.on_startup(read_nng_messages)
|
app.on_startup(read_nng_messages)
|
||||||
|
|
||||||
app.on_exception(logger.debug)
|
app.on_exception(logger.debug)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ui.run(reload=False, show=True, port=native.find_open_port(), native=native_mode, favicon="icon/icon1.ico")
|
port = native.find_open_port()
|
||||||
|
#port = 8001
|
||||||
|
logger.debug(f"Run on port {port}")
|
||||||
|
#ui.run(reload=False, show=True, port=port, native=native_mode, favicon="icon/icon1.ico")
|
||||||
|
ui.run(reload=False, show=True, port=port, native=native_mode)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user