Below is an example code that uses Gradio's blocks to create an interface with the described items:
import gradio as gr
import pandas as pd
def process_csv(file_path):
# Load the CSV file into a pandas DataFrame
global df
df = pd.read_csv(file_path)
# Update the drop-down choices with column1 values
dropdown.update(choices=list(df.iloc[:, 0].unique()))
# Return a message to indicate success
return "CSV Loaded. Dropdown updated."
def setup_ui():
with gr.Blocks() as demo:
gr.Markdown("### Upload CSV and Update Dropdown")
with gr.Row():
file_input = gr.Textbox(label="Enter CSV file name:")
upload_btn = gr.Button("Upload CSV")
dropdown = gr.Dropdown(label="Select from Column 1")
upload_btn.click(fn=process_csv, inputs=[file_input], outputs=[])
return demo
# Start the interface
if __name__ == "__main__":
demo = setup_ui()
demo.launch()
This code does the following:
- It imports the necessary libraries (
gradio and pandas).
- It defines a function
process_csv that takes a file_path as input, loads the CSV into a pandas DataFrame, updates the dropdown choices with the unique values from the first column of the DataFrame, and returns a success message.
- It defines a function
setup_ui to set up the Gradio interface with a textbox for the CSV file name, a button to trigger the CSV loading, and a dropdown that will contain the choices from the first column of the DataFrame.
- The
upload_btn.click method links the button to the process_csv function, with the file_input as the input and no outputs (since the dropdown is updated directly within the process_csv function).
- Finally, the
setup_ui function is called, and the interface is launched when the script is run.
Note that this code assumes that the CSV file is accessible from the file path provided in the textbox. For a web-based application, you would need to replace the textbox with a file upload component and adjust the function to handle the uploaded file.