|
| 1 | +import remi.gui as gui |
| 2 | +from remi import start, App |
| 3 | +from remi_ext import TreeTable, SingleRowSelectionTable |
| 4 | + |
| 5 | +class MyApp(App): |
| 6 | + def __init__(self, *args): |
| 7 | + super(MyApp, self).__init__(*args) |
| 8 | + |
| 9 | + def main(self): |
| 10 | + self.wid = gui.VBox(style={'margin':'5px auto', 'padding': '10px'}) |
| 11 | + table = [('', '#ff9', 'cable', '1', '2', '3'), |
| 12 | + ('cable', '#ff9', '1-core cable', '1', '2', '3'), |
| 13 | + ('cable', '#ff9', 'multi core cable', '1', '2', '3'), |
| 14 | + ('multi core cable', '#ff9', '2-core cable', '1', '2', '3'), |
| 15 | + ('multi core cable', '#ff9', '3-core cable', '1', '2', '3'), |
| 16 | + ('3-core cable', '#ff9', '3-core armoured cable', '1', '2', '3'), |
| 17 | + ('cable', '#ff9', 'armoured cable', '1', '2', '3'), |
| 18 | + ('armoured cable', '#ff9', '3-core armoured cable', '1', '2', '3')] |
| 19 | + heads_color = '#dfd' |
| 20 | + uoms_color = '#ffd' |
| 21 | + heads = ['heads', heads_color, 'object', 'one', 'two', 'three'] |
| 22 | + uoms = ['uom', uoms_color, '', 'mm', 'cm', 'dm'] |
| 23 | + self.My_TreeTable(table, heads, heads2=uoms) |
| 24 | + return self.wid |
| 25 | + |
| 26 | + def My_TreeTable(self, table, heads, heads2=None): |
| 27 | + ''' Define and display a table |
| 28 | + in which the values in first column form one or more trees. |
| 29 | + ''' |
| 30 | + self.Define_TreeTable(heads, heads2) |
| 31 | + self.Display_TreeTable(table) |
| 32 | + |
| 33 | + def Define_TreeTable(self, heads, heads2=None): |
| 34 | + ''' Define a TreeTable with a heading row |
| 35 | + and optionally a second heading row. |
| 36 | + ''' |
| 37 | + display_heads = [] |
| 38 | + display_heads.append(tuple(heads[2:])) |
| 39 | + self.tree_table = TreeTable() |
| 40 | + self.tree_table.append_from_list(display_heads, fill_title=True) |
| 41 | + if heads2 is not None: |
| 42 | + heads2_color = heads2[1] |
| 43 | + row_widget = gui.TableRow() |
| 44 | + for index, field in enumerate(heads2[2:]): |
| 45 | + row_item = gui.TableItem(text=field, |
| 46 | + style={'background-color': heads2_color}) |
| 47 | + row_widget.append(row_item, field) |
| 48 | + self.tree_table.append(row_widget, heads2[0]) |
| 49 | + self.wid.append(self.tree_table) |
| 50 | + |
| 51 | + def Display_TreeTable(self, table): |
| 52 | + ''' Display a table in which the values in first column form one or more trees. |
| 53 | + The table has row with fields that are strings of identifiers/names. |
| 54 | + First convert each row into a row_widget and item_widgets |
| 55 | + that are displayed in a TableTree. |
| 56 | + Each input row shall start with a parent field (field[0]) |
| 57 | + that determines the tree hierarchy but that is not displayed on that row. |
| 58 | + The parent widget becomes an attribute of the first child widget. |
| 59 | + Field[1] is the row color, field[2:] contains the row values. |
| 60 | + Top child(s) shall have a parent field value that is blank (''). |
| 61 | + The input table rows shall be in the correct sequence. |
| 62 | + ''' |
| 63 | + parent_names = [] |
| 64 | + hierarchy = {} |
| 65 | + indent_level = 0 |
| 66 | + widget_dict = {} # key, value = name, widget |
| 67 | + for row in table: |
| 68 | + parent_name = row[0] |
| 69 | + row_color = row[1] |
| 70 | + child_name = row[2] |
| 71 | + row_widget = gui.TableRow(style={'background-color': row_color}) |
| 72 | + # Determine whether hierarchy of sub_sub concepts shall be open or not |
| 73 | + openness = 'true' |
| 74 | + row_widget.attributes['treeopen'] = openness |
| 75 | + # widget_dict[child_name] = row_widget |
| 76 | + for index, field in enumerate(row[2:]): |
| 77 | + # Determine field color |
| 78 | + field_color = '#ffff99' |
| 79 | + row_item = gui.TableItem(text=field, |
| 80 | + style={'text-align': 'left', |
| 81 | + 'background-color': field_color}) |
| 82 | + row_widget.append(row_item, field) |
| 83 | + if index == 0: |
| 84 | + row_item.parent = parent_name |
| 85 | + child_id = row_item |
| 86 | + |
| 87 | + # The root of each tree has a parent that is blank (''). |
| 88 | + # Each row with childs has as attribute openness, which by default is 'true'. |
| 89 | + # The fields can be given other attributes, such as color. |
| 90 | + |
| 91 | + # Verify whether the parent_name (child.parent) |
| 92 | + # is present or is in the list of parent_names. |
| 93 | + print('parent-child:', parent_name, child_name) |
| 94 | + if parent_name == '': |
| 95 | + hierarchy[child_name] = 0 |
| 96 | + parent_names.append(child_name) |
| 97 | + target_level = 0 |
| 98 | + elif parent_name in parent_names: |
| 99 | + hierarchy[child_name] = hierarchy[parent_name] + 1 |
| 100 | + target_level = hierarchy[child_name] |
| 101 | + else: |
| 102 | + # Parent not in parent_names |
| 103 | + print('Error: Parent name "{}" does not appear in network' |
| 104 | + .format(parent_name)) |
| 105 | + return |
| 106 | + print('indent, target-pre:', indent_level, target_level, |
| 107 | + parent_name, child_name) |
| 108 | + # Indentation |
| 109 | + if target_level > indent_level: |
| 110 | + self.tree_table.begin_fold() |
| 111 | + indent_level += 1 |
| 112 | + if target_level < indent_level: |
| 113 | + while target_level < indent_level: |
| 114 | + indent_level += -1 |
| 115 | + self.tree_table.end_fold() |
| 116 | + print('indent, target-post:', indent_level, target_level, |
| 117 | + parent_name, child_name) |
| 118 | + if child_name not in parent_names: |
| 119 | + parent_names.append(child_name) |
| 120 | + self.tree_table.append(row_widget, child_name) |
| 121 | + |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + # starts the webserver |
| 125 | + # optional parameters |
| 126 | + start(MyApp,address='127.0.0.1', port=8081, multiple_instance=False, |
| 127 | + enable_file_cache=True, update_interval=0.1, start_browser=True) |
| 128 | + # start(MyApp, debug=True, address='0.0.0.0', port=0 ) |
0 commit comments