Skip to content

Commit 768b9a0

Browse files
authored
Merge pull request #5 from e2b-dev/prerelease
Prerelease
2 parents 3e911b7 + abe47db commit 768b9a0

23 files changed

+222
-163
lines changed

.changeset/config.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
"changelog": "@changesets/cli/changelog",
44
"commit": false,
55
"fixed": [],
6-
"ignore": [
7-
"e2b-docs",
8-
"reverse-proxy"
9-
],
6+
"ignore": [],
107
"linked": [],
118
"access": "public",
129
"baseBranch": "main",

.changeset/little-paws-ring.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@e2b/code-interpreter": patch
3+
"@e2b/code-interpreter-python": patch
4+
---
5+
6+
Release

.github/workflows/python_tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ jobs:
4040
run: poetry build
4141

4242
- name: Run tests
43-
run: poetry run pytest -n 1 --verbose -x
43+
run: poetry run pytest --verbose -x
4444
env:
4545
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}

.github/workflows/release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,17 @@ jobs:
114114
- name: Install and configure Poetry
115115
uses: snok/install-poetry@v1
116116
with:
117-
version: 1.5.1
117+
version: 1.8.1
118118
virtualenvs-create: true
119119
virtualenvs-in-project: true
120120
installer-parallel: true
121121

122122
- uses: pnpm/action-setup@v2
123123

124-
- name: Setup Node.js 18
124+
- name: Setup Node.js 20
125125
uses: actions/setup-node@v3
126126
with:
127-
node-version: 18
127+
node-version: 20
128128
cache: pnpm
129129

130130
- name: Configure pnpm

.github/workflows/release_candidates.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222

2323
- name: Setup Node.js 20
2424
uses: actions/setup-node@v4
25-
if: ${{ contains( github.event.pull_request.labels.*.name, 'js-rc') || contains( github.event.pull_request.labels.*.name, 'python-rc') }}
25+
if: ${{ contains( github.event.pull_request.labels.*.name, 'js-rc') }}
2626
with:
2727
node-version: 20
2828
registry-url: https://registry.npmjs.org
@@ -43,7 +43,7 @@ jobs:
4343
if: ${{ contains( github.event.pull_request.labels.*.name, 'js-rc') }}
4444
run: |
4545
npm version prerelease --preid=${{ github.head_ref }}
46-
npm publish --tag rc
46+
npm publish --tag rc || true
4747
env:
4848
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
4949

@@ -57,14 +57,14 @@ jobs:
5757
uses: snok/install-poetry@v1
5858
if: ${{ contains( github.event.pull_request.labels.*.name, 'python-rc') }}
5959
with:
60-
version: 1.5.1
60+
version: 1.8.1
6161
virtualenvs-create: true
6262
virtualenvs-in-project: true
6363
installer-parallel: true
6464

6565
- name: Release Candidate
6666
if: ${{ contains( github.event.pull_request.labels.*.name, 'python-rc') }}
67-
working-directory: /python
67+
working-directory: python
6868
run: |
6969
poetry version prerelease
7070
poetry build

README.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ from e2b_code_interpreter import CodeInterpreter
3939
with CodeInterpreter() as sandbox:
4040
sandbox.notebook.exec_cell("x = 1")
4141

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
4444

4545
```
4646

@@ -52,8 +52,8 @@ import { CodeInterpreter } from '@e2b/code-interpreter'
5252
const sandbox = await CodeInterpreter.create()
5353
await sandbox.notebook.execCell('x = 1')
5454

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
5757

5858
await sandbox.close()
5959
```
@@ -86,10 +86,10 @@ with CodeInterpreter() as sandbox:
8686
sandbox.notebook.exec_cell("!pip install matplotlib")
8787

8888
# plot random graph
89-
result = sandbox.notebook.exec_cell(code)
89+
execution = sandbox.notebook.exec_cell(code)
9090

9191
# there's your image
92-
image = result.data[0].png
92+
image = execution.results[0].png
9393

9494
# example how to show the image / prove it works
9595
i = base64.b64decode(image)
@@ -121,10 +121,10 @@ plt.show()
121121
// you can install dependencies in "jupyter notebook style"
122122
await sandbox.notebook.execCell("!pip install matplotlib")
123123

124-
const result = await sandbox.notebook.execCell(code)
124+
const execution = await sandbox.notebook.execCell(code)
125125

126126
// 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
128128

129129
await sandbox.close()
130130
```
@@ -138,35 +138,47 @@ from e2b_code_interpreter import CodeInterpreter
138138

139139
code = """
140140
import time
141+
import pandas as pd
141142
142143
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)
144148
print("world")
145149
"""
150+
146151
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+
148154
```
149155

150156
#### JavaScript
151157

152158
```js
153-
import { CodeInterpreter } from "@e2b/code-interpreter"
159+
import { CodeInterpreter } from '@e2b/code-interpreter'
154160

155-
code = `
161+
const code = `
156162
import time
163+
import pandas as pd
157164
158165
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)
160170
print("world")
161171
`
162172

163173
const sandbox = await CodeInterpreter.create()
164174

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()
170182
```
171183

172184
### Pre-installed Python packages inside the sandbox

js/README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import { CodeInterpreter } from '@e2b/code-interpreter'
2323
const sandbox = await CodeInterpreter.create()
2424
await sandbox.notebook.execCell('x = 1')
2525

26-
const result = await sandbox.notebook.execCell('x += 1; x')
27-
console.log(result.text) // outputs 2
26+
const execution = await sandbox.notebook.execCell('x+=1; x')
27+
console.log(execution.text) // outputs 2
2828

2929
await sandbox.close()
3030
```
@@ -45,15 +45,15 @@ y = np.sin(x)
4545
4646
plt.plot(x, y)
4747
plt.show()
48-
`
48+
`;
4949

5050
// you can install dependencies in "jupyter notebook style"
5151
await sandbox.notebook.execCell("!pip install matplotlib")
5252

53-
const result = await sandbox.notebook.execCell(code)
53+
const execution = await sandbox.notebook.execCell(code)
5454

5555
// this contains the image data, you can e.g. save it to file or send to frontend
56-
result.data[0].png
56+
execution.results[0].png
5757

5858
await sandbox.close()
5959
```
@@ -63,21 +63,27 @@ await sandbox.close()
6363
```js
6464
import { CodeInterpreter } from '@e2b/code-interpreter'
6565

66-
code = `
66+
const code = `
6767
import time
68+
import pandas as pd
6869
6970
print("hello")
70-
time.sleep(5)
71+
time.sleep(3)
72+
data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
73+
display(data.head(10))
74+
time.sleep(3)
7175
print("world")
72-
`;
76+
`
7377

7478
const sandbox = await CodeInterpreter.create()
7579

76-
await sandbox.notebook.execCell(
77-
code,
78-
(out) => console.log(out),
79-
(outErr) => console.error(outErr),
80-
)
80+
await sandbox.notebook.execCell(code, {
81+
onStdout: (out) => console.log(out),
82+
onStderr: (outErr) => console.error(outErr),
83+
onDisplayData: (outData) => console.log(outData.text)
84+
})
85+
86+
await sandbox.close()
8187
```
8288

8389
### Pre-installed Python packages inside the sandbox

js/example.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
const { CodeInterpreter } = require('./dist')
22
const dotenv = require('dotenv')
3-
dotenv.config();
3+
dotenv.config()
44

55
async function main() {
6-
const sandbox = await CodeInterpreter.create()
7-
const r =await sandbox.notebook.execCell('x = 1; x')
8-
console.log(r)
6+
const sandbox = await CodeInterpreter.create()
7+
const r = await sandbox.notebook.execCell('x = 1; x')
8+
console.log(r)
99
}
1010

1111
main().catch(console.error)
12-

js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@e2b/code-interpreter",
3-
"version": "0.0.0",
3+
"version": "0.0.1-prerelease.2",
44
"description": "E2B Code Interpreter - Stateful code execution",
55
"homepage": "https://e2b.dev",
66
"license": "MIT",

js/src/code-interpreter.ts

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ProcessMessage, Sandbox, SandboxOpts } from 'e2b'
2-
import {Data, JupyterKernelWebSocket, Result} from './messaging'
2+
import { Result, JupyterKernelWebSocket, Execution } from './messaging'
33
import { createDeferredPromise } from './utils'
44

55
interface Kernels {
@@ -46,10 +46,12 @@ export class JupyterExtension {
4646
return this.kernelIDPromise.promise
4747
}
4848

49-
constructor(private sandbox: CodeInterpreter) { }
49+
constructor(private sandbox: CodeInterpreter) {}
5050

5151
async connect(timeout?: number) {
52-
return this.startConnectingToDefaultKernel(this.setDefaultKernelID, { timeout })
52+
return this.startConnectingToDefaultKernel(this.setDefaultKernelID, {
53+
timeout
54+
})
5355
}
5456

5557
/**
@@ -61,24 +63,43 @@ export class JupyterExtension {
6163
* @param kernelID The ID of the kernel to execute the code on. If not provided, the default kernel is used.
6264
* @param onStdout A callback function to handle standard output messages from the code execution.
6365
* @param onStderr A callback function to handle standard error messages from the code execution.
66+
* @param onDisplayData A callback function to handle display data messages from the code execution.
67+
* @param timeout The maximum time to wait for the code execution to complete, in milliseconds.
6468
* @returns A promise that resolves with the result of the code execution.
6569
*/
6670
async execCell(
6771
code: string,
68-
kernelID?: string,
69-
onStdout?: (msg: ProcessMessage) => any,
70-
onStderr?: (msg: ProcessMessage) => any,
71-
onDisplayData?: (data: Data) => any
72-
): Promise<Result> {
73-
kernelID = kernelID || await this.defaultKernelID
74-
const ws = this.connectedKernels[kernelID] || await this.connectToKernelWS(kernelID)
75-
76-
return await ws.sendExecutionMessage(code, onStdout, onStderr, onDisplayData)
72+
{
73+
kernelID,
74+
onStdout,
75+
onStderr,
76+
onDisplayData,
77+
timeout
78+
}: {
79+
kernelID?: string
80+
onStdout?: (msg: ProcessMessage) => Promise<void> | void
81+
onStderr?: (msg: ProcessMessage) => Promise<void> | void
82+
onDisplayData?: (data: Result) => Promise<void> | void
83+
timeout?: number
84+
} = {}
85+
): Promise<Execution> {
86+
kernelID = kernelID || (await this.defaultKernelID)
87+
const ws =
88+
this.connectedKernels[kernelID] ||
89+
(await this.connectToKernelWS(kernelID))
90+
91+
return await ws.sendExecutionMessage(
92+
code,
93+
onStdout,
94+
onStderr,
95+
onDisplayData,
96+
timeout
97+
)
7798
}
7899

79100
private async startConnectingToDefaultKernel(
80101
resolve: (value: string) => void,
81-
opts?: { timeout?: number },
102+
opts?: { timeout?: number }
82103
) {
83104
const kernelID = (
84105
await this.sandbox.filesystem.read('/root/.jupyter/kernel_id', opts)

0 commit comments

Comments
 (0)