Spaces:
Running
Running
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() | |