-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
54 lines (45 loc) · 1.59 KB
/
Copy pathapp.py
File metadata and controls
54 lines (45 loc) · 1.59 KB
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
import os
import subprocess
from flask import Flask, request, jsonify, render_template
app = Flask(__name__)
# Serve the dashboard from the templates folder
@app.route('/')
def index():
return render_template('dashboard.html')
@app.route('/optimize', methods=['POST'])
def optimize():
data = request.json
sql_query = data.get('sql', '')
if not sql_query:
return jsonify({"status": "error", "message": "No SQL provided"}), 400
# Write the SQL from the frontend into a temporary file
with open('temp_query.sql', 'w') as f:
f.write(sql_query)
try:
# Execute the C++ backend
process = subprocess.run(
['./optimizer_test', 'temp_query.sql', '--html-out'],
capture_output=True,
text=True,
check=True
)
# Read the JSON output generated by your C++ code
if os.path.exists('result.json'):
with open('result.json', 'r') as f:
result_data = f.read()
return result_data, 200, {'Content-Type': 'application/json'}
else:
return jsonify({
"status": "error",
"message": "result.json was not generated by C++ backend.",
"cpp_output": process.stdout
}), 500
except subprocess.CalledProcessError as e:
return jsonify({
"status": "error",
"message": "C++ Engine Crashed.",
"cpp_error": e.stderr
}), 500
if __name__ == '__main__':
print("Starting Bootstrap Optimizer UI on http://localhost:8080")
app.run(debug=True, port=8080)