timfocus commited on
Commit
a919f44
·
verified ·
1 Parent(s): 50b545b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -8
app.py CHANGED
@@ -12,7 +12,7 @@ BARCODES_DIR = "barcodes"
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)",
@@ -23,17 +23,17 @@ REQUIRED_COLUMNS = [
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,4 +45,58 @@ def process_file(file_path):
45
  return label_files if label_files else "No labels generated."
46
 
47
  except Exception as e:
48
- return
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  os.makedirs(LABELS_DIR, exist_ok=True)
13
  os.makedirs(BARCODES_DIR, exist_ok=True)
14
 
15
+ # Required columns for the input file
16
  REQUIRED_COLUMNS = [
17
  "Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone",
18
  "Receiver Name", "Receiver Address", "Receiver Phone", "Package Weight (kg)",
 
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 all required columns exist
34
+ missing_cols = [col for col in REQUIRED_COLUMNS if col not in df.columns]
35
+ if missing_cols:
36
+ return f"Missing columns: {', '.join(missing_cols)}"
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"Error processing file: {str(e)}"
49
+
50
+ # Function to create a shipping label
51
+ def create_shipping_label(row, index):
52
+ try:
53
+ tracking_number = str(row["Tracking Number"])
54
+ barcode_path = os.path.join(BARCODES_DIR, f"barcode_{index}.png")
55
+
56
+ # Generate barcode
57
+ ean = barcode.get_barcode_class('code128')
58
+ ean = ean(tracking_number, writer=ImageWriter())
59
+ ean.save(barcode_path)
60
+
61
+ # Create PDF label
62
+ label_path = os.path.join(LABELS_DIR, f"label_{index}.pdf")
63
+ c = canvas.Canvas(label_path, pagesize=letter)
64
+
65
+ # Add shipping details
66
+ c.setFont("Helvetica", 12)
67
+ c.drawString(50, 750, f"Order ID: {row['Order ID']}")
68
+ c.drawString(50, 730, f"Date: {row['Order Date']}")
69
+ c.drawString(50, 710, f"From: {row['Shipper Name']}")
70
+ c.drawString(50, 690, f"Address: {row['Shipper Address']}")
71
+ c.drawString(50, 670, f"Phone: {row['Shipper Phone']}")
72
+ c.drawString(50, 650, f"To: {row['Receiver Name']}")
73
+ c.drawString(50, 630, f"Address: {row['Receiver Address']}")
74
+ c.drawString(50, 610, f"Phone: {row['Receiver Phone']}")
75
+ c.drawString(50, 590, f"Weight: {row['Package Weight (kg)']} kg")
76
+ c.drawString(50, 570, f"Method: {row['Shipping Method']}")
77
+ c.drawString(50, 550, f"Tracking #: {row['Tracking Number']}")
78
+ c.drawString(50, 530, f"ETA: {row['Estimated Delivery Date']}")
79
+
80
+ # Add barcode
81
+ if os.path.exists(barcode_path):
82
+ c.drawImage(barcode_path, 50, 400, width=200, height=50)
83
+ else:
84
+ c.drawString(50, 400, "Barcode generation failed.")
85
+
86
+ c.showPage()
87
+ c.save()
88
+
89
+ return label_path
90
+ except Exception as e:
91
+ return f"Error creating label: {str(e)}"
92
+
93
+ # Gradio interface
94
+ iface = gr.Interface(
95
+ fn=process_file,
96
+ inputs=gr.File(type="filepath"), # FIXED: Use 'filepath' instead of 'file'
97
+ outputs=gr.File(type="filepath", label="Download Labels"),
98
+ title="Shipping Label Generator",
99
+ description="Upload a .csv, .xls, or .xlsx file to generate shipping labels."
100
+ )
101
+
102
+ iface.launch()