from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS  # Import CORS

app = Flask(__name__)
CORS(app)  # Enable CORS for all routes

# Serve the HTML front-end
@app.route('/')
def index():
    return send_from_directory(os.getcwd(), 'index.html')

@app.route('/sum_matrix', methods=['POST'])
def sum_matrix():
    try:
        matrix = request.json.get('matrix')
        if not matrix or not isinstance(matrix, list):
            return jsonify({"error": "Invalid matrix format"}), 400
        total_sum = sum(sum(row) for row in matrix)
        return jsonify({"sum": total_sum})
    except Exception as e:
        return jsonify({"error": str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True)

