diff --git a/nice.py b/nice.py index d273962..5efdfcf 100644 --- a/nice.py +++ b/nice.py @@ -34,6 +34,12 @@ from fastapi import Request from fastapi.responses import RedirectResponse from starlette.middleware.base import BaseHTTPMiddleware import typer +import logging + +import traceback + +# TODO fix ssl +VERIFY_SSL = False SOCKET_PATH = "tcp://localhost:9976" @@ -57,12 +63,14 @@ LOGGED_IN_USER = "None" REFRESH_TRIGGERED = True +RELOAD_ANON_FILES = False SHUTDOWN = False DELETE_FILES_ON_UPLOAD = True DEBUG = False +logging.getLogger('niceGUI').setLevel(logging.INFO) if DEBUG: BASE_SITE_URL = "http://localhost:8000" @@ -98,6 +106,7 @@ async def read_nng_messages(): ui.notify("Export done", color="positive") global REFRESH_TRIGGERED REFRESH_TRIGGERED = True + await sub.asend(b"done") logger.debug("loaded") case _: logger.error("invalid message") @@ -188,7 +197,11 @@ async def reload_anonymized_start(progress_bar, queue): ) progress_bar.visible = False - ui.notify(f"Files reloaded: {len(loaded_files)}, [Series: {len(loaded_series)}] ", color="positive") + 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") # load_series_view(loaded_series, loaded_series_data) loaded_series_ui.refresh() loaded_files_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) # with processed_files: # 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] = ( dataset.StudyDescription, - dataset.SeriesDescription, + series_description, str(dataset.SeriesInstanceUID), ) loaded_series[dataset.SeriesInstanceUID].append(output_file) loaded_series_data[dataset.SeriesInstanceUID] = ( dataset.StudyDescription, - dataset.SeriesDescription, + series_description ) q.put_nowait((n + 1) / to_process_len) @@ -333,12 +350,15 @@ def reload_anonymized(q: Queue): def clear_anonymized_files(): + global RELOAD_ANON_FILES for file in ANON_PATH.iterdir(): if file.is_file(): file.unlink() logger.debug("Deleted all files in ANON_PATH") + RELOAD_ANON_FILES = True + return @@ -380,6 +400,7 @@ def upload_files(q, rqst): f"{BASE_SITE_URL}/api/atlas/upload_dicom", # data=data, files=files, + verify=VERIFY_SSL ) logger.debug(f"Resp: {resp}") @@ -447,9 +468,11 @@ def login() -> Optional[RedirectResponse]: global rqst, LOGIN_SUCCESS, LOGGED_IN_USER rqst = requests.session() try: - rsp = rqst.get(LOGIN_URL) - except requests.exceptions.ConnectionError: + rsp = rqst.get(LOGIN_URL, verify=VERIFY_SSL) + except requests.exceptions.ConnectionError as e: ui.notify("Connection error!", color="negative") + logger.debug(e) + print(traceback.format_exc()) return token = ( @@ -474,6 +497,7 @@ def login() -> Optional[RedirectResponse]: data=data, # headers=header, # cookies=cookies + verify=VERIFY_SSL ) print(rsp.content) @@ -543,7 +567,9 @@ def main_page(): # typer_app() # Create a queue to communicate with the heavy computation process + logger.debug("PRE QUEUE") queue = Manager().Queue() + logger.debug("POST QUEUE") # Update the progress bar on the main process ui.timer( 0.1, @@ -551,6 +577,7 @@ def main_page(): queue.get() if not queue.empty() else progressbar.value ), ) + logger.debug("POST TImER") # Create a queue to communicate with the heavy computation process upload_queue = Manager().Queue() @@ -561,6 +588,7 @@ def main_page(): 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") @@ -622,11 +650,14 @@ def main_page(): ui.run_javascript("window.close()") async def watch_for_refresh(): - global REFRESH_TRIGGERED + global REFRESH_TRIGGERED, RELOAD_ANON_FILES while not app.is_stopped: if REFRESH_TRIGGERED: REFRESH_TRIGGERED = False 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) asyncio.create_task(watch_for_refresh()) @@ -651,32 +682,48 @@ def launch_app( work_dir: Path = typer.Option(WORK_DIR, help="Working directory"), nng: bool = typer.Option(True, help="Use nng"), 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 EXPORT_PATH = WORK_DIR / Path("export/") PROCESS_PATH = WORK_DIR / Path("to_process/") ANON_PATH = WORK_DIR / Path("anon/") + DEBUG = debug logger.debug(f"Work dir: {WORK_DIR}") if nng: + socket_error = False try: with pynng.Pair0(listen=SOCKET_PATH) as sub: pass except pynng.exceptions.AddressInUse: logger.debug("Address in use") - with pynng.Pair0(dial=SOCKET_PATH) as sub: - sub.send(b"loaded") - sys.exit(0) + try: + with pynng.Pair0(dial=SOCKET_PATH, send_timeout=4000, recv_timeout=4000) as sub: + sub.send(b"loaded") + msg = sub.recv() + print(msg) + sys.exit(0) + except pynng.exceptions.Timeout: + logger.error(f"Socket timeout: {SOCKET_PATH}") + # Happens when the socket is locked + socket_error = True + pass - app.on_startup(read_nng_messages) + if not socket_error: + app.on_startup(read_nng_messages) app.on_exception(logger.debug) 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: logger.error(e) pass