Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,13 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from fpdf import FPDF
|
4 |
from datetime import datetime
|
5 |
|
6 |
-
# Initialize an empty DataFrame to store invoice records
|
7 |
invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date", "Amount"])
|
8 |
|
9 |
-
# Function to add new invoice entry and display
|
10 |
def add_invoice(company_name, name, invoice_no, date, amount):
|
11 |
global invoice_data
|
12 |
# Add the new record to the DataFrame
|
@@ -18,52 +19,64 @@ def add_invoice(company_name, name, invoice_no, date, amount):
|
|
18 |
"Amount": float(amount)
|
19 |
}
|
20 |
invoice_data = pd.concat([invoice_data, pd.DataFrame([new_row])], ignore_index=True)
|
21 |
-
# Calculate the total amount
|
22 |
-
total_amount = invoice_data["Amount"].sum()
|
23 |
-
return invoice_data, f"Total Amount: {total_amount}"
|
24 |
|
25 |
-
#
|
26 |
-
|
27 |
-
global invoice_data
|
28 |
-
pdf = FPDF()
|
29 |
-
pdf.add_page()
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
|
68 |
# Gradio interface
|
69 |
with gr.Blocks() as app:
|
@@ -88,7 +101,7 @@ with gr.Blocks() as app:
|
|
88 |
|
89 |
# Define button functionalities
|
90 |
add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
|
91 |
-
generate_pdf_button.click(generate_pdf, [], [pdf_output])
|
92 |
|
93 |
# Launch the app
|
94 |
app.launch(share=True)
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import pandas as pd
|
4 |
from fpdf import FPDF
|
5 |
from datetime import datetime
|
6 |
|
7 |
+
# Initialize an empty DataFrame to store all invoice records
|
8 |
invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date", "Amount"])
|
9 |
|
10 |
+
# Function to add new invoice entry and display records by company name
|
11 |
def add_invoice(company_name, name, invoice_no, date, amount):
|
12 |
global invoice_data
|
13 |
# Add the new record to the DataFrame
|
|
|
19 |
"Amount": float(amount)
|
20 |
}
|
21 |
invoice_data = pd.concat([invoice_data, pd.DataFrame([new_row])], ignore_index=True)
|
|
|
|
|
|
|
22 |
|
23 |
+
# Filter records by company name
|
24 |
+
filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
|
|
|
|
|
|
|
25 |
|
26 |
+
# Calculate the total amount for that company
|
27 |
+
total_amount = filtered_data["Amount"].sum()
|
28 |
+
return filtered_data, f"Total Amount for {company_name}: {total_amount}"
|
29 |
+
|
30 |
+
# Function to generate and save the filtered invoice data to a PDF
|
31 |
+
def generate_pdf(company_name):
|
32 |
+
global invoice_data
|
33 |
|
34 |
+
try:
|
35 |
+
pdf = FPDF()
|
36 |
+
pdf.add_page()
|
37 |
+
|
38 |
+
# Add header
|
39 |
+
pdf.set_font("Arial", 'B', 16) # Bold and larger font for header
|
40 |
+
pdf.cell(200, 10, txt=f"Invoice Report for {company_name}", ln=True, align="C") # Report title
|
41 |
+
pdf.set_font("Arial", size=12) # Reset to normal font size
|
42 |
+
|
43 |
+
# Dynamic date in header
|
44 |
+
current_date = datetime.now().strftime("%d/%m/%Y")
|
45 |
+
pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
|
46 |
+
pdf.ln(10) # Add some space before the table starts
|
47 |
|
48 |
+
# Table header with Company Name and Name
|
49 |
+
pdf.cell(50, 10, txt="Company Name", border=1, align="C")
|
50 |
+
pdf.cell(40, 10, txt="Name", border=1, align="C")
|
51 |
+
pdf.cell(40, 10, txt="Invoice No", border=1, align="C")
|
52 |
+
pdf.cell(30, 10, txt="Date", border=1, align="C")
|
53 |
+
pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")
|
54 |
|
55 |
+
# Filter records by company name
|
56 |
+
filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
|
57 |
+
|
58 |
+
# Add filtered invoice data to PDF
|
59 |
+
for index, row in filtered_data.iterrows():
|
60 |
+
pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
|
61 |
+
pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
|
62 |
+
pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
|
63 |
+
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
64 |
+
pdf.cell(30, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
65 |
+
|
66 |
+
# Add total amount at the end for the company
|
67 |
+
total_amount = filtered_data["Amount"].sum()
|
68 |
+
pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
|
69 |
+
pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
70 |
|
71 |
+
# Save PDF to file
|
72 |
+
pdf_file_path = "/content/invoice_records.pdf"
|
73 |
+
pdf.output(pdf_file_path)
|
74 |
|
75 |
+
return pdf_file_path
|
76 |
+
|
77 |
+
except Exception as e:
|
78 |
+
print(f"Error generating PDF: {e}")
|
79 |
+
return None
|
80 |
|
81 |
# Gradio interface
|
82 |
with gr.Blocks() as app:
|
|
|
101 |
|
102 |
# Define button functionalities
|
103 |
add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
|
104 |
+
generate_pdf_button.click(generate_pdf, [company_name], [pdf_output])
|
105 |
|
106 |
# Launch the app
|
107 |
app.launch(share=True)
|