-
Notifications
You must be signed in to change notification settings - Fork 949
/
generate-spec.py
265 lines (230 loc) · 8.55 KB
/
generate-spec.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import argparse
import json
from operator import itemgetter
import pathlib
import sys
from traitlets import (
CaselessStrEnum,
Unicode,
Tuple,
List,
Bool,
CFloat,
Float,
CInt,
Int,
Instance,
Dict,
Bytes,
Any,
Union,
)
import ipywidgets as widgets
from ipywidgets import Color
from ipywidgets.widgets.trait_types import TypedTuple, ByteMemoryView
from ipywidgets.widgets.widget_link import Link
HEADER = '''# Model State
This is a description of the model state for each widget in the core Jupyter
widgets library. The model ID of a widget is the id of the comm object the
widget is using. A reference to a widget is serialized to JSON as a string of
the form `"IPY_MODEL_<MODEL_ID>"`, where `<MODEL_ID>` is the model ID of a
previously created widget of the specified type.
This model specification is for ipywidgets 8.
## Model attributes
Each widget in the Jupyter core widgets is represented below. The heading
represents the model name, module, and version, view name, module, and version
that the widget is registered with.
'''
NUMBER_MAP = {
'int': 'number (integer)',
'float': 'number (float)',
'bool': 'boolean',
'bytes': 'Bytes'
}
def trait_type(trait, widget_list):
attributes = {}
if isinstance(trait, CaselessStrEnum):
w_type = 'string'
attributes['enum'] = trait.values
elif isinstance(trait, Unicode):
w_type = 'string'
elif isinstance(trait, (Tuple, List)):
w_type = 'array'
elif isinstance(trait, TypedTuple):
w_type = 'array'
attributes['items'] = trait_type(trait._trait, widget_list)
elif isinstance(trait, Bool):
w_type = 'bool'
elif isinstance(trait, (CFloat, Float)):
w_type = 'float'
elif isinstance(trait, (CInt, Int)):
w_type = 'int'
elif isinstance(trait, Color):
w_type = 'color'
elif isinstance(trait, Dict):
w_type = 'object'
elif isinstance(trait, Union):
union_attributes = []
union_types = []
for ut in trait.trait_types:
ua = trait_type(ut, widget_list)
union_attributes.append(ua)
union_types.append(ua['type'])
w_type = union_types
attributes['union_attributes'] = union_attributes
elif isinstance(trait, (Bytes, ByteMemoryView)):
w_type = 'bytes'
elif isinstance(trait, Instance) and issubclass(trait.klass,
widgets.Widget):
w_type = 'reference'
attributes['widget'] = trait.klass.__name__
# ADD the widget to this documenting list
if (trait.klass not in [i[1] for i in widget_list]
and trait.klass is not widgets.Widget):
widget_list.append((trait.klass.__name__, trait.klass))
elif isinstance(trait, Any):
# In our case, these all happen to be values that are converted to
# strings
w_type = 'label'
else:
w_type = trait.__class__.__name__
attributes['type'] = w_type
if trait.allow_none:
attributes['allow_none'] = True
return attributes
def jsdefault(trait):
if isinstance(trait, Instance):
default = trait.make_dynamic_default()
if issubclass(trait.klass, widgets.Widget):
return 'reference to new instance'
else:
try:
# traitlets 5
default = trait.default()
except AttributeError:
# traitlets 4 - can be deleted when we depend only on traitlets 5
if isinstance(trait, Union):
default = trait.make_dynamic_default()
else:
default = trait.default_value
if isinstance(default, bytes) or isinstance(default, memoryview):
default = trait.default_value_repr()
return default
def mddefault(attribute):
default = attribute['default']
is_ref = isinstance(default, str) and default.startswith('reference')
if default is None:
default = 'null'
elif isinstance(default, bool):
default = str(default).lower()
elif not is_ref and attribute['type'] != 'bytes':
default = "{!r}".format(default)
if not is_ref:
default = '`{}`'.format(default)
return default
def mdtype(attribute):
md_type = attribute['type']
if 'union_attributes' in attribute and isinstance(md_type, (list, tuple)):
md_type = ' or '.join(
mdtype(ua) for ua in attribute['union_attributes']
)
if md_type in NUMBER_MAP:
md_type = NUMBER_MAP[md_type]
if attribute.get('allow_none'):
md_type = '`null` or {}'.format(md_type)
if 'enum' in attribute:
md_type = '{} (one of {})'.format(
md_type, ', '.join('`{!r}`'.format(n) for n in attribute['enum'])
)
if 'items' in attribute:
md_type = '{} of {}'.format(md_type, mdtype(attribute['items']))
if 'widget' in attribute:
md_type = '{} to {} widget'.format(md_type, attribute['widget'])
return md_type
def format_widget(widget):
out = []
fmt = '%(name)s (%(module)s, %(version)s)'
out.append('### %s; %s' % (fmt % widget['model'], fmt % widget['view']))
out.append('')
out.append('{name: <16} | {typing: <16} | {default: <16} | {help}'.format(
name='Attribute', typing='Type', default='Default', help='Help')
)
out.append('{0:-<16}-|-{0:-<16}-|-{0:-<16}-|----'.format('-'))
for attribute in sorted(widget['attributes'], key=itemgetter('name')):
s = '{name: <16} | {type: <16} | {default: <16} | {help}'.format(
name='`{}`'.format(attribute['name']),
default=mddefault(attribute),
type=mdtype(attribute),
help=attribute['help']
)
out.append(s)
out.append('')
return '\n'.join(out)
def jsonify(identifier, widget, widget_list):
model = dict(zip(['module', 'version', 'name'], identifier[:3]))
view = dict(zip(['module', 'version', 'name'], identifier[3:]))
attributes = []
for name, trait in widget.traits(sync=True).items():
if name == '_view_count':
# don't document this since it is totally experimental at this point
continue
attribute = dict(
name=name,
help=trait.help or '',
default=jsdefault(trait)
)
attribute.update(trait_type(trait, widget_list))
attributes.append(attribute)
return dict(model=model, view=view, attributes=attributes)
def create_spec(widget_list):
widget_data = []
for widget_name, widget_cls in widget_list:
if issubclass(widget_cls, Link):
widget = widget_cls((widgets.IntSlider(), 'value'),
(widgets.IntSlider(), 'value'))
elif issubclass(widget_cls, (widgets.SelectionRangeSlider,
widgets.SelectionSlider)):
widget = widget_cls(options=[1])
else:
widget = widget_cls()
widget_data.append(jsonify(widget_name, widget, widget_list))
return widget_data
def create_markdown(spec):
output = [HEADER]
for widget in spec:
output.append(format_widget(widget))
return '\n'.join(output)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Description of your program')
parser.add_argument('-f', '--format', choices=['json', 'json-pretty', 'markdown'],
help='Format to generate', default='json')
parser.add_argument('output', nargs='?', type=pathlib.Path)
args = parser.parse_args()
format = args.format
widgets_to_document = sorted(widgets.Widget._widget_types.items())
spec = create_spec(widgets_to_document)
if args.output:
args.output.parent.mkdir(exist_ok=True)
output = open(args.output, mode='w', encoding='utf8')
else:
output = sys.stdout
try:
if format == 'json':
try:
json.dump(spec, output, sort_keys=True)
except TypeError:
print('Encountered error when converting spec to JSON. Here is the spec:')
print(spec)
raise
elif format == 'json-pretty':
json.dump(spec, output, sort_keys=True,
indent=2, separators=(',', ': '))
elif format == 'markdown':
# We go through the json engine to convert tuples to lists, etc.
output.write(create_markdown(json.loads(json.dumps(spec))))
output.write('\n')
finally:
if args.output:
output.close()