atifsial123 commited on
Commit
9ff0c52
·
verified ·
1 Parent(s): 5b1e7be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -30
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  import pandas as pd
3
  from fpdf import FPDF
4
  from datetime import datetime
5
- import os # To check file existence
6
 
7
  # Initialize an empty DataFrame to store all invoice records
8
  invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date", "Amount"])
@@ -10,31 +10,35 @@ invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date
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
14
- new_row = {
15
- "Company Name": company_name,
16
- "Name": name,
17
- "Invoice No": invoice_no,
18
- "Date": date,
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
- pdf_file_path = "/content/invoice_records.pdf" # Define the file path
34
 
35
  try:
36
- print(f"Generating PDF for company: {company_name}")
37
- # Create PDF
38
  pdf = FPDF()
39
  pdf.add_page()
40
  pdf.set_font("Arial", 'B', 16)
@@ -44,8 +48,6 @@ def generate_pdf(company_name):
44
  pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
45
  pdf.ln(10)
46
 
47
- # Log the generation process
48
- print("Adding table headers to the PDF")
49
  # Table headers
50
  pdf.cell(50, 10, txt="Company Name", border=1, align="C")
51
  pdf.cell(40, 10, txt="Name", border=1, align="C")
@@ -53,11 +55,11 @@ def generate_pdf(company_name):
53
  pdf.cell(30, 10, txt="Date", border=1, align="C")
54
  pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")
55
 
56
- # Add filtered data for the company
57
  filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
58
- print(f"Number of rows for {company_name}: {len(filtered_data)}")
59
 
60
- for index, row in filtered_data.iterrows():
61
  pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
62
  pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
63
  pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
@@ -69,8 +71,6 @@ def generate_pdf(company_name):
69
  pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
70
  pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")
71
 
72
- # Log that we're saving the PDF
73
- print(f"Saving PDF to {pdf_file_path}")
74
  # Save PDF file
75
  pdf.output(pdf_file_path)
76
 
@@ -79,7 +79,6 @@ def generate_pdf(company_name):
79
  print(f"PDF successfully created at {pdf_file_path}")
80
  return pdf_file_path
81
  else:
82
- print("Error: PDF file not found.")
83
  return "Error: PDF not generated."
84
 
85
  except Exception as e:
@@ -111,7 +110,7 @@ with gr.Blocks() as app:
111
  add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
112
 
113
  def show_pdf_link(company_name):
114
- pdf_path = generate_pdf(company_name)
115
  if pdf_path != "Error: PDF generation failed.":
116
  return f'<a href="{pdf_path}" target="_blank">Download PDF</a>'
117
  else:
 
2
  import pandas as pd
3
  from fpdf import FPDF
4
  from datetime import datetime
5
+ import os
6
 
7
  # Initialize an empty DataFrame to store all invoice records
8
  invoice_data = pd.DataFrame(columns=["Company Name", "Name", "Invoice No", "Date", "Amount"])
 
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
+ try:
14
+ # Add the new record to the DataFrame
15
+ new_row = {
16
+ "Company Name": company_name,
17
+ "Name": name,
18
+ "Invoice No": invoice_no,
19
+ "Date": date,
20
+ "Amount": float(amount)
21
+ }
22
+ invoice_data = pd.concat([invoice_data, pd.DataFrame([new_row])], ignore_index=True)
23
+
24
+ # Filter records by company name
25
+ filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
26
+
27
+ # Calculate the total amount for that company
28
+ total_amount = filtered_data["Amount"].sum()
29
+ return filtered_data, f"Total Amount for {company_name}: {total_amount}"
30
+
31
+ except Exception as e:
32
+ print(f"Error during adding invoice: {e}")
33
+ return pd.DataFrame(), "Error during adding invoice"
34
 
35
  # Function to generate and save the filtered invoice data to a PDF
36
+ def generate_invoice_pdf(company_name):
37
  global invoice_data
38
+ pdf_file_path = "/content/invoice_report.pdf" # Define the file path
39
 
40
  try:
41
+ # Create a PDF
 
42
  pdf = FPDF()
43
  pdf.add_page()
44
  pdf.set_font("Arial", 'B', 16)
 
48
  pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
49
  pdf.ln(10)
50
 
 
 
51
  # Table headers
52
  pdf.cell(50, 10, txt="Company Name", border=1, align="C")
53
  pdf.cell(40, 10, txt="Name", border=1, align="C")
 
55
  pdf.cell(30, 10, txt="Date", border=1, align="C")
56
  pdf.cell(30, 10, txt="Amount", border=1, ln=True, align="C")
57
 
58
+ # Filter records by company name
59
  filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
60
+ print(f"Processing {len(filtered_data)} records for {company_name}...")
61
 
62
+ for _, row in filtered_data.iterrows():
63
  pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
64
  pdf.cell(40, 10, txt=str(row['Name']), border=1, align="C")
65
  pdf.cell(40, 10, txt=str(row['Invoice No']), border=1, align="C")
 
71
  pdf.cell(160, 10, txt="Total Amount", border=1, align="R")
72
  pdf.cell(30, 10, txt=str(total_amount), border=1, ln=True, align="C")
73
 
 
 
74
  # Save PDF file
75
  pdf.output(pdf_file_path)
76
 
 
79
  print(f"PDF successfully created at {pdf_file_path}")
80
  return pdf_file_path
81
  else:
 
82
  return "Error: PDF not generated."
83
 
84
  except Exception as e:
 
110
  add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
111
 
112
  def show_pdf_link(company_name):
113
+ pdf_path = generate_invoice_pdf(company_name)
114
  if pdf_path != "Error: PDF generation failed.":
115
  return f'<a href="{pdf_path}" target="_blank">Download PDF</a>'
116
  else: