-
Notifications
You must be signed in to change notification settings - Fork 240
/
options_widget.py
188 lines (164 loc) · 5.94 KB
/
options_widget.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
"""
A widget-based representation of OptionsDictionary for use in Jupyter notebooks.
"""
try:
import ipywidgets as widgets
from ipywidgets import DOMWidget, register
from ipywidgets import interact, Layout
from IPython.display import display
except Exception:
widgets = None
from openmdao.utils.options_dictionary import OptionsDictionary
from openmdao.utils.general_utils import issue_warning
class OptionsWidget(object):
"""
Widget to set options.
Parameters
----------
opts : OptionsDictionary
Options to edit.
"""
def __init__(self, opts):
"""
Initialize.
"""
if widgets is None:
issue_warning(f"ipywidgets is required to use {self.__class__.__name__}."
"To install it run `pip install openmdao[notebooks]`.")
return
_dict = opts._dict
_widgets = []
_style = {'description_width': 'initial', 'align-items': 'center'}
messages = widgets.Output()
@messages.capture(clear_output=True)
def option_changed(change):
owner = change['owner']
newval = change['new']
name = owner.description
option = _dict[name]
# if it's an arbitrary list, parse lines of text
if option['types'] is list and option['values'] is None:
newval = newval.strip().split('\n')
try:
option['val'] = newval
except ValueError as err:
print(str(err))
for name, option in sorted(_dict.items()):
val = option['val']
types = option['types']
values = option['values']
desc = option['desc']
if values:
if types is list:
_widgets.append(widgets.SelectMultiple(
description=name,
tooltip=desc,
options=sorted(values),
value=val,
disabled=False,
style=_style
))
continue
else:
_widgets.append(widgets.Dropdown(
description=name,
tooltip=desc,
options=values,
value=val,
disabled=False,
style=_style
))
continue
upper = option['upper']
lower = option['lower']
if upper and lower:
if isinstance(val, int):
_widgets.append(widgets.IntSlider(
description=name,
tooltip=desc,
min=lower,
max=upper,
value=val,
step=1,
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d',
style=_style
))
else:
_widgets.append(widgets.FloatSlider(
description=name,
tooltip=desc,
min=lower,
max=upper,
value=val,
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='f',
style=_style
))
continue
if isinstance(val, float):
_widgets.append(widgets.FloatText(
description=name,
tooltip=desc,
min=lower,
max=upper,
value=val,
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='f',
style=_style
))
continue
if isinstance(val, int):
_widgets.append(widgets.IntText(
description=name,
tooltip=desc,
min=lower,
max=upper,
value=val,
step=1,
disabled=False,
continuous_update=False,
orientation='horizontal',
readout=True,
readout_format='d',
style=_style
))
continue
types = option['types']
if types == list:
_widgets.append(widgets.Textarea(
description=name,
tooltip=desc,
value='\n'.join(val),
continuous_update=False,
rows=5,
disabled=False,
style=_style
))
continue
# unhandled option type, just show value as uneditable text
_widgets.append(widgets.Textarea(
description=name,
tooltip=desc,
value=str(val),
disabled=True,
style=_style
))
for wdgt in _widgets:
wdgt.observe(option_changed, 'value')
# sort widgets by how many rows they use
_wdgt_rows = [(wdgt.rows if getattr(wdgt, 'rows', None) else 1, wdgt) for wdgt in _widgets]
_wdgt_rows.sort(key=lambda x: x[0])
_widgets = [wdgt for _, wdgt in _wdgt_rows]
box_layout = Layout(display='flex', flex_flow='row wrap')
display(widgets.GridBox(children=_widgets, layout=box_layout))
display(messages)