Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
101 lines (82 loc) · 2.73 KB
/
Copy pathapi.py
File metadata and controls
101 lines (82 loc) · 2.73 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python3
"""
Simple Flask API for Node Editor components
Serves component data from SQLite database
"""
import sqlite3
import json
from flask import Flask, jsonify
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # Enable CORS for browser access
DATABASE = 'components.db'
def get_db():
"""Connect to SQLite database"""
conn = sqlite3.connect(DATABASE)
conn.row_factory = sqlite3.Row # Return rows as dictionaries
return conn
def init_db():
"""Initialize database from schema file"""
conn = sqlite3.connect(DATABASE)
with open('components.db.sql', 'r') as f:
conn.executescript(f.read())
conn.commit()
conn.close()
print(f"Database initialized: {DATABASE}")
@app.route('/api/components', methods=['GET'])
def get_components():
"""Get all components with their parameters"""
conn = get_db()
cursor = conn.cursor()
# Get all components
cursor.execute('SELECT id, name FROM components ORDER BY name')
components = []
for row in cursor.fetchall():
component_id = row['id']
component_name = row['name']
# Get parameters for this component
cursor.execute('''
SELECT name, datatype, default_value, description, options
FROM parameters
WHERE component_id = ?
ORDER BY id
''', (component_id,))
parameters = []
for param_row in cursor.fetchall():
param = {
'name': param_row['name'],
'datatype': param_row['datatype'],
'defaultValue': parse_default_value(param_row['default_value'], param_row['datatype']),
'description': param_row['description']
}
# Add options for string type if available
if param_row['options']:
param['options'] = json.loads(param_row['options'])
parameters.append(param)
components.append({
'name': component_name,
'parameters': parameters
})
conn.close()
return jsonify(components)
def parse_default_value(value, datatype):
"""Parse default value based on datatype"""
if value is None:
return None
if datatype == 'int':
return int(value)
elif datatype == 'float':
return float(value)
elif datatype == 'boolean':
return value.lower() == 'true'
else:
return value
if __name__ == '__main__':
import os
# Initialize database if it doesn't exist
if not os.path.exists(DATABASE):
print("Creating database...")
init_db()
print("Starting API server on http://localhost:5000")
print("API endpoint: http://localhost:5000/api/components")
app.run(debug=True, port=5000)