Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,7 +34,7 @@ def parse_code():
|
|
| 34 |
|
| 35 |
parts, _ = parse_python_code(code)
|
| 36 |
|
| 37 |
-
# Extract nodes
|
| 38 |
nodes = []
|
| 39 |
connections = []
|
| 40 |
node_id_map = {} # Maps parser node_id to canvas node index
|
|
@@ -45,7 +45,7 @@ def parse_code():
|
|
| 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,
|
|
@@ -58,31 +58,34 @@ def parse_code():
|
|
| 58 |
}
|
| 59 |
|
| 60 |
if category == 'function':
|
| 61 |
-
#
|
| 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
|
| 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']
|
|
@@ -90,17 +93,29 @@ def parse_code():
|
|
| 90 |
|
| 91 |
# Create connections based on parent_path and variable usage
|
| 92 |
for part in parts:
|
| 93 |
-
|
|
|
|
| 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
|
| 101 |
connections.append({'from': from_id, 'to': to_id})
|
| 102 |
-
elif
|
| 103 |
connections.append({'from': to_id, 'to': from_id})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return jsonify({'nodes': nodes, 'connections': connections})
|
| 106 |
|
|
|
|
| 34 |
|
| 35 |
parts, _ = parse_python_code(code)
|
| 36 |
|
| 37 |
+
# Extract nodes and connections
|
| 38 |
nodes = []
|
| 39 |
connections = []
|
| 40 |
node_id_map = {} # Maps parser node_id to canvas node index
|
|
|
|
| 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 = {
|
| 50 |
'id': len(nodes),
|
| 51 |
'type': category,
|
|
|
|
| 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
|
| 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 |
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']
|
|
|
|
| 93 |
|
| 94 |
# Create connections based on parent_path and variable usage
|
| 95 |
for part in parts:
|
| 96 |
+
category = part['category']
|
| 97 |
+
if category in ['input_variable', 'returned_variable', 'assigned_variable']:
|
| 98 |
parent_path = part['parent_path']
|
| 99 |
if 'Function' in parent_path:
|
| 100 |
parent_node_id = parent_path.split(' -> ')[0]
|
| 101 |
if parent_node_id in node_id_map:
|
| 102 |
from_id = node_id_map[part['node_id']]
|
| 103 |
to_id = node_id_map[parent_node_id]
|
| 104 |
+
if category == 'input_variable':
|
| 105 |
connections.append({'from': from_id, 'to': to_id})
|
| 106 |
+
elif category == 'returned_variable':
|
| 107 |
connections.append({'from': to_id, 'to': from_id})
|
| 108 |
+
elif category == 'assigned_variable':
|
| 109 |
+
# Connect assigned variables to parent function (e.g., local variables)
|
| 110 |
+
connections.append({'from': from_id, 'to': to_id})
|
| 111 |
+
elif category == 'import':
|
| 112 |
+
# Connect imports to functions that might use them (simplified: connect to all functions)
|
| 113 |
+
for other_part in parts:
|
| 114 |
+
if other_part['category'] == 'function' and other_part['node_id'] in node_id_map:
|
| 115 |
+
connections.append({
|
| 116 |
+
'from': node_id_map[part['node_id']],
|
| 117 |
+
'to': node_id_map[other_part['node_id']]
|
| 118 |
+
})
|
| 119 |
|
| 120 |
return jsonify({'nodes': nodes, 'connections': connections})
|
| 121 |
|