Spaces:
Sleeping
Sleeping
Update analytics_tab.py
Browse files- analytics_tab.py +17 -10
analytics_tab.py
CHANGED
|
@@ -1,7 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import plotly.graph_objects as go
|
| 4 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class AnalyticsTab:
|
| 7 |
def __init__(self, get_sheet_func, get_farmers_func, format_indian_currency_func, format_date_display_func):
|
|
@@ -150,7 +156,7 @@ class AnalyticsTab:
|
|
| 150 |
# Build PDF
|
| 151 |
doc.build(elements)
|
| 152 |
|
| 153 |
-
#
|
| 154 |
abs_path = os.path.abspath(pdf_filename)
|
| 155 |
if os.path.exists(abs_path):
|
| 156 |
file_size = os.path.getsize(abs_path)
|
|
@@ -176,21 +182,22 @@ class AnalyticsTab:
|
|
| 176 |
return None, None, f"❌ No transactions found for farmer '{farmer_name}'"
|
| 177 |
return None, None, "❌ No transactions found"
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
display_df = df[['Date', 'Farmer Name', 'Type', 'Bank Account', 'Amount', 'Balance']].copy()
|
| 181 |
-
display_df.columns = ['Date', 'Farmer', 'Description', 'Bank Account', 'Amount (₹)', 'Balance (₹)']
|
| 182 |
-
|
| 183 |
-
# Generate PDF with original numeric values in df
|
| 184 |
pdf_path = self.create_transactions_pdf(farmer_name, df)
|
| 185 |
|
| 186 |
-
#
|
|
|
|
|
|
|
| 187 |
display_df['Date'] = display_df['Date'].apply(lambda x: self.format_date_display(str(x)[:10]))
|
| 188 |
display_df['Amount (₹)'] = display_df['Amount (₹)'].apply(self.format_indian_currency)
|
| 189 |
display_df['Balance (₹)'] = display_df['Balance (₹)'].apply(self.format_indian_currency)
|
| 190 |
|
| 191 |
farmer_text = f" for {farmer_name}" if farmer_name and farmer_name.strip() else ""
|
| 192 |
|
| 193 |
-
#
|
|
|
|
|
|
|
|
|
|
| 194 |
return display_df, pdf_path, f"✅ Showing {len(display_df)} transactions{farmer_text}"
|
| 195 |
except Exception as e:
|
| 196 |
import traceback
|
|
@@ -479,7 +486,7 @@ class AnalyticsTab:
|
|
| 479 |
|
| 480 |
# PDF Download
|
| 481 |
with gr.Row():
|
| 482 |
-
trans_pdf_download = gr.File(label="📄 Download Transactions PDF", interactive=False)
|
| 483 |
|
| 484 |
all_trans_df = gr.Dataframe(
|
| 485 |
label="Transaction History",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
from datetime import datetime
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
from reportlab.lib.pagesizes import letter
|
| 6 |
+
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
|
| 7 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 8 |
+
from reportlab.lib import colors
|
| 9 |
+
from reportlab.lib.units import inch
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
class AnalyticsTab:
|
| 13 |
def __init__(self, get_sheet_func, get_farmers_func, format_indian_currency_func, format_date_display_func):
|
|
|
|
| 156 |
# Build PDF
|
| 157 |
doc.build(elements)
|
| 158 |
|
| 159 |
+
# Return absolute path for Gradio File component
|
| 160 |
abs_path = os.path.abspath(pdf_filename)
|
| 161 |
if os.path.exists(abs_path):
|
| 162 |
file_size = os.path.getsize(abs_path)
|
|
|
|
| 182 |
return None, None, f"❌ No transactions found for farmer '{farmer_name}'"
|
| 183 |
return None, None, "❌ No transactions found"
|
| 184 |
|
| 185 |
+
# Generate PDF FIRST with original numeric df
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
pdf_path = self.create_transactions_pdf(farmer_name, df)
|
| 187 |
|
| 188 |
+
# Then create display dataframe
|
| 189 |
+
display_df = df[['Date', 'Farmer Name', 'Type', 'Bank Account', 'Amount', 'Balance']].copy()
|
| 190 |
+
display_df.columns = ['Date', 'Farmer', 'Description', 'Bank Account', 'Amount (₹)', 'Balance (₹)']
|
| 191 |
display_df['Date'] = display_df['Date'].apply(lambda x: self.format_date_display(str(x)[:10]))
|
| 192 |
display_df['Amount (₹)'] = display_df['Amount (₹)'].apply(self.format_indian_currency)
|
| 193 |
display_df['Balance (₹)'] = display_df['Balance (₹)'].apply(self.format_indian_currency)
|
| 194 |
|
| 195 |
farmer_text = f" for {farmer_name}" if farmer_name and farmer_name.strip() else ""
|
| 196 |
|
| 197 |
+
# Debug print
|
| 198 |
+
print(f"PDF Path being returned: {pdf_path}")
|
| 199 |
+
print(f"PDF exists: {os.path.exists(pdf_path) if pdf_path else 'None'}")
|
| 200 |
+
|
| 201 |
return display_df, pdf_path, f"✅ Showing {len(display_df)} transactions{farmer_text}"
|
| 202 |
except Exception as e:
|
| 203 |
import traceback
|
|
|
|
| 486 |
|
| 487 |
# PDF Download
|
| 488 |
with gr.Row():
|
| 489 |
+
trans_pdf_download = gr.File(label="📄 Download Transactions PDF", interactive=False, visible=True)
|
| 490 |
|
| 491 |
all_trans_df = gr.Dataframe(
|
| 492 |
label="Transaction History",
|