Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 tempfile
|
6 |
import os
|
7 |
|
8 |
# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
|
@@ -23,19 +23,16 @@ def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
|
|
23 |
}
|
24 |
ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
|
25 |
|
26 |
-
# Debugging output
|
27 |
-
print("New row added:", new_row)
|
28 |
-
print("Updated ledger:", ledger)
|
29 |
-
|
30 |
# Filter the ledger for the selected head
|
31 |
filtered_ledger = ledger[ledger["Head"] == head]
|
32 |
-
|
33 |
-
#
|
34 |
total_amount = filtered_ledger["Amount"].sum()
|
35 |
-
|
|
|
|
|
36 |
|
37 |
except Exception as e:
|
38 |
-
print(f"Error during adding invoice: {e}")
|
39 |
return pd.DataFrame(), "Error during adding invoice"
|
40 |
|
41 |
# Function to search and display all previous records by head
|
@@ -45,16 +42,12 @@ def search_by_head(head):
|
|
45 |
# Filter the ledger for the selected head
|
46 |
filtered_ledger = ledger[ledger["Head"] == head]
|
47 |
|
48 |
-
# Debugging output
|
49 |
-
print(f"Records for {head}:", filtered_ledger)
|
50 |
-
|
51 |
# Calculate the total for the selected head
|
52 |
total_amount = filtered_ledger["Amount"].sum()
|
53 |
|
54 |
-
return filtered_ledger, f"Total for {head}: {total_amount}"
|
55 |
|
56 |
except Exception as e:
|
57 |
-
print(f"Error during search by head: {e}")
|
58 |
return pd.DataFrame(), "Error during search by head"
|
59 |
|
60 |
# Function to generate a PDF report for a specific head
|
@@ -64,17 +57,12 @@ def generate_ledger_pdf(head):
|
|
64 |
# Filter records by head
|
65 |
filtered_ledger = ledger[ledger["Head"] == head]
|
66 |
|
67 |
-
# Check if there are any records for the specified head
|
68 |
if filtered_ledger.empty:
|
69 |
-
print(f"No records found for {head}")
|
70 |
return None
|
71 |
|
72 |
-
# Debugging output
|
73 |
-
print(f"Generating PDF for {head}")
|
74 |
-
|
75 |
# Use a temporary directory to save the file
|
76 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
77 |
-
pdf_file_path = tmp_file.name
|
78 |
|
79 |
# Create a PDF
|
80 |
pdf = FPDF()
|
@@ -101,25 +89,21 @@ def generate_ledger_pdf(head):
|
|
101 |
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
102 |
pdf.cell(50, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
103 |
|
104 |
-
# Total amount for the head
|
105 |
total_amount = filtered_ledger["Amount"].sum()
|
106 |
pdf.cell(140, 10, txt="Total Amount", border=1, align="R")
|
107 |
pdf.cell(50, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
108 |
|
109 |
-
# Save PDF
|
110 |
pdf.output(pdf_file_path)
|
111 |
|
112 |
-
# Return the
|
113 |
-
print(f"PDF generated at: {pdf_file_path}")
|
114 |
return pdf_file_path
|
115 |
|
116 |
except Exception as e:
|
117 |
-
print(f"Error during PDF generation: {e}")
|
118 |
return None
|
119 |
|
120 |
# Gradio interface
|
121 |
with gr.Blocks() as app:
|
122 |
-
# Section for adding new invoices with Head and Company Name fields
|
123 |
with gr.Row():
|
124 |
head = gr.Textbox(label="Head")
|
125 |
company_name = gr.Textbox(label="Company Name")
|
@@ -128,15 +112,13 @@ with gr.Blocks() as app:
|
|
128 |
date = gr.Textbox(label="Date")
|
129 |
amount = gr.Number(label="Amount")
|
130 |
add_button = gr.Button("Add Invoice")
|
131 |
-
|
132 |
-
# Section for searching and displaying ledger by head
|
133 |
with gr.Row():
|
134 |
search_head = gr.Textbox(label="Search by Head")
|
135 |
search_button = gr.Button("Search Ledger")
|
136 |
ledger_records = gr.DataFrame(label="Ledger Records")
|
137 |
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
138 |
|
139 |
-
# Section for generating and displaying PDF link
|
140 |
with gr.Row():
|
141 |
generate_pdf_button = gr.Button("Generate PDF")
|
142 |
pdf_output = gr.File(label="Download PDF")
|
@@ -144,20 +126,9 @@ with gr.Blocks() as app:
|
|
144 |
# Define button functionalities
|
145 |
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
146 |
|
147 |
-
|
148 |
-
filtered_ledger, total = search_by_head(head)
|
149 |
-
return filtered_ledger, total
|
150 |
-
|
151 |
-
search_button.click(search_ledger, [search_head], [ledger_records, total_display])
|
152 |
-
|
153 |
-
def generate_pdf(head):
|
154 |
-
pdf_path = generate_ledger_pdf(head)
|
155 |
-
if pdf_path:
|
156 |
-
return pdf_path
|
157 |
-
else:
|
158 |
-
return None
|
159 |
|
160 |
-
generate_pdf_button.click(
|
161 |
|
162 |
# Launch the app
|
163 |
app.launch(share=True)
|
|
|
2 |
import pandas as pd
|
3 |
from fpdf import FPDF
|
4 |
from datetime import datetime
|
5 |
+
import tempfile
|
6 |
import os
|
7 |
|
8 |
# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
|
|
|
23 |
}
|
24 |
ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
|
25 |
|
|
|
|
|
|
|
|
|
26 |
# Filter the ledger for the selected head
|
27 |
filtered_ledger = ledger[ledger["Head"] == head]
|
28 |
+
|
29 |
+
# Return filtered ledger and the sum of amounts for this head
|
30 |
total_amount = filtered_ledger["Amount"].sum()
|
31 |
+
|
32 |
+
# Return only the relevant rows and total to speed up processing
|
33 |
+
return filtered_ledger.to_dict('records'), f"Total for {head}: {total_amount}"
|
34 |
|
35 |
except Exception as e:
|
|
|
36 |
return pd.DataFrame(), "Error during adding invoice"
|
37 |
|
38 |
# Function to search and display all previous records by head
|
|
|
42 |
# Filter the ledger for the selected head
|
43 |
filtered_ledger = ledger[ledger["Head"] == head]
|
44 |
|
|
|
|
|
|
|
45 |
# Calculate the total for the selected head
|
46 |
total_amount = filtered_ledger["Amount"].sum()
|
47 |
|
48 |
+
return filtered_ledger.to_dict('records'), f"Total for {head}: {total_amount}"
|
49 |
|
50 |
except Exception as e:
|
|
|
51 |
return pd.DataFrame(), "Error during search by head"
|
52 |
|
53 |
# Function to generate a PDF report for a specific head
|
|
|
57 |
# Filter records by head
|
58 |
filtered_ledger = ledger[ledger["Head"] == head]
|
59 |
|
|
|
60 |
if filtered_ledger.empty:
|
|
|
61 |
return None
|
62 |
|
|
|
|
|
|
|
63 |
# Use a temporary directory to save the file
|
64 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
65 |
+
pdf_file_path = tmp_file.name
|
66 |
|
67 |
# Create a PDF
|
68 |
pdf = FPDF()
|
|
|
89 |
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
90 |
pdf.cell(50, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
91 |
|
|
|
92 |
total_amount = filtered_ledger["Amount"].sum()
|
93 |
pdf.cell(140, 10, txt="Total Amount", border=1, align="R")
|
94 |
pdf.cell(50, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
95 |
|
96 |
+
# Save the PDF
|
97 |
pdf.output(pdf_file_path)
|
98 |
|
99 |
+
# Return the path of the generated PDF
|
|
|
100 |
return pdf_file_path
|
101 |
|
102 |
except Exception as e:
|
|
|
103 |
return None
|
104 |
|
105 |
# Gradio interface
|
106 |
with gr.Blocks() as app:
|
|
|
107 |
with gr.Row():
|
108 |
head = gr.Textbox(label="Head")
|
109 |
company_name = gr.Textbox(label="Company Name")
|
|
|
112 |
date = gr.Textbox(label="Date")
|
113 |
amount = gr.Number(label="Amount")
|
114 |
add_button = gr.Button("Add Invoice")
|
115 |
+
|
|
|
116 |
with gr.Row():
|
117 |
search_head = gr.Textbox(label="Search by Head")
|
118 |
search_button = gr.Button("Search Ledger")
|
119 |
ledger_records = gr.DataFrame(label="Ledger Records")
|
120 |
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
121 |
|
|
|
122 |
with gr.Row():
|
123 |
generate_pdf_button = gr.Button("Generate PDF")
|
124 |
pdf_output = gr.File(label="Download PDF")
|
|
|
126 |
# Define button functionalities
|
127 |
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
128 |
|
129 |
+
search_button.click(search_by_head, [search_head], [ledger_records, total_display])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
+
generate_pdf_button.click(generate_ledger_pdf, [search_head], [pdf_output])
|
132 |
|
133 |
# Launch the app
|
134 |
app.launch(share=True)
|