Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -413,6 +413,7 @@ def process_and_zip(items, fmt, w, h):
|
|
413 |
|
414 |
stop_event.clear()
|
415 |
return files, zip_path, "\n".join(msg_lines), tmp
|
|
|
416 |
def read_uploaded_workbook(file):
|
417 |
if not file:
|
418 |
return [], "β No file uploaded"
|
@@ -420,31 +421,50 @@ def read_uploaded_workbook(file):
|
|
420 |
# read all sheets except "Cleared Data"
|
421 |
xls = pd.ExcelFile(file.name)
|
422 |
sheets = [s for s in xls.sheet_names if s.lower() != "cleared data"]
|
423 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
424 |
df = pd.concat(df_list, ignore_index=True)
|
425 |
df.columns = [c.strip() for c in df.columns]
|
426 |
|
|
|
427 |
item_col = next((c for c in df.columns if c.lower() == 'itemcode'), None)
|
428 |
if not item_col:
|
429 |
return [], "β Missing 'ItemCode' column"
|
430 |
|
|
|
431 |
url_cols = [c for c in df.columns if any(k in c.lower() for k in ["url", "image", "link"])]
|
432 |
data = []
|
433 |
for _, row in df.iterrows():
|
434 |
raw = row[item_col]
|
435 |
if pd.isna(raw):
|
436 |
continue
|
437 |
-
key = str(raw).strip().split('.')[0]
|
|
|
|
|
438 |
idx = 0
|
439 |
for col in url_cols:
|
440 |
if pd.notna(row[col]):
|
441 |
name = f"{key}" if idx == 0 else f"{key}_{idx}"
|
442 |
data.append({"url": str(row[col]).strip(), "name": name})
|
443 |
idx += 1
|
|
|
444 |
return data, f"β
Fetched {len(data)} image link(s)"
|
|
|
445 |
except Exception as e:
|
446 |
return [], f"β Error: {e}"
|
447 |
|
|
|
448 |
def clear_all(tmp_dir):
|
449 |
# wipe out the temp folder if it exists
|
450 |
if tmp_dir and os.path.exists(tmp_dir):
|
@@ -695,7 +715,20 @@ with gr.Blocks(css=css) as demo:
|
|
695 |
inputs=[upload_box],
|
696 |
outputs=[status]
|
697 |
)
|
698 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
699 |
process_btn.click(
|
700 |
fn=process_all,
|
701 |
inputs=[
|
|
|
413 |
|
414 |
stop_event.clear()
|
415 |
return files, zip_path, "\n".join(msg_lines), tmp
|
416 |
+
|
417 |
def read_uploaded_workbook(file):
|
418 |
if not file:
|
419 |
return [], "β No file uploaded"
|
|
|
421 |
# read all sheets except "Cleared Data"
|
422 |
xls = pd.ExcelFile(file.name)
|
423 |
sheets = [s for s in xls.sheet_names if s.lower() != "cleared data"]
|
424 |
+
|
425 |
+
# load each sheet and drop ones that are totally empty
|
426 |
+
df_list = []
|
427 |
+
for s in sheets:
|
428 |
+
df_s = pd.read_excel(file.name, sheet_name=s, engine="openpyxl")
|
429 |
+
# drop rows and cols that are all-NA, then check if anything remains
|
430 |
+
if not df_s.dropna(how="all").empty:
|
431 |
+
df_list.append(df_s)
|
432 |
+
|
433 |
+
if not df_list:
|
434 |
+
return [], "β No data in any sheet"
|
435 |
+
|
436 |
+
# now safely concatenate only non-empty sheets
|
437 |
df = pd.concat(df_list, ignore_index=True)
|
438 |
df.columns = [c.strip() for c in df.columns]
|
439 |
|
440 |
+
# find the ItemCode column
|
441 |
item_col = next((c for c in df.columns if c.lower() == 'itemcode'), None)
|
442 |
if not item_col:
|
443 |
return [], "β Missing 'ItemCode' column"
|
444 |
|
445 |
+
# collect all URL/image/link columns
|
446 |
url_cols = [c for c in df.columns if any(k in c.lower() for k in ["url", "image", "link"])]
|
447 |
data = []
|
448 |
for _, row in df.iterrows():
|
449 |
raw = row[item_col]
|
450 |
if pd.isna(raw):
|
451 |
continue
|
452 |
+
key = (str(raw).strip().split('.')[0]
|
453 |
+
if str(raw).strip().replace('.', '', 1).isdigit()
|
454 |
+
else str(raw).strip())
|
455 |
idx = 0
|
456 |
for col in url_cols:
|
457 |
if pd.notna(row[col]):
|
458 |
name = f"{key}" if idx == 0 else f"{key}_{idx}"
|
459 |
data.append({"url": str(row[col]).strip(), "name": name})
|
460 |
idx += 1
|
461 |
+
|
462 |
return data, f"β
Fetched {len(data)} image link(s)"
|
463 |
+
|
464 |
except Exception as e:
|
465 |
return [], f"β Error: {e}"
|
466 |
|
467 |
+
|
468 |
def clear_all(tmp_dir):
|
469 |
# wipe out the temp folder if it exists
|
470 |
if tmp_dir and os.path.exists(tmp_dir):
|
|
|
715 |
inputs=[upload_box],
|
716 |
outputs=[status]
|
717 |
)
|
718 |
+
status.change(
|
719 |
+
fn=lambda txt: txt, # a noβop so we can attach JS
|
720 |
+
inputs=[status],
|
721 |
+
outputs=[status],
|
722 |
+
js="""
|
723 |
+
(txt) => {
|
724 |
+
const el = document.getElementById('status-box');
|
725 |
+
if (el) {
|
726 |
+
el.scrollTop = 0;
|
727 |
+
}
|
728 |
+
return txt;
|
729 |
+
}
|
730 |
+
"""
|
731 |
+
)
|
732 |
process_btn.click(
|
733 |
fn=process_all,
|
734 |
inputs=[
|