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)
|
@@ -22,17 +22,16 @@ def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
|
|
22 |
"Amount": float(amount)
|
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 |
-
#
|
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
|
@@ -45,65 +44,71 @@ def search_by_head(head):
|
|
45 |
# Calculate the total for the selected head
|
46 |
total_amount = filtered_ledger["Amount"].sum()
|
47 |
|
48 |
-
return filtered_ledger
|
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
|
54 |
def generate_ledger_pdf(head):
|
55 |
global ledger
|
56 |
try:
|
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()
|
69 |
pdf.add_page()
|
|
|
|
|
70 |
pdf.set_font("Arial", 'B', 16)
|
71 |
-
pdf.cell(
|
72 |
-
pdf.set_font("Arial",
|
73 |
current_date = datetime.now().strftime("%d/%m/%Y")
|
74 |
-
pdf.cell(
|
75 |
pdf.ln(10)
|
76 |
-
|
77 |
# Table headers
|
78 |
-
pdf.
|
79 |
-
pdf.cell(
|
80 |
-
pdf.cell(
|
81 |
-
pdf.cell(30, 10,
|
82 |
-
pdf.cell(
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
85 |
for _, row in filtered_ledger.iterrows():
|
86 |
-
pdf.cell(
|
87 |
-
pdf.cell(40, 10,
|
88 |
-
pdf.cell(30, 10,
|
89 |
-
pdf.cell(30, 10,
|
90 |
-
pdf.cell(
|
91 |
|
|
|
92 |
total_amount = filtered_ledger["Amount"].sum()
|
93 |
-
pdf.
|
94 |
-
pdf.
|
|
|
|
|
95 |
|
96 |
-
# Save
|
97 |
pdf.output(pdf_file_path)
|
98 |
|
99 |
-
# Return the path
|
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,13 +117,15 @@ with gr.Blocks() as app:
|
|
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,9 +133,20 @@ with gr.Blocks() as app:
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
|
131 |
-
generate_pdf_button.click(
|
132 |
|
133 |
# Launch the app
|
134 |
app.launch(share=True)
|
|
|
2 |
import pandas as pd
|
3 |
from fpdf import FPDF
|
4 |
from datetime import datetime
|
5 |
+
import tempfile # Use for temporary file handling
|
6 |
import os
|
7 |
|
8 |
# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
|
|
|
22 |
"Amount": float(amount)
|
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 |
+
# Calculate the total for the selected head
|
30 |
total_amount = filtered_ledger["Amount"].sum()
|
31 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
|
|
|
|
32 |
|
33 |
except Exception as e:
|
34 |
+
print(f"Error during adding invoice: {e}")
|
35 |
return pd.DataFrame(), "Error during adding invoice"
|
36 |
|
37 |
# Function to search and display all previous records by head
|
|
|
44 |
# Calculate the total for the selected head
|
45 |
total_amount = filtered_ledger["Amount"].sum()
|
46 |
|
47 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
48 |
|
49 |
except Exception as e:
|
50 |
+
print(f"Error during search by head: {e}")
|
51 |
return pd.DataFrame(), "Error during search by head"
|
52 |
|
53 |
# Function to generate a PDF report for a specific head
|
54 |
def generate_ledger_pdf(head):
|
55 |
global ledger
|
56 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# Use a temporary directory to save the file
|
58 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
59 |
+
pdf_file_path = tmp_file.name # Get the temporary file path
|
60 |
|
61 |
# Create a PDF
|
62 |
pdf = FPDF()
|
63 |
pdf.add_page()
|
64 |
+
|
65 |
+
# Title and header
|
66 |
pdf.set_font("Arial", 'B', 16)
|
67 |
+
pdf.cell(0, 10, f"Ledger Report for {head}", ln=True, align="C")
|
68 |
+
pdf.set_font("Arial", 'I', 12)
|
69 |
current_date = datetime.now().strftime("%d/%m/%Y")
|
70 |
+
pdf.cell(0, 10, f"Report Date: {current_date}", ln=True, align="C")
|
71 |
pdf.ln(10)
|
72 |
+
|
73 |
# Table headers
|
74 |
+
pdf.set_font("Arial", 'B', 12)
|
75 |
+
pdf.cell(50, 10, "Company Name", border=1, align="C")
|
76 |
+
pdf.cell(40, 10, "SP/Station", border=1, align="C")
|
77 |
+
pdf.cell(30, 10, "Invoice No", border=1, align="C")
|
78 |
+
pdf.cell(30, 10, "Date", border=1, align="C")
|
79 |
+
pdf.cell(40, 10, "Amount", border=1, ln=True, align="C")
|
80 |
+
|
81 |
+
# Filter records by head
|
82 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
83 |
+
pdf.set_font("Arial", size=12)
|
84 |
+
|
85 |
for _, row in filtered_ledger.iterrows():
|
86 |
+
pdf.cell(50, 10, str(row['Company Name']), border=1, align="C")
|
87 |
+
pdf.cell(40, 10, str(row['SP/Station']), border=1, align="C")
|
88 |
+
pdf.cell(30, 10, str(row['Invoice No']), border=1, align="C")
|
89 |
+
pdf.cell(30, 10, str(row['Date']), border=1, align="C")
|
90 |
+
pdf.cell(40, 10, f"{row['Amount']:.2f}", border=1, ln=True, align="C")
|
91 |
|
92 |
+
# Total amount for the head
|
93 |
total_amount = filtered_ledger["Amount"].sum()
|
94 |
+
pdf.ln(10) # Add some space before the total
|
95 |
+
pdf.set_font("Arial", 'B', 12)
|
96 |
+
pdf.cell(150, 10, "Total Amount", border=1, align="R")
|
97 |
+
pdf.cell(40, 10, f"{total_amount:.2f}", border=1, ln=True, align="C")
|
98 |
|
99 |
+
# Save PDF file
|
100 |
pdf.output(pdf_file_path)
|
101 |
|
102 |
+
# Return the file path for download
|
103 |
return pdf_file_path
|
104 |
|
105 |
except Exception as e:
|
106 |
+
print(f"Error during PDF generation: {e}")
|
107 |
return None
|
108 |
|
109 |
# Gradio interface
|
110 |
with gr.Blocks() as app:
|
111 |
+
# Section for adding new invoices with Head and Company Name fields
|
112 |
with gr.Row():
|
113 |
head = gr.Textbox(label="Head")
|
114 |
company_name = gr.Textbox(label="Company Name")
|
|
|
117 |
date = gr.Textbox(label="Date")
|
118 |
amount = gr.Number(label="Amount")
|
119 |
add_button = gr.Button("Add Invoice")
|
120 |
+
|
121 |
+
# Section for searching and displaying ledger by head
|
122 |
with gr.Row():
|
123 |
search_head = gr.Textbox(label="Search by Head")
|
124 |
search_button = gr.Button("Search Ledger")
|
125 |
ledger_records = gr.DataFrame(label="Ledger Records")
|
126 |
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
127 |
|
128 |
+
# Section for generating and displaying PDF link
|
129 |
with gr.Row():
|
130 |
generate_pdf_button = gr.Button("Generate PDF")
|
131 |
pdf_output = gr.File(label="Download PDF")
|
|
|
133 |
# Define button functionalities
|
134 |
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
135 |
|
136 |
+
def search_ledger(head):
|
137 |
+
filtered_ledger, total = search_by_head(head)
|
138 |
+
return filtered_ledger, total
|
139 |
+
|
140 |
+
search_button.click(search_ledger, [search_head], [ledger_records, total_display])
|
141 |
+
|
142 |
+
def generate_pdf(head):
|
143 |
+
pdf_path = generate_ledger_pdf(head)
|
144 |
+
if pdf_path:
|
145 |
+
return pdf_path
|
146 |
+
else:
|
147 |
+
return None
|
148 |
|
149 |
+
generate_pdf_button.click(generate_pdf, [search_head], [pdf_output])
|
150 |
|
151 |
# Launch the app
|
152 |
app.launch(share=True)
|