Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ npm install @e2b/code-interpreter
### Minimal example with the sharing context

```js
import { CodeInterpreter } from '@e2b/code-interpreter'
import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await CodeInterpreter.create()
await sandbox.notebook.execCell('x = 1')
const sandbox = await Sandbox.create()
await sandbox.runCode('x = 1')

const execution = await sandbox.notebook.execCell('x+=1; x')
const execution = await sandbox.runCode('x+=1; x')
console.log(execution.text) // outputs 2

await sandbox.close()
await sandbox.kill()
```

### Get charts and any display-able data

```js
import { CodeInterpreter } from '@e2b/code-interpreter'
import { Sandbox } from '@e2b/code-interpreter'

const sandbox = await CodeInterpreter.create()
const sandbox = await Sandbox.create()

const code = `
import matplotlib.pyplot as plt
Expand All @@ -48,20 +48,20 @@ plt.show()
`

// you can install dependencies in "jupyter notebook style"
await sandbox.notebook.execCell('!pip install matplotlib')
await sandbox.runCode('!pip install matplotlib')

const execution = await sandbox.notebook.execCell(code)
const execution = await sandbox.runCode(code)

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

await sandbox.close()
await sandbox.kill()
```

### Streaming code output

```js
import { CodeInterpreter } from '@e2b/code-interpreter'
import { Sandbox } from '@e2b/code-interpreter'

const code = `
import time
Expand All @@ -75,13 +75,13 @@ time.sleep(3)
print("world")
`

const sandbox = await CodeInterpreter.create()
const sandbox = await Sandbox.create()

await sandbox.notebook.execCell(code, {
await sandbox.runCode(code, {
onStdout: (out) => console.log(out),
onStderr: (outErr) => console.error(outErr),
onResult: (result) => console.log(result.text),
})

await sandbox.close()
await sandbox.kill()
```
6 changes: 3 additions & 3 deletions js/example.mts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dotenv from 'dotenv'

import { CodeInterpreter } from './dist'
import { Sandbox } from './dist'

dotenv.config()

Expand All @@ -22,10 +22,10 @@ import pandas
pandas.DataFrame({"a": [1, 2, 3]})
`

const sandbox = await CodeInterpreter.create()
const sandbox = await Sandbox.create()
console.log(sandbox.sandboxId)

const execution = await sandbox.notebook.execCell(code, {
const execution = await sandbox.runCode(code, {
onStdout(msg) {
console.log('stdout', msg)
},
Expand Down
2 changes: 2 additions & 0 deletions js/src/consts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_TIMEOUT_MS = 60_000 // 1 minute
export const JUPYTER_PORT = 49999
4 changes: 2 additions & 2 deletions js/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export * from 'e2b'

export { Sandbox, JupyterExtension } from './sandbox'

export { Sandbox } from './sandbox'
export type { Context } from './sandbox'
export type {
Logs,
ExecutionError,
Expand Down
Loading
Loading