File size: 2,814 Bytes
34dad03
 
65e7391
 
34dad03
65e7391
34dad03
65e7391
9d3fa20
 
65e7391
9d3fa20
 
65e7391
 
9d3fa20
65e7391
 
9d3fa20
 
65e7391
 
 
 
 
9d3fa20
fa2dcad
9d3fa20
 
 
fa2dcad
 
 
65e7391
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9d3fa20
 
 
 
65e7391
 
 
 
 
 
 
 
 
 
 
34dad03
 
9d3fa20
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
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()