Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,6 @@ import gradio as gr
|
|
| 7 |
import re
|
| 8 |
import zipfile
|
| 9 |
import datetime
|
| 10 |
-
|
| 11 |
import openpyxl
|
| 12 |
from openpyxl.styles import Font, PatternFill
|
| 13 |
|
|
@@ -236,6 +235,32 @@ def drop_missing_rows(df, columns):
|
|
| 236 |
df = df.dropna(subset=columns, how='any')
|
| 237 |
return df,removed_rows
|
| 238 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
warnings = []
|
| 241 |
def is_found(c,text):
|
|
@@ -385,7 +410,9 @@ def process(files,button):
|
|
| 385 |
return None, msg
|
| 386 |
|
| 387 |
names = unzip_files(files.name)
|
| 388 |
-
|
|
|
|
|
|
|
| 389 |
for name in names:
|
| 390 |
#name = os.path.basename(name)
|
| 391 |
if valid(name):
|
|
@@ -400,10 +427,10 @@ def process(files,button):
|
|
| 400 |
df = pd.read_excel(name)
|
| 401 |
|
| 402 |
old_cols = df.columns
|
| 403 |
-
|
| 404 |
print("Before columns")
|
| 405 |
-
print(
|
| 406 |
-
|
| 407 |
if "summ" in name:
|
| 408 |
df,columns = filter_premiums(df)
|
| 409 |
if columns == None:
|
|
@@ -427,7 +454,7 @@ def process(files,button):
|
|
| 427 |
finalnames.append(columns[ind]+" ("+altnames[ind]+")")
|
| 428 |
columns = [x for x in columns if x is not None]
|
| 429 |
|
| 430 |
-
|
| 431 |
print("After columns")
|
| 432 |
print(columns)
|
| 433 |
|
|
@@ -438,6 +465,17 @@ def process(files,button):
|
|
| 438 |
|
| 439 |
#display(df)
|
| 440 |
name = os.path.basename(name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
c_name = name.split('.')[0]+'_cleaned.csv'
|
| 442 |
df.to_csv(c_name,index=False)
|
| 443 |
|
|
@@ -455,10 +493,13 @@ def process(files,button):
|
|
| 455 |
status.append(op_outcome(name,' Failed (Only .csv, .xlsx, .zip are allowed)'+fail))
|
| 456 |
|
| 457 |
if len(cleaned_names) > 0 :
|
|
|
|
|
|
|
| 458 |
final_file = zip_files(cleaned_names)
|
| 459 |
else:
|
| 460 |
final_file = None
|
| 461 |
msg = '\n'.join(f"{index + 1}.{value}" for index, value in enumerate(status))
|
|
|
|
| 462 |
return gr.File.update(value=final_file,visible=True),msg
|
| 463 |
#return(str(files)+'fole')
|
| 464 |
|
|
@@ -510,4 +551,5 @@ with gr.Blocks(css=elems) as demo:
|
|
| 510 |
|
| 511 |
bt.click(fn = process, inputs=[inp,bt], outputs=[out,log])
|
| 512 |
|
| 513 |
-
|
|
|
|
|
|
| 7 |
import re
|
| 8 |
import zipfile
|
| 9 |
import datetime
|
|
|
|
| 10 |
import openpyxl
|
| 11 |
from openpyxl.styles import Font, PatternFill
|
| 12 |
|
|
|
|
| 235 |
df = df.dropna(subset=columns, how='any')
|
| 236 |
return df,removed_rows
|
| 237 |
|
| 238 |
+
def write_log(sheet_data_dict):
|
| 239 |
+
workbook = openpyxl.Workbook()
|
| 240 |
+
for sheet_name, data_dict in sheet_data_dict.items():
|
| 241 |
+
|
| 242 |
+
sheet = workbook.create_sheet(title=sheet_name)
|
| 243 |
+
for title, data in data_dict.items():
|
| 244 |
+
# Write the title
|
| 245 |
+
sheet.append([title])
|
| 246 |
+
title_cell = sheet.cell(row=sheet.max_row, column=1)
|
| 247 |
+
title_cell.font = Font(size=14, bold=True) # Set the font size and bold
|
| 248 |
+
|
| 249 |
+
# Write the list values
|
| 250 |
+
lst = data[0]
|
| 251 |
+
sheet.append(lst)
|
| 252 |
+
|
| 253 |
+
# Check if color is provided
|
| 254 |
+
if len(data) > 1:
|
| 255 |
+
color = data[1]
|
| 256 |
+
fill = PatternFill(start_color=color, end_color=color, fill_type="solid")
|
| 257 |
+
row_to_color = sheet[sheet.max_row]
|
| 258 |
+
for cell in row_to_color:
|
| 259 |
+
cell.fill = fill
|
| 260 |
+
|
| 261 |
+
workbook.remove(workbook["Sheet"])
|
| 262 |
+
workbook.save('Log.xlsx')
|
| 263 |
+
|
| 264 |
|
| 265 |
warnings = []
|
| 266 |
def is_found(c,text):
|
|
|
|
| 410 |
return None, msg
|
| 411 |
|
| 412 |
names = unzip_files(files.name)
|
| 413 |
+
|
| 414 |
+
sheet_data = dict()
|
| 415 |
+
|
| 416 |
for name in names:
|
| 417 |
#name = os.path.basename(name)
|
| 418 |
if valid(name):
|
|
|
|
| 427 |
df = pd.read_excel(name)
|
| 428 |
|
| 429 |
old_cols = df.columns
|
| 430 |
+
old_olds = list(old_cols)
|
| 431 |
print("Before columns")
|
| 432 |
+
print(old_olds)
|
| 433 |
+
|
| 434 |
if "summ" in name:
|
| 435 |
df,columns = filter_premiums(df)
|
| 436 |
if columns == None:
|
|
|
|
| 454 |
finalnames.append(columns[ind]+" ("+altnames[ind]+")")
|
| 455 |
columns = [x for x in columns if x is not None]
|
| 456 |
|
| 457 |
+
|
| 458 |
print("After columns")
|
| 459 |
print(columns)
|
| 460 |
|
|
|
|
| 465 |
|
| 466 |
#display(df)
|
| 467 |
name = os.path.basename(name)
|
| 468 |
+
#print(columns)
|
| 469 |
+
#print(warnings)
|
| 470 |
+
sheetwarning = ['None']
|
| 471 |
+
if len(warnings) > 0:
|
| 472 |
+
sheetwarnings = [warnings,'#FFA500']
|
| 473 |
+
sheet_data[name] = {
|
| 474 |
+
"Before columns": [old_olds],
|
| 475 |
+
"After columns": [columns, '00FF00'],
|
| 476 |
+
#"Warnings": [sheetwarnings]
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
c_name = name.split('.')[0]+'_cleaned.csv'
|
| 480 |
df.to_csv(c_name,index=False)
|
| 481 |
|
|
|
|
| 493 |
status.append(op_outcome(name,' Failed (Only .csv, .xlsx, .zip are allowed)'+fail))
|
| 494 |
|
| 495 |
if len(cleaned_names) > 0 :
|
| 496 |
+
write_log(sheet_data)
|
| 497 |
+
cleaned_names.append('Log.xlsx')
|
| 498 |
final_file = zip_files(cleaned_names)
|
| 499 |
else:
|
| 500 |
final_file = None
|
| 501 |
msg = '\n'.join(f"{index + 1}.{value}" for index, value in enumerate(status))
|
| 502 |
+
|
| 503 |
return gr.File.update(value=final_file,visible=True),msg
|
| 504 |
#return(str(files)+'fole')
|
| 505 |
|
|
|
|
| 551 |
|
| 552 |
bt.click(fn = process, inputs=[inp,bt], outputs=[out,log])
|
| 553 |
|
| 554 |
+
|
| 555 |
+
demo.launch(debug = True)
|