I want the write a python program with gradio that can let the LLM generate excel file with the process as follow:
User can read and edit the generated excel file in the app
Have a download button which user can click it and it will download the file
How to do this? Show code.
Assistant
To create a Python program using Gradio that allows a user to generate, edit, and download an Excel file, you will need to use several libraries, including gradio for the web interface and pandas for handling Excel files. Below is a step-by-step implementation of this functionality.
Steps to Follow
Generate an initial Excel file using Python and pandas.
Create a Gradio interface that allows users to view and edit the DataFrame.
Provide a button for users to download the edited Excel file.
Requirements
You'll need to install the following packages if you haven't done so already:
bash
pip install gradio pandas openpyxl
Implementation
Here's a simple example that encapsulates all the above steps:
python
import pandas as pd
import gradio as gr
# Function to generate an initial Excel file
def generate_excel():
# Create a sample DataFrame
data = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']
}
df = pd.DataFrame(data)
# Save the initial DataFrame to an Excel file
excel_file_path = "initial_data.xlsx"
df.to_excel(excel_file_path, index=False)
return excel_file_path, df
# Function to save the edited DataFrame
def save_excel(df):
excel_file_path = "edited_data.xlsx"
df.to_excel(excel_file_path, index=False)
return excel_file_path
# Generate the initial Excel file and DataFrame
initial_file_path, initial_df = generate_excel()
# Gradio Interface
with gr.Blocks() as demo:
gr.Markdown("## Editable Excel File Generator")
# DataFrame input component
editable_df = gr.DataFrame(value=initial_df, label="Edit the DataFrame", interactive=True)
# Button to save the edited DataFrame
save_button = gr.Button("Save and Download Excel")
# Download link
download_link = gr.File(label="Download your Excel File", interactive=False)
# Logic for saving and updating the download link
def update_download_link(df):
saved_path = save_excel(df) # Save edited DataFrame to new Excel file
return saved_path
# Button interaction
save_button.click(update_download_link, inputs=[editable_df], outputs=[download_link])
# Launch the app
demo.launch()
Explanation of the Code
Generating the initial Excel file: The generate_excel function creates a simple DataFrame, saves it to an Excel file, and returns the file path along with the DataFrame.
Editing the DataFrame: The gr.DataFrame component allows users to edit the DataFrame directly in the web app.
Saving and Downloading the Edited File: When the "Save and Download Excel" button is clicked, the save_excel function is called with the edited DataFrame. The resulting Excel file path is sent back to the gr.File component, enabling the user to download the file.
Running the Code
To run the code, simply save it to a Python file (e.g., excel_gradio.py) and execute it:
bash
python excel_gradio.py
Once you run the script, a local server will start, and you'll be able to open the Gradio interface in your web browser. You can edit the DataFrame and download the modified Excel file.