Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from fpdf import FPDF
|
4 |
+
|
5 |
+
# Initialize an empty DataFrame to store invoice records
|
6 |
+
invoice_data = pd.DataFrame(columns=["Invoice No", "Date", "Amount"])
|
7 |
+
|
8 |
+
# Function to add new invoice entry and display total amount
|
9 |
+
def add_invoice(invoice_no, date, amount):
|
10 |
+
global invoice_data
|
11 |
+
# Add the new record to the DataFrame
|
12 |
+
new_row = {"Invoice No": invoice_no, "Date": date, "Amount": float(amount)}
|
13 |
+
invoice_data = pd.concat([invoice_data, pd.DataFrame([new_row])], ignore_index=True)
|
14 |
+
# Calculate the total amount
|
15 |
+
total_amount = invoice_data["Amount"].sum()
|
16 |
+
return invoice_data, f"Total Amount: {total_amount}"
|
17 |
+
|
18 |
+
# Function to generate and save the invoice data to a PDF
|
19 |
+
def generate_pdf():
|
20 |
+
global invoice_data
|
21 |
+
pdf = FPDF()
|
22 |
+
pdf.add_page()
|
23 |
+
pdf.set_font("Arial", size=12)
|
24 |
+
|
25 |
+
# Title of the PDF
|
26 |
+
pdf.cell(200, 10, txt="Invoice Records", ln=True, align="C")
|
27 |
+
|
28 |
+
# Table header
|
29 |
+
pdf.cell(60, 10, txt="Invoice No", border=1, align="C")
|
30 |
+
pdf.cell(60, 10, txt="Date", border=1, align="C")
|
31 |
+
pdf.cell(60, 10, txt="Amount", border=1, ln=True, align="C")
|
32 |
+
|
33 |
+
# Add invoice data to PDF
|
34 |
+
for index, row in invoice_data.iterrows():
|
35 |
+
pdf.cell(60, 10, txt=str(row['Invoice No']), border=1, align="C")
|
36 |
+
pdf.cell(60, 10, txt=str(row['Date']), border=1, align="C")
|
37 |
+
pdf.cell(60, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
38 |
+
|
39 |
+
# Add total amount at the end
|
40 |
+
total_amount = invoice_data["Amount"].sum()
|
41 |
+
pdf.cell(120, 10, txt="Total Amount", border=1, align="R")
|
42 |
+
pdf.cell(60, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
43 |
+
|
44 |
+
# Save PDF to file
|
45 |
+
pdf_file_path = "/content/invoice_records.pdf"
|
46 |
+
pdf.output(pdf_file_path)
|
47 |
+
|
48 |
+
return pdf_file_path
|
49 |
+
|
50 |
+
# Gradio interface
|
51 |
+
with gr.Blocks() as app:
|
52 |
+
# Section for adding new invoices
|
53 |
+
with gr.Row():
|
54 |
+
invoice_no = gr.Textbox(label="Invoice No")
|
55 |
+
date = gr.Textbox(label="Date")
|
56 |
+
amount = gr.Number(label="Amount")
|
57 |
+
add_button = gr.Button("Add Invoice")
|
58 |
+
|
59 |
+
# Section for displaying records and total amount
|
60 |
+
with gr.Row():
|
61 |
+
invoice_records = gr.DataFrame(label="Invoice Records")
|
62 |
+
total_amount = gr.Textbox(label="Total Amount", interactive=False)
|
63 |
+
|
64 |
+
# Section for generating and downloading PDF
|
65 |
+
with gr.Row():
|
66 |
+
generate_pdf_button = gr.Button("Generate PDF")
|
67 |
+
pdf_output = gr.File(label="Download PDF")
|
68 |
+
|
69 |
+
# Define button functionalities
|
70 |
+
add_button.click(add_invoice, [invoice_no, date, amount], [invoice_records, total_amount])
|
71 |
+
generate_pdf_button.click(generate_pdf, [], [pdf_output])
|
72 |
+
|
73 |
+
# Launch the app
|
74 |
+
app.launch(share=True)
|