atifsial123 commited on
Commit
a18fe7e
·
verified ·
1 Parent(s): 3c31181

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
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 # Use this for temporary file handling
6
  import os
7
 
8
  # Initialize an empty DataFrame to store all invoice records
@@ -60,7 +60,6 @@ def generate_invoice_pdf(company_name):
60
 
61
  # Filter records by company name
62
  filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
63
- print(f"Processing {len(filtered_data)} records for {company_name}...")
64
 
65
  for _, row in filtered_data.iterrows():
66
  pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
@@ -82,7 +81,7 @@ def generate_invoice_pdf(company_name):
82
 
83
  except Exception as e:
84
  print(f"Error during PDF generation: {e}")
85
- return "Error: PDF generation failed."
86
 
87
  # Gradio interface
88
  with gr.Blocks() as app:
@@ -103,19 +102,19 @@ with gr.Blocks() as app:
103
  # Section for generating and displaying PDF link
104
  with gr.Row():
105
  generate_pdf_button = gr.Button("Generate PDF")
106
- pdf_link = gr.HTML() # To manually display the download link
107
 
108
  # Define button functionalities
109
  add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
110
 
111
- def show_pdf_link(company_name):
112
  pdf_path = generate_invoice_pdf(company_name)
113
- if pdf_path != "Error: PDF generation failed.":
114
- return f'<a href="{pdf_path}" target="_blank">Download PDF</a>'
115
  else:
116
- return "Error generating PDF."
117
 
118
- generate_pdf_button.click(show_pdf_link, [company_name], pdf_link)
119
 
120
  # Launch the app
121
  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 invoice records
 
60
 
61
  # Filter records by company name
62
  filtered_data = invoice_data[invoice_data["Company Name"] == company_name]
 
63
 
64
  for _, row in filtered_data.iterrows():
65
  pdf.cell(50, 10, txt=str(row['Company Name']), border=1, align="C")
 
81
 
82
  except Exception as e:
83
  print(f"Error during PDF generation: {e}")
84
+ return None
85
 
86
  # Gradio interface
87
  with gr.Blocks() as app:
 
102
  # Section for generating and displaying PDF link
103
  with gr.Row():
104
  generate_pdf_button = gr.Button("Generate PDF")
105
+ pdf_output = gr.File(label="Download PDF")
106
 
107
  # Define button functionalities
108
  add_button.click(add_invoice, [company_name, name, invoice_no, date, amount], [invoice_records, total_amount])
109
 
110
+ def generate_pdf(company_name):
111
  pdf_path = generate_invoice_pdf(company_name)
112
+ if pdf_path:
113
+ return pdf_path
114
  else:
115
+ return None
116
 
117
+ generate_pdf_button.click(generate_pdf, [company_name], [pdf_output])
118
 
119
  # Launch the app
120
  app.launch(share=True)