29
29
from .xmldocs import listdoc # noqa
30
30
from functools import partial
31
31
32
- try :
33
- import IPython .display
34
- HAVE_IPY = True
35
- try :
36
- import sidecar
37
- HAVE_SIDECAR = True
38
- except Exception :
39
- HAVE_SIDECAR = False
32
+
33
+ # Will attempt to import module, returns module and true if successful
34
+ def try_import_module (module , sub_modules = None ):
40
35
try :
41
- import ipywidgets
42
- HAVE_IPYWIDGETS = True
43
- except Exception : # no widgets
44
- HAVE_IPYWIDGETS = False
45
- except Exception : # no IPython
46
- HAVE_IPY = False
47
- HAVE_SIDECAR = False
48
- HAVE_IPYWIDGETS = False
36
+ return __import__ (module , fromlist = sub_modules ), True
37
+ except ImportError :
38
+ return None , False
39
+
40
+
41
+ # Save whether modules were successfully imported
42
+ IPython , HAVE_IPY = try_import_module ("IPython" , ["display" ])
43
+ ipywidgets , HAVE_IPYWIDGETS = try_import_module ("ipywidgets" )
44
+ sidecar , HAVE_SIDECAR = try_import_module ("sidecar" )
49
45
50
46
51
47
try :
@@ -59,7 +55,7 @@ def get_update_array_kw(disp, array, widgets, debug_target=None):
59
55
return {}
60
56
if debug_target is not None :
61
57
with debug_target :
62
- print ("ok looking at kewwords for" , array .id )
58
+ print ("ok looking at keywords for" , array .id )
63
59
kw = {}
64
60
for ax in array .getAxisList ():
65
61
if debug_target is not None :
@@ -179,24 +175,29 @@ def handle_slider_change(self, change, widgets, name):
179
175
debug_target = self ._parent ._display_target_out
180
176
else :
181
177
debug_target = None
182
- kw1 = get_update_array_kw (disp , disp .array [0 ], widgets , debug_target )
183
- kw2 = get_update_array_kw (disp , disp .array [1 ], widgets , debug_target )
178
+ kw1 = get_update_array_kw (
179
+ disp , disp .array [0 ], widgets , debug_target )
180
+ kw2 = get_update_array_kw (
181
+ disp , disp .array [1 ], widgets , debug_target )
184
182
# Ok in some case (u/v e.g) same dims but different name on 2nd array
185
183
if disp .array [1 ] is not None :
186
184
for axId in kw1 :
187
185
if axId not in kw2 : # probably should be there as wll
188
- ax = disp .array [0 ].getAxis (disp .array [0 ].getAxisIndex (axId ))
186
+ ax = disp .array [0 ].getAxis (
187
+ disp .array [0 ].getAxisIndex (axId ))
189
188
if debug :
190
189
with self ._parent ._display_target_out :
191
- print ("Examing axis:" , axId , "vs" , ax , hasattr (ax , "axis" ))
190
+ print ("Examing axis:" , axId , "vs" ,
191
+ ax , hasattr (ax , "axis" ))
192
192
if hasattr (ax , "axis" ): # special dim (T,Z,Y,X)
193
193
if debug :
194
194
with self ._parent ._display_target_out :
195
195
print ("Examing axis:" , axId , "vs" , ax .axis )
196
196
for ax2 in disp .array [1 ].getAxisList ():
197
197
if debug :
198
198
with self ._parent ._display_target_out :
199
- print ("Examing axis:" , axId , "vs" , ax2 .id )
199
+ print ("Examing axis:" ,
200
+ axId , "vs" , ax2 .id )
200
201
if hasattr (ax2 , "axis" ) and ax2 .axis == ax .axis :
201
202
kw2 [ax2 .id ] = kw1 [ax .id ]
202
203
if debug :
@@ -235,7 +236,8 @@ def handle_slider_change(self, change, widgets, name):
235
236
sp = slider .description
236
237
if debug :
237
238
with self ._parent ._display_target_out :
238
- print ("OPk looking at:" , name , slider .description , sp , sp == name )
239
+ print ("OPk looking at:" , name ,
240
+ slider .description , sp , sp == name )
239
241
if sp == name :
240
242
value = label .values [change ["new" ]]
241
243
label .value = "{}" .format (value )
@@ -264,7 +266,8 @@ def generate_sliders(self, debug):
264
266
widgets = []
265
267
for disp_name in self ._parent .display_names :
266
268
disp = vcs .elements ["display" ][disp_name ]
267
- gm_info = vcs .graphicsmethodinfo (vcs .getgraphicsmethod (disp .g_type , disp .g_name ))
269
+ gm_info = vcs .graphicsmethodinfo (
270
+ vcs .getgraphicsmethod (disp .g_type , disp .g_name ))
268
271
data = disp .array [0 ]
269
272
if data is None :
270
273
continue
@@ -274,7 +277,8 @@ def generate_sliders(self, debug):
274
277
if dim .isTime ():
275
278
values = dim .asComponentTime ()
276
279
else :
277
- values = ["{}{}" .format (value , units ) for value in dim [:]]
280
+ values = ["{}{}" .format (value , units )
281
+ for value in dim [:]]
278
282
slider = ipywidgets .IntSlider (
279
283
value = 0 ,
280
284
min = 0 ,
@@ -291,7 +295,8 @@ def generate_sliders(self, debug):
291
295
label .values = values
292
296
box = ipywidgets .HBox ([slider , label ])
293
297
widgets .append (box )
294
- funcs .append (partial (self .handle_slider_change , name = dim .id ))
298
+ funcs .append (
299
+ partial (self .handle_slider_change , name = dim .id ))
295
300
dimensions .add ((dim .id , units , dim [0 ], dim [- 1 ]))
296
301
for i , wdgt in enumerate (widgets ):
297
302
slider = wdgt .children [0 ]
@@ -320,11 +325,13 @@ def _repr_png_(self):
320
325
self ._parent ._display_target_image = ipywidgets .Image ()
321
326
if HAVE_IPYWIDGETS :
322
327
if debug :
323
- self ._parent ._display_target_out = ipywidgets .Output (layout = {'border' : '1px solid black' })
328
+ self ._parent ._display_target_out = ipywidgets .Output (
329
+ layout = {'border' : '1px solid black' })
324
330
else :
325
331
self ._parent ._display_target_out = None
326
332
widgets = self .generate_sliders (debug )
327
- vbox = ipywidgets .VBox (widgets + [self ._parent ._display_target_image ])
333
+ vbox = ipywidgets .VBox (
334
+ widgets + [self ._parent ._display_target_image ])
328
335
if HAVE_SIDECAR and sidecar_on :
329
336
with self ._parent ._display_target :
330
337
IPython .display .clear_output ()
@@ -482,19 +489,19 @@ def _set_backend(self, value):
482
489
"dictionary of things the backend wants to be able to reuse" )
483
490
484
491
##########################################################################
485
- # #
486
- # Initialize the display plot attributes. #
487
- # #
492
+ # #
493
+ # Initialize the display plot attributes. #
494
+ # #
488
495
##########################################################################
489
496
def __init__ (self , Dp_name , Dp_name_src = 'default' , parent = None ):
490
- # #
497
+ # #
491
498
###################################################################
492
- # Initialize the display plot's class and its members #
493
- # The getDpmember function retrieves the values of the #
494
- # display plot members in the C structure and passes back the #
495
- # appropriate Python Object. #
499
+ # Initialize the display plot's class and its members #
500
+ # The getDpmember function retrieves the values of the #
501
+ # display plot members in the C structure and passes back the #
502
+ # appropriate Python Object. #
496
503
###################################################################
497
- # #
504
+ # #
498
505
self .extradisplays = []
499
506
self ._name = Dp_name
500
507
self .s_name = 'Dp'
@@ -526,9 +533,9 @@ def __init__(self, Dp_name, Dp_name_src='default', parent=None):
526
533
527
534
vcs .elements ["display" ][self ._name ] = self
528
535
##########################################################################
529
- # #
530
- # List out display plot members (attributes). #
531
- # #
536
+ # #
537
+ # List out display plot members (attributes). #
538
+ # #
532
539
##########################################################################
533
540
534
541
def list (self ):
0 commit comments