-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
162 lines (138 loc) · 5.34 KB
/
main.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
from fbs_runtime.application_context.PyQt6 import ApplicationContext
from PyQt6.QtCore import QSize, Qt
from PyQt6.QtWidgets import (
QApplication,
QFileDialog,
QMainWindow,
QPushButton,
QComboBox,
QLabel,
QGridLayout,
QWidget,
QLineEdit,
)
import polars as pl
import os
from os.path import isfile, join, realpath
from datetime import datetime
import sys
class DesktopDB(QWidget):
def __init__(self):
super().__init__()
self.file_paths = []
self.fld = ''
self.setWindowTitle('DesktopDB')
self.resize(500, 350)
layout = QGridLayout()
self.setLayout(layout)
## Widgets
# Selection Buttons
directory_button = QPushButton('Select Database Directory', clicked=self.directory_select)
flat_file_button = QPushButton('Select Flat File(s)', clicked=self.file_select)
# Input Field
input_label = QLabel('Enter Desired Database Name:')
self.inputField = QLineEdit()
# Combo Box
self.cb = QComboBox()
self.options = ('Create Single New Table from File(s)', 'Create New Table per File', 'Append File(s) to Existing Table')
self.cb.addItems(self.options)
self.cb.currentIndexChanged.connect(self.selection_change)
# Workflow Buttons
execute_workflow_button = QPushButton('Execute Workflow', clicked=self.run_workflow)
## Layout Structure
# Selection Button Layout
layout.addWidget(directory_button, 0, 0, 1, 3)
layout.addWidget(flat_file_button, 1, 0, 1, 3)
# Input Field Layout
layout.addWidget(input_label, 2, 0, 1,1)
layout.addWidget(self.inputField, 2, 1, 1, 2)
# Combo Box Layout
layout.addWidget(self.cb, 3, 0, 1, 3)
# Workflow Button Layout
layout.addWidget(execute_workflow_button, 4, 0, 1, 3)
# Combo Box Selection Change
def selection_change(self, i):
print("Items in the list are :")
for count in range(self.cb.count()):
print(self.cb.itemText(count))
print("Current index", i,"selection changed ", self.cb.currentText())
# Select the File Directory
def directory_select(self):
fld = QFileDialog.getExistingDirectory(
self,
caption = 'Select Database Directory'
)
self.fld = fld
return self.fld
def file_select(self):
files = QFileDialog.getOpenFileNames(
parent=self,
caption = 'Select File(s)',
directory=os.getcwd(),
filter = 'Data File (*.xlsx *.csv *.txt);; Excel File (*.xlsx)',
initialFilter='Data File (*.xlsx *.csv *.txt)'
)
self.file_paths = files[0]
return self.file_paths
def run_workflow(self):
input_text = self.inputField.text()
combo_selection = self.cb.currentText()
# Check if inputted table name has extension
if input_text.endswith('.parquet'):
tbl_name = input_text
elif not input_text.endswith('.parquet'):
tbl_name = f"{input_text}.parquet"
# Determine file type to import
def file_type(f):
if os.path.splitext(f)[1] == '.xlsx':
df = pl.read_excel(f)
elif os.path.splitext(f)[1] in ['.csv', '.txt']:
df = pl.read_csv(f)
return df
# Helpder Function to Concatenate Files - Called as Needed
def file_concat():
if len(self.file_paths) > 1:
dfo = pl.DataFrame()
for f in self.file_paths:
df = file_type(f)
dfo = pl.concat([dfo, df], how = 'diagonal')
elif len(self.file_paths) == 1:
dfo = file_type(self.file_paths[0])
return dfo
# Evaluate Drop Down Options
if combo_selection == 'Create Single New Table from File(s)':
df = file_concat()
df.write_parquet(f"{self.fld}/{tbl_name}", compression = 'zstd')
elif combo_selection == 'Create New Table per File':
for f in self.file_paths:
df = file_type(f)
df.write_parquet(f"{self.fld}/{os.path.splitext(os.path.basename(f))[0]}.parquet")
elif combo_selection == 'Append File(s) to Existing Table':
pfile = QFileDialog.getOpenFileName(
parent=self,
caption = 'Select Parquet File to Append To',
directory=os.getcwd(),
filter = 'Data File (*.parquet)',
initialFilter= 'Data File (*.parquet)'
)
df = file_concat()
dfp = pl.read_parquet(pfile[0])
dfp = pl.concat([dfp, df], how = 'diagonal')
dfp.write_parquet(pfile[0])
if __name__ == '__main__':
appctxt = ApplicationContext()
app = QApplication([])
app.setStyleSheet(
'''
QWidget{
font-size: 20px;
}
QPushButton{
font-size: 20px;
}
'''
)
DesktopDB = DesktopDB()
DesktopDB.show()
exit_code = appctxt.app.exec() # 2. Invoke appctxt.app.exec()
sys.exit(exit_code)