/
jupyter.py
192 lines (157 loc) · 6.75 KB
/
jupyter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# This file contains the defintion of the Python part of the WWT Jupyter
# widget. Note that we don't tag each trait from BaseWWTWidget as sync=True
# because we instead use JSON messages to transmit any changes between the
# Python and Javascript parts so that we can re-use this for the Qt client.
import ipywidgets as widgets
from traitlets import Unicode, Float, default, link, directional_link
from ipyevents import Event as DOMListener
from .core import BaseWWTWidget
from .layers import ImageLayer
from .jupyter_server import serve_file
from . import __version__
__all__ = ['WWTJupyterWidget']
_npm_version = (__version__.replace('.alpha', '-alpha.')
.replace('.beta', '-beta.')
.replace('.dev', '-dev.')
.replace('.rc', '-rc.'))
VIEW_MODULE_VERSION = _npm_version
MODEL_MODULE_VERSION = _npm_version
dom_listener = DOMListener()
@widgets.register
class WWTJupyterWidget(widgets.DOMWidget, BaseWWTWidget):
"""
An AAS WorldWide Telescope Jupyter widget.
"""
_view_name = Unicode('WWTView').tag(sync=True)
_model_name = Unicode('WWTModel').tag(sync=True)
_view_module = Unicode('pywwt').tag(sync=True)
_model_module = Unicode('pywwt').tag(sync=True)
_view_module_version = Unicode(VIEW_MODULE_VERSION).tag(sync=True)
_model_module_version = Unicode(MODEL_MODULE_VERSION).tag(sync=True)
_ra = Float(0.0).tag(sync=True, wwt=None)
_dec = Float(0.0).tag(sync=True, wwt=None)
_fov = Float(60.0).tag(sync=True, wwt=None)
_datetime = Unicode('2017-03-09T12:30:00').tag(sync=True, wwt=None)
# wwt=None tag needed to avoid linkage to 'wwt.settings.set_' type traits
# (see _on_trait_change() in core.py)
def __init__(self):
widgets.DOMWidget.__init__(self)
BaseWWTWidget.__init__(self)
dom_listener.source = self
dom_listener.prevent_default_action = True
dom_listener.watched_events = ['wheel']
self._controls = None
@default('layout')
def _default_layout(self):
return widgets.Layout(height='400px', align_self='stretch')
def _send_msg(self, **kwargs):
self.send(kwargs)
def _serve_file(self, filename, extension=''):
return serve_file(filename, extension=extension)
def _get_view_data(self, field):
if field == 'ra':
return self._ra
elif field == 'dec':
return self._dec
elif field == 'fov':
return self._fov
elif field == 'datetime':
return self._datetime
else:
raise ValueError("'field' should be one of: 'ra', 'dec', "
"'fov', or 'datetime'")
def _create_image_layer(self, **kwargs):
"""Returns a specialized subclass of ImageLayer that has some extra hooks for
creating UI control points.
"""
return JupyterImageLayer(parent=self, **kwargs)
@property
def layer_controls(self):
if self._controls is None:
opacity_slider = widgets.FloatSlider(value=self.foreground_opacity,
min=0, max=1, readout=False)
foreground_menu = widgets.Dropdown(options=self.available_layers,
value=self.foreground)
background_menu = widgets.Dropdown(options=self.available_layers,
value=self.background)
link((opacity_slider, 'value'), (self, 'foreground_opacity'))
link((foreground_menu, 'value'), (self, 'foreground'))
link((background_menu, 'value'), (self, 'background'))
self._controls = widgets.HBox([background_menu, opacity_slider, foreground_menu])
return self._controls
class JupyterImageLayer(ImageLayer):
def __init__(self, **kwargs):
self._controls = None
super(JupyterImageLayer, self).__init__(**kwargs)
@property
def controls(self):
from .layers import VALID_STRETCHES, UI_COLORMAPS
if self._controls is not None:
return self._controls
opacity = widgets.FloatSlider(
description='Opacity:',
value=self.opacity,
min=0,
max=1,
readout=False,
step=0.01,
layout={'width': '200px'}
)
link((self, 'opacity'), (opacity, 'value'))
stretch = widgets.Dropdown(
description='Stretch:',
options=VALID_STRETCHES,
value=self.stretch,
layout={'width': '200px'}
)
link((self, 'stretch'), (stretch, 'value'))
# NB, this will crash if `self.cmap` is not one of our allowed values
reverse_ui_colormaps = dict((kv[1], kv[0]) for kv in UI_COLORMAPS.items())
colormap = widgets.Dropdown(
description='Colormap:',
options=UI_COLORMAPS.keys(),
value=reverse_ui_colormaps[self.cmap.name],
layout={'width': '200px'}
)
directional_link((colormap, 'label'), (self, 'cmap'), lambda x: UI_COLORMAPS[x])
directional_link((self, 'cmap'), (colormap, 'label'), lambda x: reverse_ui_colormaps[x.name])
vrange = widgets.FloatRangeSlider(
description='Fine min/max:',
value=[self.vmin, self.vmax],
min=self._data_min,
max=self._data_max,
readout=True,
layout={'width': '600px'},
step=(self.vmax - self.vmin) / 100,
format='.3g'
)
# Linkage must be manual since vrange uses a pair of values whereas we
# have two separate traitlets.
vrange.observe(self._vrange_slider_updated, names=['value'])
def update_vrange(change):
# Note: when this function is called, these values are indeed updated.
vrange.value = (self.vmin, self.vmax)
self.observe(update_vrange, names=['vmin', 'vmax'])
def update_step(change):
vrange.step = (vrange.max - vrange.min) / 100
vrange.observe(update_step, names=['min', 'max'])
coarse_min = widgets.FloatText(
description='Coarse min:',
value=self._data_min,
layout={'width': '300px'}
)
link((coarse_min, 'value'), (vrange, 'min'))
coarse_max = widgets.FloatText(
description='Coarse max:',
value=self._data_max,
layout={'width': '300px'}
)
link((coarse_max, 'value'), (vrange, 'max'))
self._controls = widgets.VBox([
widgets.HBox([colormap, stretch, opacity]),
widgets.HBox([coarse_min, coarse_max]),
vrange,
])
return self._controls
def _vrange_slider_updated(self, change):
self.vmin, self.vmax = change['new']