-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgui.py
More file actions
157 lines (126 loc) · 3.65 KB
/
gui.py
File metadata and controls
157 lines (126 loc) · 3.65 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
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
import pandas as pd
import json
from pathlib import Path
data_list = []
file_names = []
df = None
root = tk.Tk()
root.title('File Conversion Tool')
canvas1 = tk.Canvas(
root,
width=310,
height=360,
bg='lightsteelblue2',
relief='raised')
canvas1.pack()
label1 = tk.Label(
root,
text='JSON => CSV',
bg='lightsteelblue2'
)
label1.config(font=('helvetica', 14))
canvas1.create_window(150, 40, window=label1)
def getJSON():
global data_list
global file_names
global df
filez = filedialog.askopenfilenames(parent=root, title='Choose JSON files')
read_files = list(filez)
file_names = [Path(p).stem for p in read_files]
for fpath in read_files:
if Path(fpath).is_file():
with open(fpath, 'r') as f:
data = json.load(f)
data_list.append(data)
else:
data_list.append(dict())
df = pd.json_normalize(data_list)
def convertToCSV():
export_file_path = filedialog.asksaveasfilename(defaultextension='.csv')
df2 = df.transpose()
df2.to_csv(export_file_path, header=file_names, index=True)
msg_success()
browseButton_JSON = tk.Button(
text="Import JSON Files",
command=getJSON,
bg='green',
fg='white',
font=('helvetica', 10, 'bold')
)
canvas1.create_window(150, 90, window=browseButton_JSON)
saveAsButton_CSV = tk.Button(
text='Convert JSON to CSV',
command=convertToCSV,
bg='green',
fg='white',
font=('helvetica', 10, 'bold'))
canvas1.create_window(150, 130, window=saveAsButton_CSV)
# ========== CSV to JSON
csv_path = None
label1 = tk.Label(
root,
text='CSV => JSON',
bg='lightsteelblue2'
)
label1.config(font=('helvetica', 14))
canvas1.create_window(150, 180, window=label1)
def getCSV():
global csv_path
csv_path = filedialog.askopenfilename(parent=root, title='Choose CSV file')
def convertToJSON():
df2 = pd.read_csv(csv_path, index_col=0)
col_names = df2.columns.tolist()
df2 = df2.transpose()
df2.reset_index(drop=True, inplace=True)
df2.fillna('', inplace=True)
dir_path = filedialog.askdirectory()
# back to nested json (2 level supported)
for i, row in enumerate(df2.to_dict('r')):
tmp_dict = {}
for key, val in row.items():
if '.' in key:
key1, key2 = key.split('.')
if not tmp_dict.get(key1):
tmp_dict[key1] = {}
tmp_dict[key1][key2] = val
else:
tmp_dict[key] = val
with open('{}/{}.json'.format(dir_path, col_names[i]), 'w', encoding='utf8') as f:
json.dump(tmp_dict, f, ensure_ascii=False)
msg_success()
browseButton_CSV = tk.Button(
text="Import CSV File",
command=getCSV,
bg='blue',
fg='white',
font=('helvetica', 10, 'bold')
)
canvas1.create_window(150, 220, window=browseButton_CSV)
saveAsButton_JSON = tk.Button(
text='Convert CSV to JSON',
command=convertToJSON,
bg='blue',
fg='white',
font=('helvetica', 12, 'bold')
)
canvas1.create_window(150, 260, window=saveAsButton_JSON)
def msg_success():
msg = tk.messagebox.showinfo('Success', 'Conversion is done')
def exitApplication():
MsgBox = tk.messagebox.askquestion(
'Exit Application', 'Are you sure you want to exit the application', icon='warning')
if MsgBox == 'yes':
root.destroy()
exitButton = tk.Button(
root,
text='Exit',
command=exitApplication,
bg='brown',
fg='white',
font=('helvetica', 12, 'bold')
)
canvas1.create_window(150, 310, window=exitButton)
root.mainloop()