File size: 1,510 Bytes
45983a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import os

# Define a function to save details to Excel
def save_to_excel(name, email, phone, address):
    # File path for Excel file
    file_path = "stored_details.xlsx"
    
    # Check if the Excel file exists
    if os.path.exists(file_path):
        # Load existing file
        df = pd.read_excel(file_path)
    else:
        # Create a new DataFrame if file doesn't exist
        df = pd.DataFrame(columns=["Name", "Email", "Phone", "Address"])
    
    # Append new details
    new_entry = {"Name": name, "Email": email, "Phone": phone, "Address": address}
    df = df.append(new_entry, ignore_index=True)
    
    # Save the updated DataFrame back to Excel
    df.to_excel(file_path, index=False, engine='openpyxl')
    
    return "Details saved successfully!", file_path

# Gradio Interface
def download_excel():
    # Return the path to the Excel file for download
    return "stored_details.xlsx"

# Define the Gradio components
with gr.Blocks() as demo:
    gr.Markdown("# Save Details to Excel")
    
    name = gr.Textbox(label="Name")
    email = gr.Textbox(label="Email")
    phone = gr.Textbox(label="Phone")
    address = gr.Textbox(label="Address")
    submit_button = gr.Button("Save to Excel")
    message = gr.Label()
    download_button = gr.File(label="Download Excel File")
    
    # Connect components
    submit_button.click(save_to_excel, [name, email, phone, address], [message, download_button])

# Launch the Gradio app
demo.launch()