Update app.py
Browse files
app.py
CHANGED
|
@@ -12,22 +12,28 @@ BARCODES_DIR = "barcodes"
|
|
| 12 |
os.makedirs(LABELS_DIR, exist_ok=True)
|
| 13 |
os.makedirs(BARCODES_DIR, exist_ok=True)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Function to process the uploaded file
|
| 16 |
def process_file(file_path):
|
| 17 |
try:
|
| 18 |
# Load data from file
|
| 19 |
-
if file_path.endswith(
|
| 20 |
df = pd.read_csv(file_path)
|
| 21 |
-
elif file_path.endswith((
|
| 22 |
df = pd.read_excel(file_path)
|
| 23 |
else:
|
| 24 |
return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
|
| 25 |
|
| 26 |
# Ensure required columns exist
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
return f"Missing column: {col}"
|
| 31 |
|
| 32 |
# Process each row and generate labels
|
| 33 |
label_files = []
|
|
@@ -39,49 +45,4 @@ def process_file(file_path):
|
|
| 39 |
return label_files if label_files else "No labels generated."
|
| 40 |
|
| 41 |
except Exception as e:
|
| 42 |
-
return f"
|
| 43 |
-
|
| 44 |
-
# Function to create a shipping label
|
| 45 |
-
def create_shipping_label(row, index):
|
| 46 |
-
try:
|
| 47 |
-
tracking_number = str(row["Tracking Number"])
|
| 48 |
-
barcode_path = os.path.join(BARCODES_DIR, f"barcode_{index}.png")
|
| 49 |
-
|
| 50 |
-
# Generate barcode
|
| 51 |
-
ean = barcode.get_barcode_class('code128')
|
| 52 |
-
ean = ean(tracking_number, writer=ImageWriter())
|
| 53 |
-
ean.save(barcode_path)
|
| 54 |
-
|
| 55 |
-
# Create PDF label
|
| 56 |
-
label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
|
| 57 |
-
c = canvas.Canvas(label_path, pagesize=letter)
|
| 58 |
-
|
| 59 |
-
# Add sender & receiver details
|
| 60 |
-
c.setFont("Helvetica", 12)
|
| 61 |
-
c.drawString(50, 750, f"From: {row['Sender Name']}")
|
| 62 |
-
c.drawString(50, 730, f"To: {row['Receiver Name']}")
|
| 63 |
-
c.drawString(50, 710, f"Address: {row['Address']}, {row['City']}, {row['State']} {row['Zip']}")
|
| 64 |
-
|
| 65 |
-
# Add barcode
|
| 66 |
-
if os.path.exists(barcode_path):
|
| 67 |
-
c.drawImage(barcode_path, 50, 600, width=200, height=50)
|
| 68 |
-
else:
|
| 69 |
-
c.drawString(50, 600, "Barcode generation failed.")
|
| 70 |
-
|
| 71 |
-
c.showPage()
|
| 72 |
-
c.save()
|
| 73 |
-
|
| 74 |
-
return label_path
|
| 75 |
-
except Exception as e:
|
| 76 |
-
return f"Error creating label: {str(e)}"
|
| 77 |
-
|
| 78 |
-
# Gradio interface
|
| 79 |
-
iface = gr.Interface(
|
| 80 |
-
fn=process_file,
|
| 81 |
-
inputs=gr.File(type="filepath"), # FIXED: Use 'filepath' instead of 'file'
|
| 82 |
-
outputs=gr.File(type="filepath", label="Download Labels"),
|
| 83 |
-
title="Shipping Label Generator",
|
| 84 |
-
description="Upload a .csv, .xls, or .xlsx file to generate shipping labels."
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
iface.launch()
|
|
|
|
| 12 |
os.makedirs(LABELS_DIR, exist_ok=True)
|
| 13 |
os.makedirs(BARCODES_DIR, exist_ok=True)
|
| 14 |
|
| 15 |
+
# Define required columns
|
| 16 |
+
REQUIRED_COLUMNS = [
|
| 17 |
+
"Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone",
|
| 18 |
+
"Receiver Name", "Receiver Address", "Receiver Phone", "Package Weight (kg)",
|
| 19 |
+
"Shipping Method", "Tracking Number", "Estimated Delivery Date"
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
# Function to process the uploaded file
|
| 23 |
def process_file(file_path):
|
| 24 |
try:
|
| 25 |
# Load data from file
|
| 26 |
+
if file_path.endswith(".csv"):
|
| 27 |
df = pd.read_csv(file_path)
|
| 28 |
+
elif file_path.endswith((".xls", ".xlsx")):
|
| 29 |
df = pd.read_excel(file_path)
|
| 30 |
else:
|
| 31 |
return "Invalid file format. Please upload a .csv, .xls, or .xlsx file."
|
| 32 |
|
| 33 |
# Ensure required columns exist
|
| 34 |
+
missing_columns = [col for col in REQUIRED_COLUMNS if col not in df.columns]
|
| 35 |
+
if missing_columns:
|
| 36 |
+
return f"Missing columns: {', '.join(missing_columns)}"
|
|
|
|
| 37 |
|
| 38 |
# Process each row and generate labels
|
| 39 |
label_files = []
|
|
|
|
| 45 |
return label_files if label_files else "No labels generated."
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|