-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change from on display data to on result
- Loading branch information
Showing
9 changed files
with
146 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { ProcessMessage } from 'e2b' | ||
import { CodeInterpreter, Result } from '../src' | ||
|
||
import { expect, test } from 'vitest' | ||
|
||
test('streaming output', async () => { | ||
const out: ProcessMessage[] = [] | ||
const sandbox = await CodeInterpreter.create() | ||
await sandbox.notebook.execCell('print(1)', { | ||
onStdout: (msg) => out.push(msg) | ||
}) | ||
|
||
expect(out.length).toEqual(1) | ||
expect(out[0].line).toEqual('1\n') | ||
await sandbox.close() | ||
}) | ||
|
||
test('streaming error', async () => { | ||
const out: ProcessMessage[] = [] | ||
const sandbox = await CodeInterpreter.create() | ||
await sandbox.notebook.execCell('import sys;print(1, file=sys.stderr)', { | ||
onStderr: (msg) => out.push(msg) | ||
}) | ||
|
||
expect(out.length).toEqual(1) | ||
expect(out[0].line).toEqual('1\n') | ||
await sandbox.close() | ||
}) | ||
|
||
test('streaming result', async () => { | ||
const out: Result[] = [] | ||
const sandbox = await CodeInterpreter.create() | ||
const code = ` | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
x = np.linspace(0, 20, 100) | ||
y = np.sin(x) | ||
plt.plot(x, y) | ||
plt.show() | ||
x | ||
` | ||
await sandbox.notebook.execCell(code, { | ||
onResult: (result) => out.push(result) | ||
}) | ||
|
||
expect(out.length).toEqual(2) | ||
await sandbox.close() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from e2b_code_interpreter.main import CodeInterpreter | ||
|
||
|
||
def test_streaming_output(): | ||
out = [] | ||
with CodeInterpreter() as sandbox: | ||
def test(line) -> int: | ||
out.append(line) | ||
return 1 | ||
sandbox.notebook.exec_cell("print(1)", on_stdout=test) | ||
|
||
assert len(out) == 1 | ||
assert out[0].line == "1\n" | ||
|
||
|
||
def test_streaming_error(): | ||
out = [] | ||
with CodeInterpreter() as sandbox: | ||
sandbox.notebook.exec_cell("import sys;print(1, file=sys.stderr)", on_stderr=out.append) | ||
|
||
assert len(out) == 1 | ||
assert out[0].line == "1\n" | ||
|
||
|
||
def test_streaming_result(): | ||
code = """ | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
x = np.linspace(0, 20, 100) | ||
y = np.sin(x) | ||
plt.plot(x, y) | ||
plt.show() | ||
x | ||
""" | ||
|
||
out = [] | ||
with CodeInterpreter() as sandbox: | ||
sandbox.notebook.exec_cell(code, on_result=out.append) | ||
|
||
assert len(out) == 2 | ||
|