-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython-example.ts
More file actions
60 lines (53 loc) · 1.57 KB
/
python-example.ts
File metadata and controls
60 lines (53 loc) · 1.57 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
import { ExecutionEngine, ContainerStrategy, Language } from '../src';
import * as path from 'path';
async function main() {
const engine = new ExecutionEngine();
let sessionId: string | undefined = ''
try {
// Create a session with per-session strategy
sessionId = await engine.createSession({
strategy: ContainerStrategy.PER_SESSION,
containerConfig: {
image: 'python:3.9-slim',
mounts: [
{
type: 'directory',
source: path.join(__dirname, 'data'),
target: '/app/data'
}
],
environment: {
PYTHONUNBUFFERED: '1'
}
}
});
// Execute Python code that reads from mounted directory
const result = await engine.executeCode(sessionId, {
language: 'python',
code: `import os
import json
from pathlib import Path
# Read data from mounted directory
data_dir = Path('/app/data')
for file in data_dir.glob('*.json'):
with open(file) as f:
data = json.load(f)
print(f'Processing {file.name}:')
print(json.dumps(data, indent=2))`,
dependencies: []
});
console.log('Execution Result:');
console.log('STDOUT:', result.stdout);
console.log('STDERR:', result.stderr);
console.log('Exit Code:', result.exitCode);
console.log('Execution Time:', result.executionTime, 'ms');
console.log('Container Workspace folder:', result.workspaceDir);
} catch (error) {
console.error('Error:', error);
} finally {
if (sessionId) {
await engine.cleanupSession(sessionId);
}
}
}
main();