-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnbmeta-00-01__exploration.py
450 lines (366 loc) · 12.1 KB
/
nbmeta-00-01__exploration.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# coding: utf-8
# # nbmeta
# * Src: https://github.com/westurner/nbmeta
# In[ ]:
# In[1]:
get_ipython().system(u'pip install zope.interface dominate')
# In[7]:
"""store and IPython.display.display"""
from IPython.display import display, HTML
class DisplayConfig:
print_obj = True
def _display(obj, **kwargs):
print_obj = kwargs.get('print_obj', DisplayConfig.print_obj)
if print_obj: print(obj)
_obj = obj if hasattr(obj, '_repr_html_') else HTML(repr(obj))
return display(_obj)
class EmitConfig:
emit_writefn = staticmethod(_display)
# emit_writefn = staticmethod(print)
emit_allowoverwrite = False
_store = None
def get_store(store):
if store is None:
global _store
_store = OrderedDict() if _store is None else _store
store = _store
return store
def emit(key=None, obj=None, store=None, writefn=EmitConfig.emit_writefn):
store = get_store(store)
key = len(store) if key is None else key
if EmitConfig.emit_allowoverwrite:
if key in store:
raise KeyError((key, 'is already set the store. see emit_allowoverwrite'))
store[key] = obj
output = (key, obj)
writefn(output)
return output # return store
def test_emit():
emit(None, [10 % 12, 10 % 24])
emit(None, [12 % 10, 24 % 10])
emit(None, [divmod(10, 12), divmod(10, 24)])
emit(None, [divmod(12, 10), divmod(24, 10)])
#assert len(_store) == 4
#assert list(_store.keys()) == [0, 1, 2, 3]
# print(json.dumps(_store, indent=2))
# from pprint import pformat
# print(pformat(_store))
test_emit()
# In[103]:
import dominate
import dominate.util
tags = dominate.tags
div, h3, code, span, a = tags.div, tags.h3, tags.code, tags.span, tags.a
text, raw = dominate.util.text, dominate.util.raw
# In[]:
def generate_dominate_examples():
with div("this") as this:
this.add(div("that", id="that"))
div("that2", id="that")
with tags.ul():
[tags.li(n) for n in range(3)]
return this
html = generate_dominate_examples()
htmlstr = html.render()
# In[]:
from pygments import highlight
from pygments.lexers import HtmlLexer
from pygments.formatters import HtmlFormatter
def highlight_html(htmlstr):
"""Highlight as HTML pygments
Args:
htmlstr (str): HTML to highlight
Returns:
str: HTML string highlighted as HTML
"""
return highlight(
htmlstr,
HtmlLexer(),
HtmlFormatter(noclasses=True))
from zope.interface import Interface, Attribute, implements
class IReprHTML(Interface):
def _repr_html_(self):
"""Return HTML as a unicode string
Returns:
unicode: HTML string
"""
raise NotImplementedError()
class ReprHTMLConf:
print_html = False
print_highlight = False
class ReprHTML(object):
implements(IReprHTML)
def __init__(self, obj):
self.obj = obj
def __getattr__(self, attr):
if attr.startswith('_repr'):
if attr in ReprHTML:
return object.__getattribute__(self, attr)
return self.obj.__getattr__(self.obj, attr)
def _repr_html_(self, obj=None):
if obj is None:
obj = self.obj
if hasattr(obj, '_repr_html_'):
obj_html = obj._repr_html_()
if hasattr(obj, 'render'): # dominate
obj_html = obj.render()
#else:
# obj_html = obj
node = div([
div([h3('HTML'),
text(obj_html, escape=False),
#raw(obj_html)
]),
div([h3('HTML (source)'),
code(
text(obj_html, escape=True))]),
div([h3('HTML (highlighted)'),
text(
highlight_html(obj_html),
escape=False)])
])
node_html = node.render() # XXX: XSS
if ReprHTMLConf.print_html:
if ReprHTMLConf.print_highlight:
print(node_html)
else:
print(obj_html)
return node_html
#ReprHTMLConf.print_html = True
ReprHTML(htmlstr)
# In[104]:
import cgi
from collections import OrderedDict
class Meta(object):
def __init__(self, obj, meta=None, **kwargs):
self.obj = obj
if meta is None:
meta = OrderedDict()
meta.update(kwargs) # TODO
self.meta = meta
def __getattr__(self, attr):
if 1: # attr.startswith('_repr'):
if hasattr(Meta, attr):
return object.__getattribute__(self, attr)
return self.obj.__getattribute__(attr)
def to_html(self):
"""
Generate HTML by walking nested nodes
Returns:
str: HTML string
"""
typeof="http://schema.org/CreativeWork http://jupyter/ns#JupyterNotebook"
with div(typeof=typeof) as doc:
def metahtml(obj, cur_node):
with cur_node as doc:
with tags.ul() as ul_node:
if hasattr(obj, 'items'):
for key, value in obj.items():
with tags.li(property=key) as li_node:
li_node.add(a(key, href=key, property="rdf:Predicate"))
visit_node(value, key, li_node=li_node)
#elif hasattr(obj, '__iter__'):
# for value in obj:
# with tags.li() as li_node:
# #a(key, href=value)
# visit_node(value, li_node=li_node)
else:
#with tags.li() as li_node:
visit_node(obj, li_node=ul_node)
return cur_node
def visit_node(value, key=None, li_node=None):
if isinstance(value, basestring):
if value.startswith('http://'): # TODO: isinstance(URI)
a( span(text(value), property=key),
href=value,
property='rdf:Object')
else:
span(text(value), property=key)
elif hasattr(value, 'items'):
metahtml(value.items(), li_node)
#elif isinstance(value, Meta):
# metahtml(value.obj, li_node) # TODO: value.obj ->
elif hasattr(value, '__iter__'):
#elif isinstance(value, (list, tuple)):
with tags.ul() as _ul_node:
with tags.li() as _li_node:
# TODO: BUG: XXX
#raise Exception(value)
for _value in value:
#_items = OrderedDict(
# (__value, __value) for __value in _value).items()
metahtml(_value, _li_node) # TODO
elif hasattr(value, '_repr_html_'):
text(value._repr_html_(), escape=False)
#if isinstance(value, dominator.tag)
elif hasattr(value, 'render'):
text(value.render(), escape=False)
#TODO: markupsafe __html__ ("?)
else:
raise Exception((type(value), value))
doc_ = metahtml(self.meta, doc)
return doc.render()
def _repr_html_(self):
return self._objhtml_and_meta_html()
def _objhtml_and_meta_html(self):
obj = self.obj
# if hasattr(obj, '_repr_nbmeta_')
if hasattr(obj, '_repr_html_'):
obj_html = obj._repr_html_()
elif hasattr(obj, 'render'):
obj_html = obj.render()
else:
obj_html = str(obj) #cgi.escape(repr(obj))
assert type(obj_html) == unicode
meta_html = self.to_html()
assert type(meta_html) == unicode
return u'\n'.join((obj_html, meta_html))
ns = OrderedDict()
schema = ns['@context'] = {
"url": "http://schema.org/url",
"author": "http://schema.org/author",
"givenName": "http://schema.org/givenName",
"Person": "http://schema.org/Person"
}
rdf = ns['rdf'] = {"a": "rdf:type"}
# ns['@context']['rdf'] = # RDF_URI
# ns['@context']["a"] = "rdf:type"
ReprHTML(
Meta(this.render(),
meta=OrderedDict([
(schema['url'], "http://localhost:8888/notebooks/nb/...ipynb"),
(schema['author'], [
OrderedDict([
(rdf["a"], schema['Person']),
(schema['givenName'], "awesome")
# TODO: TST: list
]),
]),
])
)
)
# TODO
# RreprJSONLD() RDFa
# In[112]:
_jsonldstr = """
{"@context": {
"schema": "http://schema.org/",
"jupyter": "https://jupyter.org/ns/v4/#",
"_base": "http://localhost:8000/ns/v1#"
},
"@type": [
"jupyter:JupyterNotebook",
"schema:ScholarlyArticle",
"schema:DataCatalog"
],
"@id": "http://localhost:8888/notebooks/nb/nbmeta-00-01__exploration.ipynb",
"name": "Notebook Name",
"author": [{
"@type": "schema:Person",
"givenName": "Wesley",
"familyName": "Turner",
"url": "https://westurner.org/"
}],
"dateCreated": "2017-01-30",
"about": [
{"url": ["https://en.wikipedia.org/wiki/JSONLD"] },
{"url": "https://pypi.org/project/pipfile/", "name": "Pipfile and Pipfile.lock"}
]
}
"""
import json
import collections
from pygments import highlight
from pygments.lexers import JavascriptLexer
from pygments.formatters import HtmlFormatter
class CodeBlock(object):
def __init__(self, code, fmt='jsonld'):
self.code = code
self.fmt = fmt
def _repr_html_(self, *args, **kwargs):
return self.to_html(*args, **kwargs)
def to_html(self, code=None, fmt=None):
code = code if code is not None else self.code
fmt = fmt if fmt is not None else self.fmt
fmts = {
None: {'lexer': None, 'formatter': None},
'jsonld': {'lexer': JavascriptLexer,
'formatter': HtmlFormatter(noclasses=True)}}
_fmt = fmts.get(fmt)
return highlight(
code,
_fmt['lexer'](),
_fmt['formatter'])
def json_loads(obj, *args, **kwargs):
kwargs.setdefault('object_pairs_hook', collections.OrderedDict)
return json.loads(obj, *args, **kwargs)
def json_dumps(obj, *args, **kwargs):
kwargs.setdefault('indent', 2)
return json.dumps(obj, *args, **kwargs)
# In[]:
def create_jsonld_code_block(jsonldstr):
"""
Create a
"""
data = OrderedDict()
data['objs'] = json_loads(jsonldstr)
#ReprHTML(
m = Meta(
CodeBlock(jsonldstr, fmt='jsonld'),
jsonld=json_dumps(data['objs']))
print(m.meta['jsonld'])
m
create_jsonld_code_block(_jsonldstr)
# In[115]:
"""
Check these off when there are tests:
- [ ] Node
- [ ] ``@type=Union[URI, List[URI]]`` # JSONLD
- [ ] type()
- [ ] types() -> types_expanded()
- [ ] children=[]
- [ ] _repr_html_
- [ ] __repr__
- [ ] __unicode__
- [ ] __iter__
yield node; yield node.children
- [ ] Thing(Node)
- [ ] name
- [ ] description
- [ ] url
- [ ] CreativeWork(Thing)
- [ ] author
- [ ] ScholarlyArticle(CreativeWork)
- [ ] JupyterNotebook(CreativeWork, [ScholarlyArticle?] )
- [ ] CodeBlock(Node)
- [ ] Include(Node)('../README.rst')
- [ ] Figure(Node)(obj=plot, data=data, meta={author:, title:})
- [ ] Environment(
- [ ] SoftwareEnvironment "SE" ( https://schema.org/SoftwareApplication Software- )
- [ ] PipRequirements(SE)(txt='../requirements-2.txt'))
- [ ] PipFreeze(SE)(PipRequirements)
- [ ] Pipfile(SE)(pipfile='../Pipfile')
- [ ] PipfileLock(SE)(?='../Pipfile.lock')
- [ ] CondaEnvironment(yml='../environment.yml')
"""
# In[6]:
"""
this_notebook = TODO_GET_HERE()
base URI
XHTML
HTML5
https://schema.org/mainEntityOfPage
<>
"""
# In[131]:
''' [ ] how to set the cell metadata? '''
def inspect_gloals():
sorted(globals().keys())
ipy = get_ipython()
emit('ipy.displayhook', ipy.displayhook)
emit('ipy.filename', ipy.filename)
instance = ipy.instance()
emit('ipy.instance()', instance)
#globals()['celldata'][cell_n] = value
inspect_globals()
# In[ ]: