-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonTreeViewer.py
More file actions
219 lines (164 loc) · 6.69 KB
/
jsonTreeViewer.py
File metadata and controls
219 lines (164 loc) · 6.69 KB
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
#!/usr/bin/env python3
__author__ = "Ashwin Nanjappa"
# GUI viewer to view JSON data as tree.
# Ubuntu packages needed:
# python3-pyqt5
# Std
import argparse
import collections
import json
import sys
import pathlib
# External
from PyQt5 import QtCore
from PyQt5 import QtGui
from PyQt5 import QtWidgets
class TextToTreeItem:
def __init__(self):
self.text_list = []
self.titem_list = []
def append(self, text_list, titem):
for text in text_list:
self.text_list.append(text)
self.titem_list.append(titem)
# Return model indices that match string
def find(self, find_str):
titem_list = []
for i, s in enumerate(self.text_list):
if find_str in s:
titem_list.append(self.titem_list[i])
#print("The search ", find_str, " was in ",s) #debugging, later comment this
#else: #debugging, later comment this
#print("The search ", find_str, " was not in ",s) #debugging, later comment this
return titem_list
class JsonView(QtWidgets.QWidget):
def __init__(self, fpath, titleName, parent=None):
super(JsonView, self).__init__(parent=parent)
self.find_box = None
self.tree_widget = None
self.text_to_titem = TextToTreeItem()
self.find_str = ""
self.found_titem_list = []
self.found_idx = 0
#jfile = open(fpath)
#jdata = json.load(jfile, object_pairs_hook=collections.OrderedDict)
jdata = fpath
# Find UI
find_layout = self.make_find_ui()
# Tree
self.tree_widget = QtWidgets.QTreeWidget()
self.tree_widget.setAnimated(True)
current_directory = str(pathlib.Path(__file__).parent.absolute())
pathVline = current_directory.replace("\\", "/") + '/icon/vline.png'
pathbranchmore = current_directory.replace("\\", "/") + '/icon/branch-more.png'
pathbranchend = current_directory.replace("\\", "/") + '/icon/branch-end.png'
pathbranchclosed = current_directory.replace("\\", "/") + '/icon/branch-closed.png'
pathbranchopen = current_directory.replace("\\", "/") + '/icon/branch-open.png'
self.tree_widget.setStyleSheet("""
QTreeView::branch:has-siblings:!adjoins-item {
border-image: url("""+pathVline+""") 0;
}
QTreeView::branch:has-siblings:adjoins-item {
border-image: url("""+pathbranchmore+""") 0;
}
QTreeView::branch:!has-children:!has-siblings:adjoins-item {
border-image: url("""+pathbranchend+""") 0;
}
QTreeView::branch:has-children:!has-siblings:closed,
QTreeView::branch:closed:has-children:has-siblings {
border-image: none;
image: url("""+pathbranchclosed+""");
}
QTreeView::branch:open:has-children:!has-siblings,
QTreeView::branch:open:has-children:has-siblings {
border-image: none;
image: url("""+pathbranchopen+""");
}
""")
self.tree_widget.setHeaderLabels(["Key", "Value"])
self.tree_widget.header().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
root_item = QtWidgets.QTreeWidgetItem(["Root"])
self.recurse_jdata(jdata, root_item)
self.tree_widget.addTopLevelItem(root_item)
# Add table to layout
layout = QtWidgets.QHBoxLayout()
layout.addWidget(self.tree_widget)
# Group box
gbox = QtWidgets.QGroupBox(titleName)
gbox.setLayout(layout)
#Remove borders
gbox.setFlat(True)
layout2 = QtWidgets.QVBoxLayout()
layout2.addLayout(find_layout)
layout2.addWidget(gbox)
self.setLayout(layout2)
def make_find_ui(self):
# Text box
self.find_box = QtWidgets.QLineEdit()
self.find_box.returnPressed.connect(self.find_button_clicked)
self.find_box.setPlaceholderText("Insert string here. Case sensitive")
# Find Button
find_button = QtWidgets.QPushButton("Find")
find_button.clicked.connect(self.find_button_clicked)
# Search Label
search_label = QtWidgets.QLabel("Look for entry")
layout = QtWidgets.QHBoxLayout()
layout.addWidget(search_label)
layout.addWidget(self.find_box)
layout.addWidget(find_button)
return layout
def find_button_clicked(self):
find_str = self.find_box.text()
# Very common for use to click Find on empty string
if find_str == "":
return
# New search string
if find_str != self.find_str:
self.find_str = find_str
self.found_titem_list = self.text_to_titem.find(self.find_str)
self.found_idx = 0
item_num = len(self.found_titem_list)
if item_num == 0: # Handle if nothing culd be found
#TODO: A message may be sent to the user
pass
else:
self.tree_widget.setCurrentItem(self.found_titem_list[self.found_idx])
self.found_idx = (self.found_idx + 1) % item_num
def recurse_jdata(self, jdata, tree_widget):
if isinstance(jdata, dict):
for key, val in jdata.items():
self.tree_add_row(key, val, tree_widget)
elif isinstance(jdata, list):
for i, val in enumerate(jdata):
key = str(i)
self.tree_add_row(key, val, tree_widget)
else:
print("The data that you are trying to see is not structured in the correct way!")
def tree_add_row(self, key, val, tree_widget):
text_list = []
if isinstance(val, dict) or isinstance(val, list):
text_list.append(key)
row_item = QtWidgets.QTreeWidgetItem([key])
self.recurse_jdata(val, row_item)
else:
text_list.append(key)
text_list.append(str(val))
row_item = QtWidgets.QTreeWidgetItem([key, str(val)])
tree_widget.addChild(row_item)
self.text_to_titem.append(text_list, row_item)
class JsonViewer(QtWidgets.QMainWindow):
def __init__(self, fpath):
super(JsonViewer, self).__init__()
json_view = JsonView(fpath, "teste")
self.setCentralWidget(json_view)
self.setWindowTitle("JSON Viewer")
#Resize window
self.show()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Escape:
self.close()
if "__main__" == __name__:
qt_app = QtWidgets.QApplication(sys.argv)
fpath=r"D:\Documentos\OneDrive\Renan\Engenharia Civil\UMinho\01-REV@CONSTRUCTION\bSDD_API\Python\qtPracticing\json-viewer-master\sample.json"
json_viewer = JsonViewer(fpath)
sys.exit(qt_app.exec_())