@@ -39,8 +39,8 @@ from e2b_code_interpreter import CodeInterpreter
39
39
with CodeInterpreter() as sandbox:
40
40
sandbox.notebook.exec_cell(" x = 1" )
41
41
42
- result = sandbox.notebook.exec_cell(" x+=1; x" )
43
- print (result .text) # outputs 2
42
+ execution = sandbox.notebook.exec_cell(" x+=1; x" )
43
+ print (execution .text) # outputs 2
44
44
45
45
```
46
46
@@ -52,8 +52,8 @@ import { CodeInterpreter } from '@e2b/code-interpreter'
52
52
const sandbox = await CodeInterpreter .create ()
53
53
await sandbox .notebook .execCell (' x = 1' )
54
54
55
- const result = await sandbox .notebook .execCell (' x+=1; x' )
56
- console .log (result .text ) // outputs 2
55
+ const execution = await sandbox .notebook .execCell (' x+=1; x' )
56
+ console .log (execution .text ) // outputs 2
57
57
58
58
await sandbox .close ()
59
59
```
@@ -86,10 +86,10 @@ with CodeInterpreter() as sandbox:
86
86
sandbox.notebook.exec_cell(" !pip install matplotlib" )
87
87
88
88
# plot random graph
89
- result = sandbox.notebook.exec_cell(code)
89
+ execution = sandbox.notebook.exec_cell(code)
90
90
91
91
# there's your image
92
- image = result.data [0 ].png
92
+ image = execution.results [0 ].png
93
93
94
94
# example how to show the image / prove it works
95
95
i = base64.b64decode(image)
@@ -121,10 +121,10 @@ plt.show()
121
121
// you can install dependencies in "jupyter notebook style"
122
122
await sandbox .notebook .execCell (" !pip install matplotlib" )
123
123
124
- const result = await sandbox .notebook .execCell (code)
124
+ const execution = await sandbox .notebook .execCell (code)
125
125
126
126
// this contains the image data, you can e.g. save it to file or send to frontend
127
- result . data [0 ].png
127
+ execution . results [0 ].png
128
128
129
129
await sandbox .close ()
130
130
```
@@ -138,35 +138,47 @@ from e2b_code_interpreter import CodeInterpreter
138
138
139
139
code = """
140
140
import time
141
+ import pandas as pd
141
142
142
143
print("hello")
143
- time.sleep(5)
144
+ time.sleep(3)
145
+ data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
146
+ display(data.head(10))
147
+ time.sleep(3)
144
148
print("world")
145
149
"""
150
+
146
151
with CodeInterpreter() as sandbox:
147
- sandbox.notebook.exec_cell(code, on_stdout = print , on_stderr = print )
152
+ sandbox.notebook.exec_cell(code, on_stdout = print , on_stderr = print , on_display_data = (lambda data : print (data.text)))
153
+
148
154
```
149
155
150
156
#### JavaScript
151
157
152
158
``` js
153
- import { CodeInterpreter } from " @e2b/code-interpreter"
159
+ import { CodeInterpreter } from ' @e2b/code-interpreter'
154
160
155
- code = `
161
+ const code = `
156
162
import time
163
+ import pandas as pd
157
164
158
165
print("hello")
159
- time.sleep(5)
166
+ time.sleep(3)
167
+ data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
168
+ display(data.head(10))
169
+ time.sleep(3)
160
170
print("world")
161
171
`
162
172
163
173
const sandbox = await CodeInterpreter .create ()
164
174
165
- await sandbox .notebook .execCell (
166
- code,
167
- (out ) => console .log (out),
168
- (outErr ) => console .error (outErr),
169
- )
175
+ await sandbox .notebook .execCell (code, {
176
+ onStdout : (out ) => console .log (out),
177
+ onStderr : (outErr ) => console .error (outErr),
178
+ onDisplayData : (outData ) => console .log (outData .text )
179
+ })
180
+
181
+ await sandbox .close ()
170
182
```
171
183
172
184
### Pre-installed Python packages inside the sandbox
0 commit comments