Skip to content

Commit 7a16259

Browse files
committed
Add python package
1 parent a47ce96 commit 7a16259

File tree

13 files changed

+1610
-46
lines changed

13 files changed

+1610
-46
lines changed

.vscode/settings.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.codeActionsOnSave": {
4+
"source.fixAll": "always"
5+
},
6+
"[python]": {
7+
"editor.defaultFormatter": "ms-python.black-formatter",
8+
"editor.formatOnSave": true
9+
},
10+
"[json]": {
11+
"editor.defaultFormatter": "vscode.json-language-features"
12+
},
13+
"restoreTerminals.terminals": [
14+
{
15+
"splitTerminals": [
16+
{
17+
"name": "Root"
18+
}
19+
]
20+
},
21+
{
22+
"splitTerminals": [
23+
{
24+
"name": "Python",
25+
"commands": [
26+
"cd python"
27+
]
28+
}
29+
]
30+
},
31+
{
32+
"splitTerminals": [
33+
{
34+
"name": "JS",
35+
"commands": [
36+
"cd js"
37+
]
38+
}
39+
]
40+
}
41+
]
42+
}

README.md

Lines changed: 57 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
The repository contains a template and modules for the code interpreter sandbox. It is based on the Jupyter server and implements the Jupyter Kernel messaging protocol. This allows for sharing context between code executions and improves support for plotting charts and other display-able data.
44

5-
65
## Motivation
76

87
The code generated by LLMs is often split into code blocks, where each subsequent block references the previous one. This is a common pattern in Jupyter notebooks, where each cell can reference the variables and definitions from the previous cells. In the classical sandbox each code execution is independent and does not share the context with the previous executions.
@@ -14,56 +13,63 @@ Additionally, this new template also partly implements the [Jupyter Kernel messa
1413

1514
The current code interpreter allows to run Python code but each run share the context. That means that subsequent runs can reference to variables, definitions, etc from past code execution runs.
1615

17-
1816
## Current state
17+
1918
Known limited in features such as:
19+
2020
- All executions share single kernel
2121

2222
We'll be updating this module as we gather more user feedback.
2323

2424
## Installation
2525

26-
**Python**
26+
### Python
27+
2728
```sh
28-
pip install e2b-code-interpreter
29+
pip install e2b_code_interpreter
2930
```
30-
**JavaScript**
31+
32+
### JavaScript
33+
3134
```sh
32-
npm install e2b-code-interpreter
35+
npm install e2b_code_interpreter
3336
```
3437

3538
## Examples
3639

37-
### Minimal example with the sharing context:
40+
### Minimal example with the sharing context
41+
42+
#### Python
3843

39-
**Python**
4044
```python
4145
from e2b_code_interpreter import CodeInterpreterV2
4246

4347
with CodeInterpreterV2() as sandbox:
44-
sandbox.exec_python("x = 1")
48+
sandbox.exec_cell("x = 1")
4549

46-
result = sandbox.exec_python("x+=1; x")
50+
result = sandbox.exec_cell("x+=1; x")
4751
print(result.output) # outputs 2
48-
49-
```
5052

51-
**JavaScript**
53+
```
54+
55+
#### JavaScript
56+
5257
```js
53-
import { CodeInterpreterV2 } from 'e2b-code-interpreter'
58+
import { CodeInterpreterV2 } from 'e2b_code_interpreter'
5459

5560
const sandbox = await CodeInterpreterV2.create()
5661
await sandbox.execPython('x = 1')
5762

5863
const result = await sandbox.execPython('x+=1; x')
5964
console.log(result.output) # outputs 2
60-
65+
6166
await sandbox.close()
62-
```
67+
```
6368

64-
### Get charts and any display-able data
69+
### Get charts and any display-able data
70+
71+
#### Python
6572

66-
**Python**
6773
```python
6874
import base64
6975
import io
@@ -72,13 +78,12 @@ from matplotlib import image as mpimg, pyplot as plt
7278

7379
from e2b_code_interpreter import CodeInterpreterV2
7480

75-
7681
with CodeInterpreterV2() as sandbox:
77-
# you can install dependencies in "jupyter notebook style"
78-
sandbox.exec_python("!pip install matplotlib")
82+
# you can install dependencies in "jupyter notebook style"
83+
sandbox.exec_cell("!pip install matplotlib")
7984

80-
# plot random graph
81-
result = sandbox.exec_python(
85+
# plot random graph
86+
result = sandbox.exec_cell(
8287
"""
8388
import matplotlib.pyplot as plt
8489
import numpy as np
@@ -103,15 +108,14 @@ with CodeInterpreterV2() as sandbox:
103108
plt.show()
104109
```
105110

111+
#### JavaScript
106112

107-
**JavaScript**
108113
```js
109-
import { CodeInterpreterV2 } from 'e2b-code-interpreter'
110-
111-
const sandbox = await CodeInterpreterV2.create()
114+
import { CodeInterpreterV2 } from "e2b_code_interpreter";
112115

116+
const sandbox = await CodeInterpreterV2.create();
113117

114-
const code = `
118+
const code = `
115119
import matplotlib.pyplot as plt
116120
import numpy as np
117121
@@ -120,57 +124,64 @@ y = np.sin(x)
120124
121125
plt.plot(x, y)
122126
plt.show()
123-
`
127+
`;
124128

125-
// you can install dependencies in "jupyter notebook style"
126-
await sandbox.execPython('!pip install matplotlib')
129+
// you can install dependencies in "jupyter notebook style"
130+
await sandbox.execPython("!pip install matplotlib");
127131

128-
const result = await sandbox.execPython(code)
132+
const result = await sandbox.execPython(code);
129133

130134
// this contains the image data, you can e.g. save it to file or send to frontend
131-
result.display_data[0]['image/png']
132-
133-
await sandbox.close()
135+
result.display_data[0]["image/png"];
136+
137+
await sandbox.close();
134138
```
135139

136140
### Streaming code output
137141

138-
**Python**
142+
#### Python
143+
139144
```python
140145
from e2b_code_interpreter import CodeInterpreterV2
141146

142-
code = """
147+
code = """
143148
import time
144149
145150
print("hello")
146151
time.sleep(5)
147152
print("world")
148153
"""
149154
with CodeInterpreterV2() as sandbox:
150-
sandbox.exec_python(code, on_stdout=print, on_stderr=print)
155+
sandbox.exec_cell(code, on_stdout=print, on_stderr=print)
151156
```
152157

153-
**JavaScript**
158+
#### JavaScript
159+
154160
```js
155-
import { CodeInterpreterV2 } from 'e2b-code-interpreter'
161+
import { CodeInterpreterV2 } from "e2b_code_interpreter";
156162

157-
code = `
163+
code = `
158164
import time
159165
160166
print("hello")
161167
time.sleep(5)
162168
print("world")
163-
`
169+
`;
164170

165-
const sandbox = await CodeInterpreterV2.create()
171+
const sandbox = await CodeInterpreterV2.create();
166172

167-
await sandbox.execPython(code, out => console.log(out), outErr => console.error(outErr))
173+
await sandbox.execPython(
174+
code,
175+
(out) => console.log(out),
176+
(outErr) => console.error(outErr),
177+
);
168178
```
169179

170180
### Pre-installed Python packages inside the sandbox
181+
171182
The full and always up-to-date list can be found in the [`requirements.txt`](https://github.com/e2b-dev/E2B/blob/stateful-code-interpreter/sandboxes/code-interpreter-stateful/requirements.txt) file.
172183

173-
```
184+
```text
174185
# Jupyter server requirements
175186
jupyter-server==2.13.0
176187
ipykernel==6.29.3
@@ -206,4 +217,4 @@ tornado==6.4
206217
urllib3==1.26.7
207218
xarray==2024.2.0
208219
xlrd==2.0.1
209-
```
220+
```

python/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# TODO

python/e2b_code_interpreter/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)