This repository was archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
361 lines (309 loc) · 11.5 KB
/
main.py
File metadata and controls
361 lines (309 loc) · 11.5 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from typing import List
from fastapi import FastAPI, Query
import os
import pandas as pd
import json
app = FastAPI()
# Endpoint for getting files
@app.get("/getFiles")
def get_files():
"""
Returns a list of files in the working directory
---
Vrací seznam souborů v pracovním adresáři
"""
working_directory = os.getcwd()
files = os.listdir(working_directory)
csv_files = [file for file in files if file.endswith('.csv')]
xls_files = [file for file in files if file.endswith(('.xls', '.xlsx'))]
all_files = csv_files + xls_files
response = {
"csv_files": csv_files,
"xls_files": xls_files,
"all_files": all_files
}
return response
# Endpoint for getting sheets with a file name
@app.get("/getSheets/{file_name}")
def get_sheets(file_name: str ):
"""
Retrieves the names of all sheets in the specified Excel file.
Načte názvy všech listů v daném Excel souboru.
Args:
file_name (str): Optional parameter specifying the name of the Excel file. If not provided, it will default to None.
| Volitelný parametr specifikující název Excel souboru. Pokud není poskytnut, bude výchozí hodnota None.
Returns:
has_sheets: A boolean value indicating whether the file has sheets. | Logická hodnota indikující, zda soubor má listy.
sheets: A list of sheet names in the Excel file. | Seznam názvů listů v Excel souboru.
"""
if file_name is None:
response = {
"has_sheets": False,
"sheets": "No file name provided"
}
return response
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"has_sheets": False,
"sheets": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
response = {
"has_sheets": False,
"sheets": "CSV files do not have sheets"
}
else:
ex = pd.ExcelFile(file_path)
sheets = ex.sheet_names
response = {
"has_sheets": True,
"sheets": sheets
}
return response
# Endpoint for getting sheets without a file name
@app.get("/getSheets")
def get_sheets():
"""
Function for handling requests to the /getSheets endpoint without a file name.
Funkce pro zpracování požadavků na koncový bod /getSheets bez názvu souboru.
"""
response = {
"has_sheets": False,
"sheets": "No file name provided"
}
return response
# Endpoint for getting information and size of a file
@app.get("/getCSVInfo/{file_name}")
def get_file_info(file_name: str ):
"""
Retrieves information about a CSV file.
Načte informace o souboru ve formátu CSV.
Args:
file_name (str): Optional parameter specifying the name of the CSV file.
| Volitelný parametr specifikující název souboru ve formátu CSV.
Returns:
dict: Information about the CSV file.
| Informace o souboru ve formátu CSV.
"""
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"file_name": None,
"message": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
data = pd.read_csv(file_path)
info = data.describe()
response = {
"file_name": file_name,
"message": "File is a CSV",
"info": info,
"size_in_mb": os.path.getsize(file_path) / 1000000,
"shape": data.shape
}
if file_extension == '.xls' or file_extension == '.xlsx':
response = {
"file_name": file_name,
"message": "Please use getExcelInfo and specify a sheet name"
}
return response
@app.get("/getExcelInfo/{file_name}/{sheet_name}")
def get_file_info(file_name: str , sheet_name: str ):
"""
Retrieves information about an Excel file.
Načte informace o souboru ve formátu Excel.
Args:
file_name (str): Optional parameter specifying the name of the Excel file.
| Volitelný parametr specifikující název souboru ve formátu Excel.
sheet_name (str): Optional parameter specifying the name of the sheet in the Excel file.
| Volitelný parametr specifikující název listu v souboru ve formátu Excel.
Returns:
dict: Information about the Excel file.
| Informace o souboru ve formátu Excel.
"""
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"file_name": None,
"message": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
response = {
"file_name": file_name,
"message": "Please use getCSVInfo"
}
if file_extension == '.xls' or file_extension == '.xlsx':
ex = pd.ExcelFile(file_path)
if sheet_name not in ex.sheet_names:
response = {
"file_name": file_name,
"message": "Sheet does not exist"
}
return response
sheet_data = ex.parse(sheet_name)
info = sheet_data.describe().to_json()
response = {
"file_name": file_name,
"message": f"File is an Excel file, sheet {sheet_name}",
"size_in_mb": os.path.getsize(file_path) / 1000000,
"sheet": sheet_name,
"shape": sheet_data.shape,
"info": info
}
return response
@app.get("/getColumnInfo/{file_name}/{sheet_name}")
def get_column_info(file_name: str, sheet_name: str = None):
"""
Retrieves column information divided by data types for a file (CSV, XLS, XLSX).
Args:
file_name (str): The name of the file.
sheet_name (str, optional): The name of the sheet (only for XLS and XLSX files).
Returns:
dict: Column information divided by data types.
"""
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"file_name": None,
"message": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
data = pd.read_csv(file_path)
elif file_extension == '.xls' or file_extension == '.xlsx':
ex = pd.ExcelFile(file_path)
if sheet_name not in ex.sheet_names:
response = {
"file_name": file_name,
"message": "Sheet does not exist"
}
return response
data = ex.parse(sheet_name)
else:
response = {
"file_name": file_name,
"message": "Unsupported file format"
}
return response
column_info = {
"float_columns": list(data.select_dtypes(include=['float']).columns),
"int_columns": list(data.select_dtypes(include=['int']).columns),
"bool_columns": list(data.select_dtypes(include=['bool']).columns),
"datetime_columns": list(data.select_dtypes(include=['datetime']).columns),
"object_columns": list(data.select_dtypes(include=['object']).columns),
"all_columns": list(data.columns)
}
response = {
"file_name": file_name,
"message": "Column information by data types",
"column_info": column_info,
"size_in_mb": os.path.getsize(file_path) / 1000000,
"shape": data.shape
}
return response
@app.get("/getDataRange/{file_name}/{offset}/{num_lines}/{sheet_name}")
def get_column_info(file_name: str, offset: int = 0, num_lines: int = 5, sheet_name: str = None):
"""
Retrieves a range of data from a file (CSV, XLS, XLSX) based on the specified offset and number of lines.
Args:
file_name (str): The name of the file.
offset (int, optional): The starting offset of the data range.
num_lines (int, optional): The number of lines to retrieve. If set to -1, lines are sellected from the bottom.
sheet_name (str, optional): The name of the sheet (only for XLS and XLSX files).
Returns:
dict: Data range from the file.
"""
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"file_name": None,
"message": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
data = pd.read_csv(file_path)
elif file_extension == '.xls' or file_extension == '.xlsx':
ex = pd.ExcelFile(file_path)
if sheet_name not in ex.sheet_names:
response = {
"file_name": file_name,
"message": "Sheet does not exist"
}
return response
data = ex.parse(sheet_name)
else:
response = {
"file_name": file_name,
"message": "Unsupported file format"
}
return response
if num_lines < 0:
row_data = data.iloc[num_lines:].to_json(orient="records"),
else:
row_data = data.iloc[offset:offset + num_lines].to_json(orient="records"),
response = {
"file_name": file_name,
"message": f"{num_lines} rows from {offset} offset",
"data": row_data
}
return response
@app.get("/getColumnData/{file_name}/{sheet_name}")
def get_column_data(file_name: str, sheet_name: str, columns: List[int] = Query(...)):
"""
Retrieves data from specified columns of a file (CSV, XLS, XLSX).
Example of calling the API: 'GET /getColumnData/example.xlsx/Sheet1?columns=0,2,7'
Args:
file_name (str): The name of the file.
sheet_name (str): The name of the sheet.
columns (List[int]): The list of column numbers.
Returns:
dict: Data from the specified columns.
"""
working_directory = os.getcwd()
file_path = os.path.join(working_directory, file_name)
if not os.path.exists(file_path):
response = {
"file_name": None,
"message": "File does not exist"
}
return response
file_extension = os.path.splitext(file_name)[1]
if file_extension == '.csv':
data = pd.read_csv(file_path)
elif file_extension == '.xls' or file_extension == '.xlsx':
ex = pd.ExcelFile(file_path)
if sheet_name not in ex.sheet_names:
response = {
"file_name": file_name,
"message": "Sheet does not exist"
}
return response
data = ex.parse(sheet_name)
else:
response = {
"file_name": file_name,
"message": "Unsupported file format"
}
return response
selected_columns = data.iloc[:, columns]
column_data = selected_columns.to_json(orient='records')
response = {
"file_name": file_name,
"sheet_name": sheet_name,
"data": column_data
}
return response