Spaces:
Running
Running
leadingbridge
commited on
Commit
•
9d3fa20
1
Parent(s):
fa2dcad
Update app.py
Browse files
app.py
CHANGED
@@ -2,56 +2,50 @@ import gradio as gr
|
|
2 |
import pandas as pd
|
3 |
|
4 |
def display_csv(file, supplier):
|
5 |
-
# Read the CSV file
|
6 |
df = pd.read_csv(file.name)
|
7 |
-
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
# Display the full DataFrame
|
23 |
-
display_df = df.copy()
|
24 |
-
|
25 |
-
# Select specific columns to include in the output CSV
|
26 |
columns_to_include = [
|
27 |
-
'Order Number',
|
28 |
-
'
|
29 |
-
'
|
30 |
-
'Product Option Name',
|
31 |
-
'Product Option Value',
|
32 |
-
'Order Line item Properties 2 Name',
|
33 |
-
'Order Line item Properties 2 Value',
|
34 |
-
'Order Line item Properties 3 Name',
|
35 |
'Order Line item Properties 3 Value'
|
36 |
]
|
37 |
download_df = df[columns_to_include]
|
38 |
-
|
39 |
-
# Save
|
40 |
output_file = "filtered_output.csv"
|
41 |
download_df.to_csv(output_file, index=False)
|
42 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
interface = gr.Interface(
|
46 |
-
fn=display_csv,
|
47 |
-
inputs=[
|
48 |
-
"file",
|
49 |
-
gr.Dropdown(choices=["", "Supplier 1", "Supplier 2", "Supplier 3", "Supplier 4", "Supplier 5"], label="Select Supplier", value="")
|
50 |
-
],
|
51 |
-
outputs=["dataframe", "file"],
|
52 |
-
title="CSV Viewer",
|
53 |
-
description="Upload a CSV file, optionally select a supplier for specific Vendor filtering, view the full contents on screen, and download a file with selected columns."
|
54 |
-
)
|
55 |
|
56 |
# Run the interface
|
57 |
-
|
|
|
2 |
import pandas as pd
|
3 |
|
4 |
def display_csv(file, supplier):
|
|
|
5 |
df = pd.read_csv(file.name)
|
6 |
+
|
7 |
+
# Define supplier-specific filters
|
8 |
+
vendor_filters = {
|
9 |
+
"Supplier 1": lambda df: df[~df['Vendor'].isin(["Seed", "EverColor", "Candy Magic", "OLENS", "Fairy", "Shobido", "Geo Medical", "Ann365"])],
|
10 |
+
"Supplier 2": lambda df: df[df['Vendor'].isin(["Candy Magic", "OLENS", "Fairy", "Shobido"])],
|
11 |
+
"Supplier 3": lambda df: df[df['Vendor'].isin(["Seed", "EverColor"])],
|
12 |
+
"Supplier 4": lambda df: df[df['Vendor'] == "Geo Medical"],
|
13 |
+
"Supplier 5": lambda df: df[df['Vendor'] == "Ann365"]
|
14 |
+
}
|
15 |
+
|
16 |
+
# Apply filter based on selected supplier
|
17 |
+
if supplier and supplier in vendor_filters:
|
18 |
+
df = vendor_filters[supplier](df)
|
19 |
+
|
20 |
+
# Specify columns for output
|
|
|
|
|
|
|
|
|
21 |
columns_to_include = [
|
22 |
+
'Order Number', 'Quantity', 'Product Title', 'Product Option Name',
|
23 |
+
'Product Option Value', 'Order Line item Properties 2 Name',
|
24 |
+
'Order Line item Properties 2 Value', 'Order Line item Properties 3 Name',
|
|
|
|
|
|
|
|
|
|
|
25 |
'Order Line item Properties 3 Value'
|
26 |
]
|
27 |
download_df = df[columns_to_include]
|
28 |
+
|
29 |
+
# Save filtered DataFrame as CSV file
|
30 |
output_file = "filtered_output.csv"
|
31 |
download_df.to_csv(output_file, index=False)
|
32 |
+
return df, output_file
|
33 |
+
|
34 |
+
# Define the main block for the interface
|
35 |
+
with gr.Blocks() as demo:
|
36 |
+
with gr.Tab("CSV Viewer"):
|
37 |
+
with gr.Row():
|
38 |
+
file_input = gr.File(label="Upload CSV")
|
39 |
+
supplier_input = gr.Dropdown(choices=["", "Supplier 1", "Supplier 2", "Supplier 3", "Supplier 4", "Supplier 5"], label="Select Supplier")
|
40 |
+
load_button = gr.Button("Load Data")
|
41 |
+
with gr.Row():
|
42 |
+
output_df = gr.DataFrame()
|
43 |
+
output_file = gr.File(label="Download Filtered CSV")
|
44 |
+
|
45 |
+
# Bind the function to inputs and outputs using the button
|
46 |
+
load_button.click(fn=display_csv, inputs=[file_input, supplier_input], outputs=[output_df, output_file])
|
47 |
|
48 |
+
# You can add more tabs here with different functions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Run the interface
|
51 |
+
demo.launch()
|