-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_data_viwer.py
99 lines (74 loc) · 3.11 KB
/
json_data_viwer.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
# Form implementation generated from reading ui file 'json_viwer.ui'
#
# Created by: PyQt6 UI code generator 6.3.1
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt6 import QtCore, QtGui, QtWidgets
import json
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])
return titem_list
class Ui_Json_Viewer(object):
text_to_titem = TextToTreeItem()
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("This should never be reached!")
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)
def setupUi(self, Json_Viewer,source_profile):
Json_Viewer.setObjectName("Json_Viewer")
Json_Viewer.resize(1078, 754)
self.jdata_viewer = QtWidgets.QTreeWidget(Json_Viewer)
self.jdata_viewer.setHeaderLabels(["Key", "Value"])
self.jdata_viewer.setGeometry(QtCore.QRect(40, 40, 961, 641))
self.jdata_viewer.setObjectName("jdata_viewer")
self.jdata_viewer.header().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.ResizeToContents)
#self.jdata_viewer.headerItem().setText(0, "1")
root_item = QtWidgets.QTreeWidgetItem(["Data Profile"])
self.recurse_jdata(source_profile, root_item)
self.jdata_viewer.addTopLevelItem(root_item)
self.retranslateUi(Json_Viewer)
QtCore.QMetaObject.connectSlotsByName(Json_Viewer)
def retranslateUi(self, Json_Viewer):
_translate = QtCore.QCoreApplication.translate
Json_Viewer.setWindowTitle(_translate("Json_Viewer", "Form"))
if __name__ == "__main__":
import sys
with open('data_profile.json', 'r') as f:
jdata = json.load(f)
app = QtWidgets.QApplication(sys.argv)
Json_Viewer = QtWidgets.QWidget()
ui = Ui_Json_Viewer()
ui.setupUi(Json_Viewer,source_profile=jdata)
Json_Viewer.show()
sys.exit(app.exec())