Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,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 |
|
@@ -25,7 +24,7 @@ def parse_code():
|
|
25 |
file.save(file_path)
|
26 |
with open(file_path, 'r') as f:
|
27 |
code = f.read()
|
28 |
-
os.remove(file_path)
|
29 |
elif 'code' in request.form:
|
30 |
code = request.form['code']
|
31 |
|
@@ -34,16 +33,17 @@ def parse_code():
|
|
34 |
|
35 |
parts, _ = parse_python_code(code)
|
36 |
|
37 |
-
# Extract nodes and connections
|
38 |
nodes = []
|
39 |
connections = []
|
40 |
-
node_id_map = {}
|
41 |
-
|
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', 'import']:
|
49 |
node_data = {
|
@@ -51,14 +51,14 @@ def parse_code():
|
|
51 |
'type': category,
|
52 |
'label': node_id,
|
53 |
'source': source,
|
54 |
-
'x':
|
55 |
-
'y':
|
56 |
'inputs': [],
|
57 |
-
'outputs': []
|
|
|
58 |
}
|
59 |
|
60 |
if category == 'function':
|
61 |
-
# Function: inputs are parameters, output is return
|
62 |
if 'def ' in source:
|
63 |
func_name = source.split('def ')[1].split('(')[0]
|
64 |
node_data['label'] = func_name
|
@@ -67,55 +67,59 @@ def parse_code():
|
|
67 |
node_data['inputs'] = params
|
68 |
node_data['outputs'] = ['return']
|
69 |
elif category == 'input_variable':
|
70 |
-
# Input variable: output to parent function
|
71 |
var_name = source.strip().rstrip(',')
|
72 |
node_data['label'] = var_name
|
73 |
node_data['outputs'] = [var_name]
|
74 |
elif category == 'assigned_variable':
|
75 |
-
# Assigned variable: input is value, output is variable
|
76 |
var_name = source.split('=')[0].strip()
|
77 |
node_data['label'] = var_name
|
78 |
node_data['inputs'] = ['value']
|
79 |
node_data['outputs'] = [var_name]
|
|
|
|
|
80 |
elif category == 'returned_variable':
|
81 |
-
# Returned variable: input from function
|
82 |
var_name = source.split('return ')[1].strip() if 'return ' in source else node_id
|
83 |
node_data['label'] = var_name
|
84 |
node_data['inputs'] = [var_name]
|
85 |
elif category == 'import':
|
86 |
-
# Import: no inputs/outputs, just a node
|
87 |
import_name = source.split('import ')[1].split()[0] if 'import ' in source else node_id
|
88 |
node_data['label'] = import_name
|
89 |
|
90 |
nodes.append(node_data)
|
91 |
node_id_map[node_id] = node_data['id']
|
92 |
-
|
93 |
|
94 |
-
# Create connections based on
|
95 |
for part in parts:
|
96 |
category = part['category']
|
97 |
-
|
98 |
-
|
99 |
-
if '
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
connections.append({
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
return jsonify({'nodes': nodes, 'connections': connections})
|
121 |
|
|
|
7 |
UPLOAD_FOLDER = 'uploads'
|
8 |
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
9 |
|
|
|
10 |
if not os.path.exists(UPLOAD_FOLDER):
|
11 |
os.makedirs(UPLOAD_FOLDER)
|
12 |
|
|
|
24 |
file.save(file_path)
|
25 |
with open(file_path, 'r') as f:
|
26 |
code = f.read()
|
27 |
+
os.remove(file_path)
|
28 |
elif 'code' in request.form:
|
29 |
code = request.form['code']
|
30 |
|
|
|
33 |
|
34 |
parts, _ = parse_python_code(code)
|
35 |
|
|
|
36 |
nodes = []
|
37 |
connections = []
|
38 |
+
node_id_map = {}
|
39 |
+
scope_positions = defaultdict(lambda: {'x': 50, 'y': 50}) # Track positions per scope
|
40 |
|
41 |
for part in parts:
|
42 |
category = part['category']
|
43 |
node_id = part['node_id']
|
44 |
source = part['source'].strip()
|
45 |
+
scope = part['parent_path'].split(' -> ')[0] if ' -> ' in part['parent_path'] else 'global'
|
46 |
+
level = part['level']
|
47 |
|
48 |
if category in ['function', 'assigned_variable', 'input_variable', 'returned_variable', 'import']:
|
49 |
node_data = {
|
|
|
51 |
'type': category,
|
52 |
'label': node_id,
|
53 |
'source': source,
|
54 |
+
'x': scope_positions[scope]['x'] + level * 150, # Indent based on level
|
55 |
+
'y': scope_positions[scope]['y'],
|
56 |
'inputs': [],
|
57 |
+
'outputs': [],
|
58 |
+
'value': part.get('value', None)
|
59 |
}
|
60 |
|
61 |
if category == 'function':
|
|
|
62 |
if 'def ' in source:
|
63 |
func_name = source.split('def ')[1].split('(')[0]
|
64 |
node_data['label'] = func_name
|
|
|
67 |
node_data['inputs'] = params
|
68 |
node_data['outputs'] = ['return']
|
69 |
elif category == 'input_variable':
|
|
|
70 |
var_name = source.strip().rstrip(',')
|
71 |
node_data['label'] = var_name
|
72 |
node_data['outputs'] = [var_name]
|
73 |
elif category == 'assigned_variable':
|
|
|
74 |
var_name = source.split('=')[0].strip()
|
75 |
node_data['label'] = var_name
|
76 |
node_data['inputs'] = ['value']
|
77 |
node_data['outputs'] = [var_name]
|
78 |
+
if node_data['value'] and node_data['value'].isdigit():
|
79 |
+
node_data['type'] = 'number_box'
|
80 |
elif category == 'returned_variable':
|
|
|
81 |
var_name = source.split('return ')[1].strip() if 'return ' in source else node_id
|
82 |
node_data['label'] = var_name
|
83 |
node_data['inputs'] = [var_name]
|
84 |
elif category == 'import':
|
|
|
85 |
import_name = source.split('import ')[1].split()[0] if 'import ' in source else node_id
|
86 |
node_data['label'] = import_name
|
87 |
|
88 |
nodes.append(node_data)
|
89 |
node_id_map[node_id] = node_data['id']
|
90 |
+
scope_positions[scope]['y'] += 100
|
91 |
|
92 |
+
# Create connections based on variable usage
|
93 |
for part in parts:
|
94 |
category = part['category']
|
95 |
+
node_id = part['node_id']
|
96 |
+
if node_id in node_id_map:
|
97 |
+
scope = part['parent_path'].split(' -> ')[0] if ' -> ' in part['parent_path'] else 'global'
|
98 |
+
var_defs = part.get('var_defs', {})
|
99 |
+
var_uses = part.get('var_uses', {})
|
100 |
+
if category == 'assigned_variable':
|
101 |
+
var_name = part['source'].split('=')[0].strip()
|
102 |
+
# Connect to nodes where this variable is used
|
103 |
+
for use_node_id, use_scope in var_uses.get(var_name, []):
|
104 |
+
if use_node_id in node_id_map and use_scope == scope:
|
105 |
+
connections.append({
|
106 |
+
'from': node_id_map[node_id],
|
107 |
+
'to': node_id_map[use_node_id]
|
108 |
+
})
|
109 |
+
elif category == 'function':
|
110 |
+
# Connect function to its input and returned variables
|
111 |
+
for input_part in parts:
|
112 |
+
if input_part['category'] == 'input_variable' and input_part['parent_path'].startswith(node_id):
|
113 |
+
connections.append({
|
114 |
+
'from': node_id_map[input_part['node_id']],
|
115 |
+
'to': node_id_map[node_id]
|
116 |
+
})
|
117 |
+
for return_part in parts:
|
118 |
+
if return_part['category'] == 'returned_variable' and return_part['parent_path'].startswith(node_id):
|
119 |
+
connections.append({
|
120 |
+
'from': node_id_map[node_id],
|
121 |
+
'to': node_id_map[return_part['node_id']]
|
122 |
+
})
|
123 |
|
124 |
return jsonify({'nodes': nodes, 'connections': connections})
|
125 |
|