-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_browswer.py
212 lines (188 loc) · 8.04 KB
/
json_browswer.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
from six import string_types
import numbers
from collections import abc
import ipywidgets as widgets
# auxiliary functiosn for the json browser.
def extract_values_from_json_object(data):
"""
Checks the type of data and returns a set of list of keys (if any) or the string value.
Only supports, dict and lists or collectors.
"""
if not isinstance(data, dict):
return None
values = {}
for key, potential_value in data.items():
if isinstance(potential_value, (string_types, numbers.Real)):
values[key] = potential_value
return values
def process_data(data):
"""
Checks the type of data and returns a set of list of keys (if any) or the string value.
Only supports, dict and lists or collectors.
"""
if data is None:
return (None, None, None)
elif isinstance(data, dict):
keys = list(data.keys())
values = extract_values_from_json_object(data)
return (list(keys), data, values)
elif isinstance(data, string_types):
return (None, str(data), None)
elif isinstance(data, abc.Iterable):
processed_data = {}
for value_n, value in enumerate(data):
key = "{}: '{}'".format(value_n, str(value)[0:40])
processed_data[key] = value
return (list(processed_data.keys()), processed_data, None)
return (None, str(data), None)
def json_browser(input_data):
"""
The JSON browser receives a hierarchical dictionary and
allows an user to navigate it similarly to the columns view of files in Finder of Mac.
The idea of the widget is let users navigate a json-type dict object,
by going deeper in the hierarchies.
For each selected level, the user can navigate further the elements.
If the level has any element with a value, it shows them in its own box.
"""
# Defining overall layout objects.
height_selection_box = 300
smal_box_height = height_selection_box * 0.94
general_framework_layoud = widgets.Layout(
overflow_x="scroll",
# overflow_y='scroll',
# border='3px solid black',
# height='',
flex_direction="row",
display="flex",
width="900px",
height="{}px".format(height_selection_box),
)
small_box_layout = widgets.Layout(
overflow_x=None,
# overflow_y='scroll',
# border='3px solid black',
# height='',
# flex_direction='row',
# display='flex')
width="300px",
min_width="300px",
min_height="{}px".format(smal_box_height),
height="{}px".format(smal_box_height),
)
divided_box_layout = widgets.Layout(
overflow_x=None,
# overflow_y='scroll',
# border='3px solid black',
# height='',
# flex_direction='row',
# display='flex')
width="300px",
min_width="300px",
height="{}px".format(int(smal_box_height * 0.49)),
)
# Let us define the main compound widget box.
main_box = widgets.HBox(layout=general_framework_layoud)
# each hierarchy is shown in a selection box. We need to keep track of the information that
# each box stores. We define all that information here
widget_to_data = {}
keys, processed_data, these_values = process_data(input_data)
select_to_parent = {}
# I would normally use Select, but ipywidgets 6.0 uses a list instead of a box
# this will be fix later, but the 7.0 had other problems when I tested it.
select_widget = widgets.SelectMultiple(
# description='',
options=[None] + list(keys), # ordered_keys,
rows=10,
# options=['Linux\ndf', 'Windows', "OSX"],
# options=range(0, 100),
layout=small_box_layout,
)
widget_to_data[select_widget] = processed_data
main_box.children = (select_widget,)
# We then define the update function of the selector, that we will
# link to an observe callback pointed to the value trait of the select box.
def handle_change(caller, names="value"):
select_box_called = caller["owner"]
# the "train" box is different from the select box only if there is content
train_box = select_to_parent.get(select_box_called, select_box_called)
# change this to value when not s selectmultiple
# this_key = select_box_called.value
if select_box_called.value:
this_key = select_box_called.value[0]
else:
this_key = None
if this_key is not None:
# index = caller["new"]["index"]
if select_box_called not in widget_to_data:
raise Exception("Error. Could not identify the selection box.".format())
select_box_data = widget_to_data[select_box_called]
if this_key not in select_box_data:
raise Exception(
"Error. Could not identify value {} in data for selection box.".format(
this_key
)
)
new_value = select_box_data[this_key]
else:
new_value = None
keys, processed_data, these_values = process_data(new_value)
if keys is None:
if processed_data is None:
# We are in the None line, 'eliminate' the rest of selected boxes
main_box.children = main_box.children[
: main_box.children.index(train_box) + 1
]
else:
# we are in a value. Show it.
new_select_box = widgets.Text(
value=processed_data, layout=small_box_layout, disabled=True
)
main_box.children = main_box.children[
: main_box.children.index(train_box) + 1
] + (new_select_box,)
else:
# We need a new selected box. and potentially a text
if these_values is None or not these_values:
# We do not need to show any values for this level.
new_select_box = widgets.SelectMultiple(
# description='',
options=[None] + list(keys), # ordered_keys,
rows=10,
# options=['Linux\ndf', 'Windows', "OSX"],
# options=range(0, 100),
layout=small_box_layout,
)
new_select_box.observe(handle_change, names="value")
# the widget_to_data accumulates garbage with time. This is ok for a proto though.
widget_to_data[new_select_box] = processed_data
main_box.children = main_box.children[
: main_box.children.index(train_box) + 1
] + (new_select_box,)
else:
# we need to show values for this level.
new_select_box = widgets.SelectMultiple(
# description='',
options=[None] + list(keys), # ordered_keys,
rows=10,
# options=['Linux\ndf', 'Windows', "OSX"],
# options=range(0, 100),
layout=divided_box_layout,
)
value_content = "\n".join(
["{}: {}".format(key, value) for key, value in these_values.items()]
)
new_values_box = widgets.Textarea(
value=value_content, layout=divided_box_layout, disabled=True
)
new_select_box.observe(handle_change, names="value")
new_holding_box = widgets.VBox(
layout=small_box_layout, children=(new_values_box, new_select_box)
)
select_to_parent[new_select_box] = new_holding_box
# the widget_to_data accumulates garbage with time. This is ok for a proto though.
widget_to_data[new_select_box] = processed_data
main_box.children = main_box.children[
: main_box.children.index(train_box) + 1
] + (new_holding_box,)
select_widget.observe(handle_change, names="value")
return main_box