Spaces:
Running
Running
File size: 4,585 Bytes
34dad03 65e7391 34dad03 522a27f 34dad03 65e7391 9d3fa20 c3e1f9c 1fe3879 9d3fa20 65e7391 9d3fa20 65e7391 9d3fa20 fa2dcad 9d3fa20 fa2dcad 65e7391 2f518f0 9d3fa20 65e7391 522a27f f91c350 4465048 f91c350 4465048 f91c350 4465048 f91c350 34dad03 2f518f0 |
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 |
import gradio as gr
import pandas as pd
from datetime import datetime
import pytz # This library is used for timezone conversions
def order_csv(file, supplier, order_number):
df = pd.read_csv(file.name)
# Define supplier-specific filters
vendor_filters = {
"Supplier 1": lambda df: df[~df['Vendor'].isin(["Seed", "EverColor", "Candy Magic", "OLENS", "Fairy", "Shobido", "Geo Medical", "Ann365", "Lenstown", "LensVery"])],
"Supplier 2": lambda df: df[df['Vendor'].isin(["Candy Magic", "OLENS", "Fairy", "Shobido"])],
"Supplier 3": lambda df: df[df['Vendor'].isin(["Seed", "EverColor"])],
"Supplier 4": lambda df: df[df['Vendor'].isin(["Geo Medical", "Lenstown"])],
"Supplier 5": lambda df: df[df['Vendor'].isin(["Ann365", "LensVery"])]
}
# Apply supplier filter if selected
if supplier and supplier in vendor_filters:
df = vendor_filters[supplier](df)
# Filter based on order number
if order_number:
df = df[df['Order Number'] >= int(order_number)]
# Specify columns for output
columns_to_include = [
'Order Number', 'Quantity', 'Product Title', 'Product Option Name',
'Product Option Value', 'Order Line item Properties 2 Name',
'Order Line item Properties 2 Value', 'Order Line item Properties 3 Name',
'Order Line item Properties 3 Value'
]
download_df = df[columns_to_include]
# Get the current date in Hong Kong Time
hk_timezone = pytz.timezone("Asia/Hong_Kong")
current_date_hk = datetime.now(hk_timezone).strftime("%y%m%d")
# Set output file name based on the selected supplier
if supplier == "Supplier 1":
output_file = f"Leading Bridge Order {current_date_hk}.xlsx"
elif supplier == "Supplier 2":
output_file = f"Leading Bridge Order {current_date_hk}-Ammu.xlsx"
else:
output_file = "filtered_output.xlsx" # Default file name for other cases
# Save filtered DataFrame as Excel file
download_df.to_excel(output_file, index=False)
# Adjust column widths using openpyxl
from openpyxl import load_workbook
wb = load_workbook(output_file)
ws = wb.active
for column_cells in ws.columns:
max_length = 0
# Get the column letter (e.g. 'A', 'B', etc.)
column_letter = column_cells[0].column_letter
for cell in column_cells:
if cell.value is not None:
cell_length = len(str(cell.value))
if cell_length > max_length:
max_length = cell_length
adjusted_width = max_length + 2 # Add a little extra space
ws.column_dimensions[column_letter].width = adjusted_width
wb.save(output_file)
return df, output_file
# Define the main block for the interface
with gr.Blocks() as demo:
with gr.Row():
file_input = gr.File(label="Upload CSV")
supplier_input = gr.Dropdown(choices=["", "Supplier 1", "Supplier 2", "Supplier 3", "Supplier 4", "Supplier 5"], label="Select Supplier")
order_number_input = gr.Number(label="Minimum Order Number", precision=0)
load_button = gr.Button("Load Data")
with gr.Row():
output_df = gr.DataFrame()
output_file = gr.File(label="Download Filtered CSV")
# Bind the function to inputs and outputs using the button
load_button.click(fn=order_csv, inputs=[file_input, supplier_input, order_number_input], outputs=[output_df, output_file])
# Updated hyperlink block with separate sections for Shipping and Administration Tools.
gr.HTML(
"""
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
<h3>Shipping Tools</h3>
<a href="https://huggingface.co/spaces/leadingbridge/shipping-dhl-e-commerce">DHL</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-ec-ship">EC-Ship</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-fedex">Fedex</a> |
<a href="https://huggingface.co/spaces/leadingbridge/shipping-UPS">UPS</a>
</div>
<div style="text-align: center; font-size: 16px; margin-top: 20px;">
<h3>Administration Tools</h3>
<a href="https://huggingface.co/spaces/leadingbridge/email-template">Email Template</a> |
<a href="https://huggingface.co/spaces/leadingbridge/product-feed">Google Merchant</a> |
<a href="https://huggingface.co/spaces/leadingbridge/tss-order">Order Processing</a>
</div>
"""
)
# Run the interface
demo.launch()
|