Flask API backend for Open3D Examples visualizer.
Provides endpoints for:
- Listing available examples
- Running example scripts
- Streaming output from running processes
find_script_by_id(example_id)
Find script path by example ID.
Source code in app.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90 | def find_script_by_id(example_id: str) -> Path | None:
"""Find script path by example ID."""
for category_dir in SCRIPT_DIRS:
if not category_dir.exists():
continue
category = category_dir.name
stem = example_id.replace(f'{category}_', '')
script_path = category_dir / f'{stem}.py'
if script_path.exists():
return script_path
return None
|
get_example(example_id)
Get details for a specific example.
Source code in app.py
139
140
141
142
143
144
145
146
147
148 | @app.route('/api/examples/<example_id>', methods=['GET'])
def get_example(example_id: str) -> dict[str, Any]:
"""Get details for a specific example."""
all_examples = get_example_scripts()['examples']
for example in all_examples:
if example['id'] == example_id:
return jsonify(example)
return jsonify({'error': 'Example not found'}), 404
|
get_example_scripts()
Scan directories for Python example scripts.
Source code in app.py
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 | def get_example_scripts() -> dict[str, Any]:
"""Scan directories for Python example scripts."""
examples = []
for category_dir in SCRIPT_DIRS:
if not category_dir.exists():
continue
category = category_dir.name
for script_path in sorted(category_dir.glob('*.py')):
if script_path.name.startswith('_'):
continue
# Parse script for docstring/description
description = parse_script_description(script_path)
examples.append({
'id': f'{category}_{script_path.stem}',
'name': script_path.stem.replace('_', ' ').title(),
'category': category.replace('_', ' ').title(),
'description': description or 'No description available',
'path': str(script_path.relative_to(ROOT)),
})
return {'examples': examples}
|
list_examples()
List all available example scripts.
Source code in app.py
| @app.route('/api/examples', methods=['GET'])
def list_examples() -> dict[str, Any]:
"""List all available example scripts."""
return jsonify(get_example_scripts()['examples'])
|
parse_script_description(script_path)
Extract first docstring from Python script.
Source code in app.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 | def parse_script_description(script_path: Path) -> str:
"""Extract first docstring from Python script."""
try:
with open(script_path, 'r', encoding='utf-8') as f:
content = f.read()
# Simple docstring extraction
for quote in ['"""', "'''"]:
if quote in content:
start = content.find(quote) + len(quote)
end = content.find(quote, start)
if end > start:
doc = content[start:end].strip()
return doc.split('\n')[0][:100]
except Exception:
pass
return ''
|
run_example(example_id)
Execute an example script and return output.
Source code in app.py
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 | @app.route('/api/examples/<example_id>/run', methods=['POST'])
def run_example(example_id: str) -> dict[str, Any]:
"""Execute an example script and return output."""
script_path = find_script_by_id(example_id)
if not script_path:
return jsonify({
'success': False,
'error': f'Example not found: {example_id}'
}), 404
try:
# Run the example script with timeout
result = subprocess.run(
[sys.executable, str(script_path)],
capture_output=True,
text=True,
timeout=30,
cwd=str(script_path.parent)
)
return jsonify({
'success': result.returncode == 0,
'output': result.stdout,
'error': result.stderr if result.returncode != 0 else None
})
except subprocess.TimeoutExpired:
return jsonify({
'success': False,
'error': 'Execution timeout (30 seconds)'
}), 408
except Exception as e:
return jsonify({
'success': False,
'error': f'Execution error: {str(e)}'
}), 500
|
serve_frontend(path)
Serve React frontend files.
Source code in app.py
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170 | @app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def serve_frontend(path: str) -> Any:
"""Serve React frontend files."""
frontend_dist = ROOT / 'frontend' / 'dist'
if not frontend_dist.exists():
return jsonify({
'error': 'Frontend not built. Run: cd frontend && npm install && npm run build'
}), 404
if path and (frontend_dist / path).exists():
return send_from_directory(frontend_dist, path)
# Serve index.html for SPA routing
index_path = frontend_dist / 'index.html'
if index_path.exists():
return send_from_directory(frontend_dist, 'index.html')
return jsonify({'error': 'Frontend not found'}), 404
|