Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,151 +1,34 @@
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
-
from
|
|
|
|
| 3 |
import os
|
| 4 |
-
from parser import parse_python_code
|
| 5 |
-
from collections import defaultdict
|
| 6 |
-
app = Flask(__name__)
|
| 7 |
-
UPLOAD_FOLDER = 'uploads'
|
| 8 |
-
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
| 9 |
|
| 10 |
-
|
| 11 |
-
os.makedirs(UPLOAD_FOLDER)
|
| 12 |
|
| 13 |
@app.route('/')
|
| 14 |
def index():
|
| 15 |
return render_template('index.html')
|
| 16 |
-
|
| 17 |
-
@app.route('/update_node', methods=['POST'])
|
| 18 |
-
def update_node():
|
| 19 |
-
data = request.get_json()
|
| 20 |
-
node_id = data.get('id')
|
| 21 |
-
new_source = data.get('source')
|
| 22 |
-
# Update node in stored nodes (e.g., in-memory or database)
|
| 23 |
-
# Re-parse to validate and update connections
|
| 24 |
-
return jsonify({'status': 'success'})
|
| 25 |
-
|
| 26 |
-
@app.route('/update_program', methods=['POST'])
|
| 27 |
-
def update_program():
|
| 28 |
-
data = request.get_json()
|
| 29 |
-
code = data.get('code')
|
| 30 |
-
parts, _ = parse_python_code(code)
|
| 31 |
-
# Update nodes and connections
|
| 32 |
-
return jsonify({'status': 'success'})
|
| 33 |
-
|
| 34 |
-
@app.route('/parse_code', methods=['POST'])
|
| 35 |
-
def parse_code():
|
| 36 |
-
code = None
|
| 37 |
-
if 'file' in request.files and request.files['file'].filename:
|
| 38 |
-
file = request.files['file']
|
| 39 |
-
filename = secure_filename(file.filename)
|
| 40 |
-
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 41 |
-
file.save(file_path)
|
| 42 |
-
with open(file_path, 'r') as f:
|
| 43 |
-
code = f.read()
|
| 44 |
-
os.remove(file_path)
|
| 45 |
-
elif 'code' in request.form:
|
| 46 |
-
code = request.form['code']
|
| 47 |
-
|
| 48 |
-
if not code:
|
| 49 |
-
return jsonify({'error': 'No code or file provided'}), 400
|
| 50 |
-
|
| 51 |
-
parts, _ = parse_python_code(code)
|
| 52 |
-
|
| 53 |
-
nodes = []
|
| 54 |
-
connections = []
|
| 55 |
-
node_id_map = {}
|
| 56 |
-
scope_positions = defaultdict(lambda: {'x': 50, 'y': 50}) # Track positions per scope
|
| 57 |
-
|
| 58 |
-
for part in parts:
|
| 59 |
-
category = part['category']
|
| 60 |
-
node_id = part['node_id']
|
| 61 |
-
source = part['source'].strip()
|
| 62 |
-
scope = part['parent_path'].split(' -> ')[0] if ' -> ' in part['parent_path'] else 'global'
|
| 63 |
-
level = part['level']
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
if category == 'function':
|
| 79 |
-
if 'def ' in source:
|
| 80 |
-
func_name = source.split('def ')[1].split('(')[0]
|
| 81 |
-
node_data['label'] = func_name
|
| 82 |
-
params = source.split('(')[1].split(')')[0].split(',')
|
| 83 |
-
params = [p.strip() for p in params if p.strip()]
|
| 84 |
-
node_data['inputs'] = params
|
| 85 |
-
node_data['outputs'] = ['return']
|
| 86 |
-
elif category == 'input_variable':
|
| 87 |
-
var_name = source.strip().rstrip(',')
|
| 88 |
-
node_data['label'] = var_name
|
| 89 |
-
node_data['outputs'] = [var_name]
|
| 90 |
-
elif category == 'assigned_variable':
|
| 91 |
-
var_name = source.split('=')[0].strip()
|
| 92 |
-
node_data['label'] = var_name
|
| 93 |
-
node_data['inputs'] = ['value']
|
| 94 |
-
node_data['outputs'] = [var_name]
|
| 95 |
-
if node_data['value'] and node_data['value'].isdigit():
|
| 96 |
-
node_data['type'] = 'number_box'
|
| 97 |
-
elif category == 'returned_variable':
|
| 98 |
-
var_name = source.split('return ')[1].strip() if 'return ' in source else node_id
|
| 99 |
-
node_data['label'] = var_name
|
| 100 |
-
node_data['inputs'] = [var_name]
|
| 101 |
-
elif category == 'import':
|
| 102 |
-
import_name = source.split('import ')[1].split()[0] if 'import ' in source else node_id
|
| 103 |
-
node_data['label'] = import_name
|
| 104 |
-
|
| 105 |
-
nodes.append(node_data)
|
| 106 |
-
node_id_map[node_id] = node_data['id']
|
| 107 |
-
scope_positions[scope]['y'] += 100
|
| 108 |
-
|
| 109 |
-
# Create connections based on variable usage
|
| 110 |
-
for part in parts:
|
| 111 |
-
category = part['category']
|
| 112 |
-
node_id = part['node_id']
|
| 113 |
-
if node_id in node_id_map:
|
| 114 |
-
scope = part['parent_path'].split(' -> ')[0] if ' -> ' in part['parent_path'] else 'global'
|
| 115 |
-
var_defs = part.get('var_defs', {})
|
| 116 |
-
var_uses = part.get('var_uses', {})
|
| 117 |
-
if category == 'assigned_variable':
|
| 118 |
-
var_name = part['source'].split('=')[0].strip()
|
| 119 |
-
# Connect to nodes where this variable is used
|
| 120 |
-
for use_node_id, use_scope in var_uses.get(var_name, []):
|
| 121 |
-
if use_node_id in node_id_map and use_scope == scope:
|
| 122 |
-
connections.append({
|
| 123 |
-
'from': node_id_map[node_id],
|
| 124 |
-
'to': node_id_map[use_node_id]
|
| 125 |
-
})
|
| 126 |
-
elif category == 'function':
|
| 127 |
-
# Connect function to its input and returned variables
|
| 128 |
-
for input_part in parts:
|
| 129 |
-
if input_part['category'] == 'input_variable' and input_part['parent_path'].startswith(node_id):
|
| 130 |
-
connections.append({
|
| 131 |
-
'from': node_id_map[input_part['node_id']],
|
| 132 |
-
'to': node_id_map[node_id]
|
| 133 |
-
})
|
| 134 |
-
for return_part in parts:
|
| 135 |
-
if return_part['category'] == 'returned_variable' and return_part['parent_path'].startswith(node_id):
|
| 136 |
-
connections.append({
|
| 137 |
-
'from': node_id_map[node_id],
|
| 138 |
-
'to': node_id_map[return_part['node_id']]
|
| 139 |
-
})
|
| 140 |
-
|
| 141 |
-
return jsonify({'nodes': nodes, 'connections': connections})
|
| 142 |
-
|
| 143 |
-
@app.route('/save_nodes', methods=['POST'])
|
| 144 |
-
def save_nodes():
|
| 145 |
data = request.get_json()
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
if __name__ == '__main__':
|
|
|
|
| 151 |
app.run(host="0.0.0.0", port=7860, debug=True)
|
|
|
|
| 1 |
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
from parser import parse_source_to_graph
|
| 3 |
+
from dataset_gen import create_dataset_entry
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
app = Flask(__name__)
|
|
|
|
| 7 |
|
| 8 |
@app.route('/')
|
| 9 |
def index():
|
| 10 |
return render_template('index.html')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
@app.route('/parse', methods=['POST'])
|
| 13 |
+
def parse():
|
| 14 |
+
data = request.get_json()
|
| 15 |
+
code = data.get('code', '')
|
| 16 |
+
if not code.strip():
|
| 17 |
+
return jsonify({"nodes": [], "connections": []})
|
| 18 |
+
|
| 19 |
+
result = parse_source_to_graph(code)
|
| 20 |
+
return jsonify(result)
|
| 21 |
+
|
| 22 |
+
@app.route('/add_to_dataset', methods=['POST'])
|
| 23 |
+
def add_to_dataset():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
data = request.get_json()
|
| 25 |
+
code = data.get('code', '')
|
| 26 |
+
if not code.strip():
|
| 27 |
+
return jsonify({"status": "error", "message": "Empty code"})
|
| 28 |
+
|
| 29 |
+
result = create_dataset_entry(code)
|
| 30 |
+
return jsonify(result)
|
| 31 |
|
| 32 |
if __name__ == '__main__':
|
| 33 |
+
# Running on 0.0.0.0 to be accessible if deployed or in containers
|
| 34 |
app.run(host="0.0.0.0", port=7860, debug=True)
|