|
3 | 3 | import json
|
4 | 4 | import math
|
5 | 5 | import numbers
|
6 |
| -import uuid |
7 |
| -from textwrap import TextWrapper, dedent |
| 6 | +from textwrap import TextWrapper |
8 | 7 |
|
9 | 8 | import numpy as np
|
10 | 9 | from asciitree import BoxStyle, LeftAligned
|
@@ -399,83 +398,43 @@ def get_text(self, node):
|
399 | 398 | return node.get_text()
|
400 | 399 |
|
401 | 400 |
|
402 |
| -def tree_html_sublist(node, root=False, expand=False): |
403 |
| - result = '' |
404 |
| - data_jstree = '{"type": "%s"}' % node.get_type() |
405 |
| - if root or (expand is True) or (isinstance(expand, int) and node.depth < expand): |
406 |
| - css_class = 'jstree-open' |
407 |
| - else: |
408 |
| - css_class = '' |
409 |
| - result += "<li data-jstree='{}' class='{}'>".format(data_jstree, css_class) |
410 |
| - result += '<span>{}</span>'.format(node.get_text()) |
411 |
| - children = node.get_children() |
412 |
| - if children: |
413 |
| - result += '<ul>' |
414 |
| - for c in children: |
415 |
| - result += tree_html_sublist(c, expand=expand) |
416 |
| - result += '</ul>' |
417 |
| - result += '</li>' |
418 |
| - return result |
| 401 | +tree_group_icon = 'folder' |
| 402 | +tree_array_icon = 'table' |
419 | 403 |
|
420 | 404 |
|
421 |
| -def tree_html(group, expand, level): |
| 405 | +def tree_get_icon(stype): |
| 406 | + if stype == "Array": |
| 407 | + return tree_array_icon |
| 408 | + elif stype == "Group": |
| 409 | + return tree_group_icon |
| 410 | + else: |
| 411 | + raise ValueError("Unknown type: %s" % stype) |
422 | 412 |
|
423 |
| - result = '' |
424 | 413 |
|
425 |
| - # include CSS for jstree default theme |
426 |
| - css_url = '//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/themes/default/style.min.css' |
427 |
| - result += '<link rel="stylesheet" href="{}"/>'.format(css_url) |
| 414 | +def tree_widget_sublist(node, root=False, expand=False): |
| 415 | + import ipytree |
428 | 416 |
|
429 |
| - # construct the tree as HTML nested lists |
430 |
| - node_id = uuid.uuid4() |
431 |
| - result += '<div id="{}" class="zarr-tree">'.format(node_id) |
432 |
| - result += '<ul>' |
433 |
| - root = TreeNode(group, level=level) |
434 |
| - result += tree_html_sublist(root, root=True, expand=expand) |
435 |
| - result += '</ul>' |
436 |
| - result += '</div>' |
437 |
| - |
438 |
| - # construct javascript |
439 |
| - result += dedent(""" |
440 |
| - <script> |
441 |
| - if (!require.defined('jquery')) { |
442 |
| - require.config({ |
443 |
| - paths: { |
444 |
| - jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min' |
445 |
| - }, |
446 |
| - }); |
447 |
| - } |
448 |
| - if (!require.defined('jstree')) { |
449 |
| - require.config({ |
450 |
| - paths: { |
451 |
| - jstree: '//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/jstree.min' |
452 |
| - }, |
453 |
| - }); |
454 |
| - } |
455 |
| - require(['jstree'], function() { |
456 |
| - $('#%s').jstree({ |
457 |
| - types: { |
458 |
| - Group: { |
459 |
| - icon: "%s" |
460 |
| - }, |
461 |
| - Array: { |
462 |
| - icon: "%s" |
463 |
| - } |
464 |
| - }, |
465 |
| - plugins: ["types"] |
466 |
| - }); |
467 |
| - }); |
468 |
| - </script> |
469 |
| - """ % (node_id, tree_group_icon, tree_array_icon)) |
| 417 | + result = ipytree.Node() |
| 418 | + result.icon = tree_get_icon(node.get_type()) |
| 419 | + if root or (expand is True) or (isinstance(expand, int) and node.depth < expand): |
| 420 | + result.opened = True |
| 421 | + else: |
| 422 | + result.opened = False |
| 423 | + result.name = node.get_text() |
| 424 | + result.nodes = [tree_widget_sublist(c, expand=expand) for c in node.get_children()] |
| 425 | + result.disabled = True |
470 | 426 |
|
471 | 427 | return result
|
472 | 428 |
|
473 | 429 |
|
474 |
| -tree_group_icon = 'fa fa-folder' |
475 |
| -tree_array_icon = 'fa fa-table' |
476 |
| -# alternatives... |
477 |
| -# tree_group_icon: 'jstree-folder' |
478 |
| -# tree_array_icon: 'jstree-file' |
| 430 | +def tree_widget(group, expand, level): |
| 431 | + import ipytree |
| 432 | + |
| 433 | + result = ipytree.Tree() |
| 434 | + root = TreeNode(group, level=level) |
| 435 | + result.add_node(tree_widget_sublist(root, root=True, expand=expand)) |
| 436 | + |
| 437 | + return result |
479 | 438 |
|
480 | 439 |
|
481 | 440 | class TreeViewer(object):
|
@@ -531,8 +490,10 @@ def __unicode__(self):
|
531 | 490 | def __repr__(self):
|
532 | 491 | return self.__unicode__()
|
533 | 492 |
|
534 |
| - def _repr_html_(self): |
535 |
| - return tree_html(self.group, expand=self.expand, level=self.level) |
| 493 | + def _ipython_display_(self): |
| 494 | + tree = tree_widget(self.group, expand=self.expand, level=self.level) |
| 495 | + tree._ipython_display_() |
| 496 | + return tree |
536 | 497 |
|
537 | 498 |
|
538 | 499 | def check_array_shape(param, array, shape):
|
|
0 commit comments