|
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} |
|
""" |
|
|
|
|
|
if not df.empty: |
|
|
|
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 = 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 |
|
 |
|
|
|
#### Monthly Spending Trend |
|
 |
|
""" |
|
|
|
return report |