-
Notifications
You must be signed in to change notification settings - Fork 0
/
puzzle.py
66 lines (55 loc) · 1.98 KB
/
puzzle.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import flask
from flask import request
from celery import Celery
import lib.n_queens
import lib.sudoku
app = flask.Flask(__name__)
app.config['CELERY_BROKER_URL'] = os.environ.get("CLOUDAMQP_URL")
app.config['CELERY_RESULT_BACKEND'] = os.environ.get("CLOUDAMQP_URL")
app.config['CELERY_IGNORE_RESULT'] = False
if os.path.exists('config.py'):
app.config.from_pyfile('config.py')
celery = Celery(app.name, backend=['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL'], BROKER_POOL_LIMIT=3)
celery.conf.update(app.config)
@celery.task
def solve_n_queens_in_background(n):
return lib.n_queens.n_queens(n)
@app.route('/')
def index():
return flask.render_template('index.html')
@app.route('/about')
def about():
return flask.render_template('about.html')
@app.route('/sudoku')
def sudoku():
[clue_set, solution] = lib.sudoku.generate_clue_set()
return flask.render_template('sudoku.html', clue_set = clue_set, solution = solution)
@app.route('/n_queens')
def n_queens_default():
args = request.args
n = int(args['n']) if args else 8
solutions = lib.n_queens.n_queens(n)
return flask.render_template('n_queens.html', solutions = solutions, n = n)
@app.route('/n_queens_board_only')
def n_queens_board_only():
n = int(request.args['n'])
return flask.render_template('n_queens_container.html', solutions = [], n = n)
@app.route('/n_queens_solutions_only')
def n_queens_solutions_only():
n = int(request.args['n'])
task = solve_n_queens_in_background.delay(n)
return flask.jsonify(Location=flask.url_for('n_queens_background_task'), task_id=task.id), 200
@app.route('/n_queens_task')
def n_queens_background_task():
task_id = request.args.get('task_id')
task = solve_n_queens_in_background.AsyncResult(task_id)
response = {}
if task.state != 'PENDING':
response['result'] = task.result
else:
response['result'] = {'status': 'PENDING'}
return flask.jsonify(response)
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(port=port)