My goal:
- drop a list of files.
- Read files and extract columns.
- Dynamically generate dropdown componenets so the user can select the source and target columns.
- I need to return (globally) all the selections, so I can use them with process_files() function. I think (3) it should be solved using gr.State([]) but I am not sure how to update it. here is my code:
Function to process files
import gradio as gr
import pandas as pd
def process_files(files, selections):
results = []
for file, selection in zip(files, selections):
file_name = file.name
source_col, target_col, source_lang, target_lang = selection
try:
if file_name.endswith(".csv"):
df = pd.read_csv(file.name)
elif file_name.endswith(".xlsx"):
df = pd.read_excel(file.name, engine="openpyxl")
else:
continue # Skip unsupported files
# Extract selected columns
extracted_data = df[[source_col, target_col]]
results.append(
f"✅ Processed {file_name} ({source_lang} → {target_lang})")
except Exception as e:
results.append(f"Error processing {file_name}: {str(e)}")
return "\n".join(results)
Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## Upload Multiple Files and Select Columns")
file_input = gr.File(file_types=[
".csv", ".xlsx"], label="Drop your files here", interactive=True, file_count="multiple")
all_selections = gr.State([])
@gr.render(inputs=[file_input], triggers=[file_input.change])
def generate_column_selectors(files):
if not files:
return "## No Files Uploaded", [] # Reset selections
with gr.Column():
for file in files:
try:
if file.name.endswith(".csv"):
df = pd.read_csv(file.name)
elif file.name.endswith(".xlsx"):
df = pd.read_excel(file.name, engine="openpyxl")
else:
continue
with gr.Row():
gr.Markdown(f"**{file.name}**") # Display filename
source_col = gr.Dropdown(
df.columns.tolist(), label="Source Column", interactive=True)
target_col = gr.Dropdown(
df.columns.tolist(), label="Target Column", interactive=True)
source_lang = gr.Textbox(
label="Source Language", placeholder="e.g., English", interactive=True)
target_lang = gr.Textbox(
label="Target Language", placeholder="e.g., French", interactive=True)
all_selections.append(
(source_col, target_col, source_lang, target_lang)) # this does not work.
except Exception as e:
gr.Markdown(f"Error processing {file.name}: {str(e)}")
submit_button = gr.Button("Submit & Process", interactive=True)
output_text = gr.Markdown()
submit_button.click(process_files, inputs=[
file_input, all_selections], outputs=[output_text])
demo.launch()