Flask-SIP-Cal / app.py
Alexvatti's picture
Create app.py
bce4929 verified
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# SIP Calculation Function
def calculate_sip(P, i, n):
M = P * (((1 + i) ** n - 1) / i) * (1 + i)
return M
# REST API Endpoint for SIP Calculation
@app.route('/api/sip', methods=['POST'])
def sip_api():
try:
data = request.json # Get JSON request data
P = float(data.get("P", 5000)) # Default: 5000
i = float(data.get("i", 12)) # Default: 12% annual
n = int(data.get("n", 10)) # Default: 10 years
# Convert annual interest rate to monthly and decimal
monthly_rate = i / 12 / 100
n_payments = n * 12
M = calculate_sip(P, monthly_rate, n_payments)
total_invested = P * n_payments
estimated_returns = M - total_invested
return jsonify({
"monthly_investment": P,
"annual_interest_rate": i,
"years": n,
"final_amount": M,
"total_invested": total_invested,
"estimated_returns": estimated_returns
})
except Exception as e:
return jsonify({"error": str(e)}), 400 # Return error in JSON format
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)