varun321's picture
Initial commit
1d54def
raw
history blame
1.68 kB
import matplotlib.pyplot as plt
import pandas as pd
import io
import base64
def generate_report(df, spending_analysis, recommendations):
report = f"""
## Invoice Reader & Budget Categorizer Report
### Categorized Transactions
{df.to_markdown(index=False)}
### Spending Insights
{spending_analysis}
### Budget Recommendations
{recommendations}
"""
# Generate visualizations
if not df.empty:
# Category-wise spending pie chart
category_spending = df.groupby("category")["amount"].sum()
plt.figure(figsize=(6, 6))
plt.pie(category_spending, labels=category_spending.index, autopct="%1.1f%%")
plt.title("Category-wise Spending")
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
category_plot = base64.b64encode(buf.getvalue()).decode("utf-8")
plt.close()
# Monthly spending trend
monthly_spending = df.groupby("month")["amount"].sum()
plt.figure(figsize=(8, 4))
monthly_spending.plot(kind="line", marker="o")
plt.title("Monthly Spending Trend")
plt.xlabel("Month")
plt.ylabel("Amount")
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
monthly_plot = base64.b64encode(buf.getvalue()).decode("utf-8")
plt.close()
report += f"""
### Visualizations
#### Category-wise Spending
![Category-wise Spending](data:image/png;base64,{category_plot})
#### Monthly Spending Trend
![Monthly Spending Trend](data:image/png;base64,{monthly_plot})
"""
return report