Skip to content

Commit d9d5e5d

Browse files
authored
Merge pull request #570 from bnmajor/warnings-and-errors
Warnings and errors
2 parents 75c18f2 + 1b0222d commit d9d5e5d

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

itkwidgets/integrations/environment.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from enum import Enum
2+
from importlib import import_module
23

34

45
class Env(Enum):
@@ -34,3 +35,28 @@ def find_env():
3435

3536

3637
ENVIRONMENT = find_env()
38+
print(f'ENVIRONMENT: {ENVIRONMENT}')
39+
40+
if ENVIRONMENT is not Env.COLAB:
41+
if ENVIRONMENT is Env.JUPYTER_NOTEBOOK:
42+
try:
43+
import imjoy_jupyter_extension
44+
except:
45+
raise RuntimeError('imjoy-jupyter-extension is required. `pip install itkwidgets[notebook]` and refresh page.')
46+
else:
47+
try:
48+
import_module("imjoy-jupyterlab-extension")
49+
except:
50+
if ENVIRONMENT is Env.JUPYTERLITE:
51+
print('imjoy-jupyterlab-extension is required')
52+
raise RuntimeError('imjoy-jupyterlab-extension is required. Install the package and refresh page.')
53+
else:
54+
raise RuntimeError('imjoy-jupyterlab-extension is required. `pip install itkwidgets[lab]` and refresh page.')
55+
56+
try:
57+
import imjoy_elfinder
58+
except:
59+
if ENVIRONMENT is Env.JUPYTERLITE:
60+
raise RuntimeError('imjoy-elfinder is required. Install the package and refresh page.')
61+
else:
62+
raise RuntimeError('imjoy-elfinder is required. `pip install imjoy-elfinder` and refresh page.')

itkwidgets/viewer.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,19 @@ class ViewerRPC:
2828
"""Viewer remote procedure interface."""
2929

3030
def __init__(
31-
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", **add_data_kwargs
31+
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", data=None
3232
):
3333
"""Create a viewer."""
3434
self._init_viewer_kwargs = dict(ui_collapsed=ui_collapsed, rotate=rotate, ui=ui)
35-
self._init_viewer_kwargs.update(**add_data_kwargs)
36-
self.init_data = {}
35+
self.init_data = data
3736
self.img = display(HTML(f'<div />'), display_id=str(uuid.uuid4()))
3837
self.wid = None
3938
if ENVIRONMENT is not Env.JUPYTERLITE:
4039
self.viewer_event = threading.Event()
4140
self.data_event = threading.Event()
4241

43-
def _get_input_data(self):
44-
input_options = ["data", "image", "label_image", "point_sets"]
45-
inputs = []
46-
for option in input_options:
47-
data = self._init_viewer_kwargs.get(option, None)
48-
if data is not None:
49-
inputs.append((option, data))
50-
return inputs
51-
52-
def setup(self):
53-
self.init_data.clear()
54-
input_data = self._get_input_data()
55-
result= None
56-
for (input_type, data) in input_data:
57-
render_type = _detect_render_type(data, input_type)
58-
key = init_key_aliases()[input_type]
59-
if render_type is RenderType.IMAGE:
60-
if input_type == 'label_image':
61-
result = _get_viewer_image(data, label=True)
62-
else:
63-
result = _get_viewer_image(data, label=False)
64-
elif render_type is RenderType.POINT_SET:
65-
result = _get_viewer_point_sets(data)
66-
if result is None:
67-
result = data
68-
self.init_data[key] = result
42+
async def setup(self):
43+
pass
6944

7045
async def run(self, ctx):
7146
"""ImJoy plugin setup function."""
@@ -157,9 +132,11 @@ class Viewer:
157132
def __init__(
158133
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", **add_data_kwargs
159134
):
135+
input_data = self.input_data(add_data_kwargs)
136+
data = self.init_data(input_data)
160137
"""Create a viewer."""
161138
self.viewer_rpc = ViewerRPC(
162-
ui_collapsed=ui_collapsed, rotate=rotate, ui=ui, **add_data_kwargs
139+
ui_collapsed=ui_collapsed, rotate=rotate, ui=ui, data=data
163140
)
164141
if ENVIRONMENT is not Env.JUPYTERLITE:
165142
self.bg_jobs = bg.BackgroundJobManager()
@@ -176,6 +153,33 @@ def loop(self):
176153
def has_viewer(self):
177154
return hasattr(self.viewer_rpc, 'itk_viewer')
178155

156+
def input_data(self, init_data_kwargs):
157+
input_options = ["data", "image", "label_image", "point_sets"]
158+
inputs = []
159+
for option in input_options:
160+
data = init_data_kwargs.get(option, None)
161+
if data is not None:
162+
inputs.append((option, data))
163+
return inputs
164+
165+
def init_data(self, input_data):
166+
_init_data = {}
167+
result= None
168+
for (input_type, data) in input_data:
169+
render_type = _detect_render_type(data, input_type)
170+
key = init_key_aliases()[input_type]
171+
if render_type is RenderType.IMAGE:
172+
if input_type == 'label_image':
173+
result = _get_viewer_image(data, label=True)
174+
else:
175+
result = _get_viewer_image(data, label=False)
176+
elif render_type is RenderType.POINT_SET:
177+
result = _get_viewer_point_sets(data)
178+
if result is None:
179+
raise RuntimeError(f"Could not process the viewer {input_type}")
180+
_init_data[key] = result
181+
return _init_data
182+
179183
async def run_queued_requests(self):
180184
def _run_queued_requests(queue):
181185
method_name, args, kwargs = queue.get()

0 commit comments

Comments
 (0)