|
1 | | -import dotenv from 'dotenv' |
| 1 | +import { config } from 'dotenv' |
2 | 2 |
|
3 | 3 | import { Sandbox } from './dist' |
4 | 4 |
|
5 | | -dotenv.config() |
| 5 | +function log(...args: any[]) { |
| 6 | + console.log(...args) |
| 7 | +} |
6 | 8 |
|
7 | | -const code = ` |
8 | | -import matplotlib.pyplot as plt |
9 | | -import numpy as np |
| 9 | +config() |
10 | 10 |
|
11 | | -x = np.linspace(0, 20, 100) |
12 | | -y = np.sin(x) |
| 11 | +const sbx = await Sandbox.create('bwyvo5fk343pbvxst536') |
| 12 | +log('ℹ️ sandbox created', sbx.sandboxId) |
13 | 13 |
|
14 | | -plt.plot(x, y) |
15 | | -plt.show() |
| 14 | +await sbx.runCode('x = 1') |
| 15 | +log('Sandbox code executed') |
16 | 16 |
|
17 | | -x = np.linspace(0, 10, 100) |
18 | | -plt.plot(x, y) |
19 | | -plt.show() |
| 17 | +const sandboxId = await sbx.betaPause() |
| 18 | +log('Sandbox paused', sandboxId) |
20 | 19 |
|
21 | | -import pandas |
22 | | -pandas.DataFrame({"a": [1, 2, 3]}) |
23 | | -` |
| 20 | +// Resume the sandbox from the same state |
| 21 | +const sameSbx = await Sandbox.connect(sbx.sandboxId) |
| 22 | +log('Sandbox resumed', sameSbx.sandboxId) |
24 | 23 |
|
25 | | -const sandbox = await Sandbox.create() |
26 | | -console.log(sandbox.sandboxId) |
| 24 | +const execution = await sameSbx.runCode('x+=1; x') |
| 25 | +// Output result |
| 26 | +log(execution.text) |
| 27 | +log(execution.error) |
| 28 | +if (execution.text !== '2') { |
| 29 | + log('Test failed:', 'Failed to resume sandbox') |
| 30 | + throw new Error('Failed to resume sandbox') |
| 31 | +} |
| 32 | +log('Sandbox resumed successfully') |
27 | 33 |
|
28 | | -const execution = await sandbox.runCode(code, { |
29 | | - onStdout(msg) { |
30 | | - console.log('stdout', msg) |
31 | | - }, |
32 | | - onStderr(msg) { |
33 | | - console.log('stderr', msg) |
34 | | - }, |
35 | | -}) |
36 | | -console.log(execution.results[0].formats()) |
37 | | -console.log(execution.results[0].data) |
38 | | -console.log(execution.results.length) |
| 34 | +await sbx.kill() |
| 35 | +log('Sandbox deleted') |
0 commit comments