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 display_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'] == "Geo Medical", "Lenstown"], | |
"Supplier 5": lambda df: df[df['Vendor'] == "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) | |
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=display_csv, inputs=[file_input, supplier_input, order_number_input], outputs=[output_df, output_file]) | |
# Run the interface | |
demo.launch() |