forked from e2b-dev/code-interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_streaming.py
More file actions
47 lines (32 loc) · 952 Bytes
/
test_streaming.py
File metadata and controls
47 lines (32 loc) · 952 Bytes
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
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