Spaces:
Sleeping
Sleeping
Download app.py from aryan79/finance_tracker: direct link, hf CLI and curl.
- Browser
- Download file 3.12 kB
-
https://huggingface.co/spaces/aryan79/finance_tracker/resolve/main/app.py
- Command line
-
hf download hf://spaces/aryan79/finance_tracker/app.py
-
curl -L -o app.py https://huggingface.co/spaces/aryan79/finance_tracker/resolve/main/app.py
3.12 kB
| from flask import Flask, request, jsonify, render_template | |
| from flask_sqlalchemy import SQLAlchemy | |
| from flask_cors import CORS | |
| from database.models import db, Transaction, Budget | |
| app = Flask(__name__, template_folder="templates", static_folder="static") | |
| CORS(app) | |
| # β Configure SQLite Database | |
| app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///finance.db" | |
| app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False | |
| db.init_app(app) | |
| # β Create tables on startup | |
| with app.app_context(): | |
| db.create_all() | |
| # β Route for Serving Frontend | |
| def home(): | |
| return render_template("index.html") # Loads frontend | |
| # β API to Add Transaction | |
| def add_transaction(): | |
| data = request.json | |
| new_transaction = Transaction( | |
| amount=data["amount"], | |
| date=data["date"], | |
| description=data["description"], | |
| category=data["category"] | |
| ) | |
| db.session.add(new_transaction) | |
| db.session.commit() | |
| return jsonify(new_transaction.to_dict()), 201 | |
| # β API to Get Transactions | |
| def get_transactions(): | |
| transactions = Transaction.query.all() | |
| return jsonify([t.to_dict() for t in transactions]) | |
| # β API to Delete a Transaction | |
| def delete_transaction(id): | |
| transaction = Transaction.query.get(id) | |
| if transaction: | |
| db.session.delete(transaction) | |
| db.session.commit() | |
| return jsonify({"message": "Transaction deleted"}), 200 | |
| return jsonify({"error": "Transaction not found"}), 404 | |
| # β API to Set Budget for a Category | |
| def set_budget(): | |
| data = request.json | |
| category = data["category"] | |
| amount = data["amount"] | |
| budget = Budget.query.filter_by(category=category).first() | |
| if budget: | |
| budget.amount = amount # Update existing budget | |
| else: | |
| budget = Budget(category=category, amount=amount) | |
| db.session.add(budget) | |
| db.session.commit() | |
| return jsonify({"message": "Budget set successfully"}), 200 | |
| # β API to Get All Budgets | |
| def get_budgets(): | |
| budgets = Budget.query.all() | |
| return jsonify([b.to_dict() for b in budgets]) | |
| # β API to Get Spending Insights | |
| def spending_insights(): | |
| transactions = Transaction.query.all() | |
| budgets = {b.category: b.amount for b in Budget.query.all()} | |
| category_spending = {} | |
| insights = {} | |
| for t in transactions: | |
| category_spending[t.category] = category_spending.get(t.category, 0) + t.amount | |
| for category, spent in category_spending.items(): | |
| budget = budgets.get(category, 0) | |
| if spent > budget: | |
| insights[category] = f"β οΈ Over budget by ${spent - budget:.2f}" | |
| else: | |
| insights[category] = f"β Under budget by ${budget - spent:.2f}" | |
| return jsonify(insights) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=5001, debug=False, use_reloader=False) |