broadfield-dev commited on
Commit
5c0b458
·
verified ·
1 Parent(s): 1ad8bd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -6
app.py CHANGED
@@ -1,23 +1,115 @@
1
  from flask import Flask, render_template, request, jsonify
2
- import json
 
 
3
 
4
  app = Flask(__name__)
 
 
5
 
6
- # Store nodes and connections in memory (replace with database for persistence)
7
- nodes = []
8
- connections = []
9
 
10
  @app.route('/')
11
  def index():
12
  return render_template('index.html')
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  @app.route('/save_nodes', methods=['POST'])
15
  def save_nodes():
16
- global nodes, connections
17
  data = request.get_json()
18
  nodes = data.get('nodes', [])
19
  connections = data.get('connections', [])
20
  return jsonify({'status': 'success', 'nodes': nodes, 'connections': connections})
21
 
22
  if __name__ == '__main__':
23
- app.run(host="0.0.0.0", port=7860, debug=True)
 
1
  from flask import Flask, render_template, request, jsonify
2
+ from werkzeug.utils import secure_filename
3
+ import os
4
+ from parser import parse_python_code
5
 
6
  app = Flask(__name__)
7
+ UPLOAD_FOLDER = 'uploads'
8
+ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
9
 
10
+ # Ensure upload folder exists
11
+ if not os.path.exists(UPLOAD_FOLDER):
12
+ os.makedirs(UPLOAD_FOLDER)
13
 
14
  @app.route('/')
15
  def index():
16
  return render_template('index.html')
17
 
18
+ @app.route('/parse_code', methods=['POST'])
19
+ def parse_code():
20
+ code = None
21
+ if 'file' in request.files and request.files['file'].filename:
22
+ file = request.files['file']
23
+ filename = secure_filename(file.filename)
24
+ file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
25
+ file.save(file_path)
26
+ with open(file_path, 'r') as f:
27
+ code = f.read()
28
+ os.remove(file_path) # Clean up
29
+ elif 'code' in request.form:
30
+ code = request.form['code']
31
+
32
+ if not code:
33
+ return jsonify({'error': 'No code or file provided'}), 400
34
+
35
+ parts, _ = parse_python_code(code)
36
+
37
+ # Extract nodes (functions and variables) and connections
38
+ nodes = []
39
+ connections = []
40
+ node_id_map = {} # Maps parser node_id to canvas node index
41
+ y_offset = 50 # For vertical layout
42
+
43
+ for part in parts:
44
+ category = part['category']
45
+ node_id = part['node_id']
46
+ source = part['source'].strip()
47
+
48
+ if category in ['function', 'assigned_variable', 'input_variable', 'returned_variable']:
49
+ node_data = {
50
+ 'id': len(nodes),
51
+ 'type': category,
52
+ 'label': node_id,
53
+ 'source': source,
54
+ 'x': 50,
55
+ 'y': y_offset,
56
+ 'inputs': [],
57
+ 'outputs': []
58
+ }
59
+
60
+ if category == 'function':
61
+ # Extract function name and parameters
62
+ if 'def ' in source:
63
+ func_name = source.split('def ')[1].split('(')[0]
64
+ node_data['label'] = func_name
65
+ params = source.split('(')[1].split(')')[0].split(',')
66
+ params = [p.strip() for p in params if p.strip()]
67
+ node_data['inputs'] = params
68
+ # Assume function can have outputs (return statements)
69
+ node_data['outputs'] = ['return']
70
+ elif category == 'input_variable':
71
+ # Input variable (function parameter)
72
+ var_name = source.strip().rstrip(',')
73
+ node_data['label'] = var_name
74
+ node_data['outputs'] = [var_name]
75
+ elif category == 'assigned_variable':
76
+ # Assigned variable
77
+ var_name = source.split('=')[0].strip()
78
+ node_data['label'] = var_name
79
+ node_data['inputs'] = ['value']
80
+ node_data['outputs'] = [var_name]
81
+ elif category == 'returned_variable':
82
+ # Returned variable
83
+ var_name = source.split('return ')[1].strip() if 'return ' in source else node_id
84
+ node_data['label'] = var_name
85
+ node_data['inputs'] = [var_name]
86
+
87
+ nodes.append(node_data)
88
+ node_id_map[node_id] = node_data['id']
89
+ y_offset += 100
90
+
91
+ # Create connections based on parent_path and variable usage
92
+ for part in parts:
93
+ if part['category'] in ['input_variable', 'assigned_variable', 'returned_variable']:
94
+ parent_path = part['parent_path']
95
+ if 'Function' in parent_path:
96
+ parent_node_id = parent_path.split(' -> ')[0]
97
+ if parent_node_id in node_id_map:
98
+ from_id = node_id_map[part['node_id']]
99
+ to_id = node_id_map[parent_node_id]
100
+ if part['category'] == 'input_variable':
101
+ connections.append({'from': from_id, 'to': to_id})
102
+ elif part['category'] == 'returned_variable':
103
+ connections.append({'from': to_id, 'to': from_id})
104
+
105
+ return jsonify({'nodes': nodes, 'connections': connections})
106
+
107
  @app.route('/save_nodes', methods=['POST'])
108
  def save_nodes():
 
109
  data = request.get_json()
110
  nodes = data.get('nodes', [])
111
  connections = data.get('connections', [])
112
  return jsonify({'status': 'success', 'nodes': nodes, 'connections': connections})
113
 
114
  if __name__ == '__main__':
115
+ app.run(debug=True)