diff --git a/qim3d/cli/__init__.py b/qim3d/cli/__init__.py index ef60bdb89d58107a08a7e12a48aa7b42b9654e25..cec79d579f1cc01809c84efef10f97a42df74a7f 100644 --- a/qim3d/cli/__init__.py +++ b/qim3d/cli/__init__.py @@ -159,20 +159,12 @@ def main(): elif args.subcommand == "viz": if args.method == "itk-vtk": - try: - # We need the full path to the file for the viewer - current_dir = os.getcwd() - full_path = os.path.normpath(os.path.join(current_dir, args.source)) - qim3d.viz.itk_vtk(full_path, open_browser=not args.no_browser) - - except qim3d.viz.NotInstalledError as err: - print(err) - message = "Itk-vtk-viewer is not installed or qim3d can not find it.\nYou can either:\n\to Use 'qim3d viz SOURCE -m k3d' to display data using different method\n\to Install itk-vtk-viewer yourself following https://kitware.github.io/itk-vtk-viewer/docs/cli.html#Installation\n\to Let qim3D install itk-vtk-viewer now (it will also install node.js in qim3d library)\nDo you want qim3D to install itk-vtk-viewer now?" - print(message) - answer = input("[Y/n]:") - if answer in "Yy": - qim3d.viz.Installer().install() - qim3d.viz.itk_vtk(full_path) + + # We need the full path to the file for the viewer + current_dir = os.getcwd() + full_path = os.path.normpath(os.path.join(current_dir, args.source)) + qim3d.viz.itk_vtk(full_path, open_browser = not args.no_browser) + elif args.method == "k3d": volume = qim3d.io.load(str(args.source)) diff --git a/qim3d/viz/__init__.py b/qim3d/viz/__init__.py index bc65ee16184325418e881e7c2303061a37cc60c9..7f21d7b9046e7337f88e9c54eb81ff1abce0db2d 100644 --- a/qim3d/viz/__init__.py +++ b/qim3d/viz/__init__.py @@ -9,7 +9,7 @@ from ._data_exploration import ( chunks, histogram, ) -from .itk_vtk_viewer import itk_vtk, Installer, NotInstalledError +from .itk_vtk_viewer import itk_vtk from ._k3d import volumetric, mesh from ._local_thickness import local_thickness from ._structure_tensor import vectors diff --git a/qim3d/viz/itk_vtk_viewer/__init__.py b/qim3d/viz/itk_vtk_viewer/__init__.py index 87678ceee22f43cf8673a4a8cfaf6510014a1726..ac88d69704f79750238f48e86da3bc0f806be25a 100644 --- a/qim3d/viz/itk_vtk_viewer/__init__.py +++ b/qim3d/viz/itk_vtk_viewer/__init__.py @@ -1,3 +1 @@ -from .installation import Installer from .run import itk_vtk -from .helpers import NotInstalledError \ No newline at end of file diff --git a/qim3d/viz/itk_vtk_viewer/run.py b/qim3d/viz/itk_vtk_viewer/run.py index 8fa5e6b8edc85573ccbb78e19c7838c08a8d6d23..45554428509423772f4dc8ec089eb591cf869a43 100644 --- a/qim3d/viz/itk_vtk_viewer/run.py +++ b/qim3d/viz/itk_vtk_viewer/run.py @@ -1,15 +1,16 @@ import subprocess -import platform from pathlib import Path import os +import webbrowser +import threading +import time + import qim3d.utils from qim3d.utils._logger import log -# from .helpers import get_qim_dir, get_nvm_dir, get_viewer_binaries, get_viewer_dir, get_node_binaries_dir, NotInstalledError, SOURCE_FNM from .helpers import * -import webbrowser -import threading -import time +from .installation import Installer + # Start viewer @@ -73,7 +74,7 @@ def run_within_qim_dir(port=3000): ) -def itk_vtk( +def try_opening_itk_vtk( filename: str = None, open_browser: bool = True, file_server_port: int = 8042, @@ -120,8 +121,8 @@ def itk_vtk( 1.85GB [00:17, 111MB/s] Loading Okinawa_Foram_1.tif - Loading: 100% -  1.85GB/1.85GB  [00:02<00:00, 762MB/s] + Loading: 100% + 1.85GB/1.85GB [00:02<00:00, 762MB/s] Loaded shape: (995, 1014, 984) Using virtual stack Exporting data to OME-Zarr format at Okinawa_Foram_1.zarr @@ -207,3 +208,34 @@ def itk_vtk( # If we still get an error, it is not installed in location we expect it to be installed and have to raise an error # which will be caught in the command line and it will ask for installation raise NotInstalledError + + +def itk_vtk( + filename: str = None, + open_browser: bool = True, + file_server_port: int = 8042, + viewer_port: int = 3000 + ): + """ + Command to run in cli/__init__.py. Tries to run the vizualization, + if that fails, asks the user to install it. This function is needed + here so we don't have to import NotInstalledError and Installer, + which exposes these to user. + """ + + try: + try_opening_itk_vtk(filename, + open_browser=open_browser, + file_server_port = file_server_port, + viewer_port = viewer_port) + + except NotInstalledError: + message = "Itk-vtk-viewer is not installed or qim3d can not find it.\nYou can either:\n\to Use 'qim3d viz SOURCE -m k3d' to display data using different method\n\to Install itk-vtk-viewer yourself following https://kitware.github.io/itk-vtk-viewer/docs/cli.html#Installation\n\to Let qim3D install itk-vtk-viewer now (it will also install node.js in qim3d library)\nDo you want qim3D to install itk-vtk-viewer now?" + print(message) + answer = input("[Y/n]:") + if answer in "Yy": + Installer().install() + try_opening_itk_vtk(filename, + open_browser=open_browser, + file_server_port = file_server_port, + viewer_port = viewer_port) \ No newline at end of file