-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
52 lines (39 loc) · 1.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from flask import Flask, request, jsonify
from flask_cors import CORS
import sys
import traceback
import logging
from backend.sudoku import solve_sudoku
import json
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info("Starting Sudoku Solver API")
sys.setrecursionlimit(60000)
app = Flask(__name__)
CORS(app)
# This function just tests the API is working
def handle_sudoku(fetched_grid):
solvable, ways, solvedGrid = solve_sudoku(fetched_grid)
return solvable, ways, solvedGrid
@app.route('/solve', methods=['POST']) # This endpoint receives a POST request
def solve():
try:
print("Received a request to solve a sudoku")
# Extract the grid from the incoming JSON data
data = request.get_json()
fetched_grid = data.get('grid') # grid is expected to be an array of arrays
if not fetched_grid:
return jsonify({'error': 'Grid not provided'}), 400
# Call the handle_sudoku function
solvable, ways, solvedGrid = handle_sudoku(fetched_grid)
# Send back a JSON response with solvability and number of ways
return jsonify({
'solvable': solvable,
'ways': ways,
'solvedGrid': solvedGrid,
})
except Exception as e:
print("Error occurred:", traceback.format_exc())
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
app.run(debug=True)