Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentaTomas committed Oct 24, 2024
2 parents 12ec3bc + f47d9e6 commit ddf7f28
Show file tree
Hide file tree
Showing 22 changed files with 126 additions and 255 deletions.
5 changes: 0 additions & 5 deletions .changeset/four-yaks-know.md

This file was deleted.

10 changes: 5 additions & 5 deletions .github/workflows/js_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ jobs:
uses: oven-sh/setup-bun@v2
with:
version: 1.1.x
#
# - name: Run Bun tests
# run: pnpm test:bun
# env:
# E2B_API_KEY: ${{ secrets.E2B_API_KEY }}

- name: Run Bun tests
run: pnpm test:bun
env:
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}

- name: Install Deno
uses: denoland/setup-deno@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ jobs:
run: poetry build

- name: Run tests
run: poetry run pytest -n 4 --verbose -x
run: poetry run pytest --verbose -x
env:
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ jobs:

release:
needs: [python-tests, js-tests]
if: always() && !contains(needs.*.result, 'failure') && needs.is_release.outputs.release == 'true'
if: (!cancelled()) && !contains(needs.*.result, 'failure') && needs.is_release.outputs.release == 'true'
name: Release
runs-on: ubuntu-latest
steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img width="100" src="https://github.com/e2b-dev/E2B/blob/main/readme-assets/logo-circle.png" alt="e2b logo">
<img width="100" src="https://raw.githubusercontent.com/e2b-dev/E2B/refs/heads/main/readme-assets/logo-circle.png" alt="e2b logo">
</p>

<h4 align="center">
Expand Down
129 changes: 26 additions & 103 deletions js/README.md
Original file line number Diff line number Diff line change
@@ -1,127 +1,50 @@
<p align="center">
<img width="100" src="/readme-assets/logo-circle.png" alt="e2b logo">
<img width="100" src="https://raw.githubusercontent.com/e2b-dev/E2B/refs/heads/main/readme-assets/logo-circle.png" alt="e2b logo">
</p>


<h1 align="center">
Code interpreter extension for JavaScript
</h1>

<h4 align="center">
<a href="https://pypi.org/project/e2b/">
<img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
style="color:transparent;width:auto;height:100%" src="https://img.shields.io/pypi/dm/e2b?label=PyPI%20Downloads">
</a>
<h4 align="center">
<a href="https://www.npmjs.com/package/e2b">
<img alt="Last 1 month downloads for the Python SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
<img alt="Last 1 month downloads for the JavaScript SDK" loading="lazy" width="200" height="20" decoding="async" data-nimg="1"
style="color:transparent;width:auto;height:100%" src="https://img.shields.io/npm/dm/e2b?label=NPM%20Downloads">
</a>
</h4>

<!---
<img width="100%" src="/readme-assets/preview.png" alt="Cover image">
--->
## What is E2B?
[E2B](https://www.e2b.dev/) is an open-source infrastructure that allows you run to AI-generated code in secure isolated sandboxes in the cloud. To start and control sandboxes, use our [JavaScript SDK](https://www.npmjs.com/package/@e2b/code-interpreter) or [Python SDK](https://pypi.org/project/e2b_code_interpreter).

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.

## Key Features

- **Stateful Execution**: Unlike traditional sandboxes that treat each code execution independently, this package maintains context across executions.
- **Displaying Charts & Data**: Implements parts of the [Jupyter Kernel messaging protocol](https://jupyter-client.readthedocs.io/en/latest/messaging.html), which support for interactive features like plotting charts, rendering DataFrames, etc.
## Run your first Sandbox

## Installation
### 1. Install SDK

```sh
npm install @e2b/code-interpreter
```

## Examples

### Minimal example with the sharing context

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

const sandbox = await Sandbox.create()
await sbx.runCode()('x = 1')

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

await sandbox.close()
npm i @e2b/code-interpreter
```

### Get charts and any display-able data

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

const sandbox = await Sandbox.create()

const 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()
`

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

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.kill()
### 2. Get your E2B API key
1. Sign up to E2B [here](https://e2b.dev).
2. Get your API key [here](https://e2b.dev/dashboard?tab=keys).
3. Set environment variable with your API key.
```
E2B_API_KEY=e2b_***
```

### Streaming code output
### 3. Execute code with code interpreter inside Sandbox

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

const code = `
import time
import pandas as pd
print("hello")
time.sleep(3)
data = pd.DataFrame(data=[[1, 2], [3, 4]], columns=["A", "B"])
display(data.head(10))
time.sleep(3)
print("world")
`

const sandbox = await Sandbox.create()
await sbx.runCode('x = 1')

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

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

### More resources
- Check out the [JavaScript/TypeScript](https://e2b.dev/docs/hello-world/js) and [Python](https://e2b.dev/docs/hello-world/py) "Hello World" guides to learn how to use our SDK.

- See [E2B documentation](https://e2b.dev/docs) to get started.

- Visit our [Cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main) to get inspired by examples with different LLMs and AI frameworks.


___
### 4. Check docs
Visit [E2B documentation](https://e2b.dev/docs).

<div align='center'>
<!-- <a href="https://e2b.dev/docs" target="_blank">
<img src="https://img.shields.io/badge/docs-%2300acee.svg?color=143D52&style=for-the-badge&logo=x&logoColor=white" alt=docs style="margin-bottom: 5px;"/></a> -->
<a href="https://twitter.com/e2b_dev" target="_blank">
<img src="https://img.shields.io/badge/x (twitter)-%2300acee.svg?color=000000&style=for-the-badge&logo=x&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
<a href="https://discord.com/invite/U7KEcGErtQ" target="_blank">
<img src="https://img.shields.io/badge/discord -%2300acee.svg?color=143D52&style=for-the-badge&logo=discord&logoColor=white" alt=discord style="margin-bottom: 5px;"/></a>
<a href="https://www.linkedin.com/company/e2b-dev/" target="_blank">
<img src="https://img.shields.io/badge/linkedin-%2300acee.svg?color=000000&style=for-the-badge&logo=linkedin&logoColor=white" alt=linkedin style="margin-bottom: 5px;"/></a>
</div align='center'>
### 5. E2B cookbook
Visit our [Cookbook](https://github.com/e2b-dev/e2b-cookbook/tree/main) to get inspired by examples with different LLMs and AI frameworks.
2 changes: 1 addition & 1 deletion js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@e2b/code-interpreter",
"version": "1.0.0",
"version": "1.0.3",
"description": "E2B Code Interpreter - Stateful code execution",
"homepage": "https://e2b.dev",
"license": "MIT",
Expand Down
1 change: 1 addition & 0 deletions js/src/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export type BoxAndWhiskerData = {
median: number
third_quartile: number
max: number
outliers: number[]
}

export type BoxAndWhiskerChart = Chart2D & {
Expand Down
2 changes: 2 additions & 0 deletions js/src/messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ export class Result {
'json',
'javascript',
'data',
'chart',
'extra',
"text"
].includes(key)
) {
this.extra[key] = data[key]
Expand Down
18 changes: 7 additions & 11 deletions js/tests/charts/boxAndWhisker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ data = {
# Create figure and axis
fig, ax = plt.subplots(figsize=(10, 6))
# Plot box plot
ax.boxplot(data.values(), labels=data.keys())
# Customize plot
ax.set_title('Exam Scores Distribution')
ax.set_xlabel('Class')
Expand Down Expand Up @@ -52,12 +49,11 @@ plt.show()
const bars = chart.elements
expect(bars.length).toBe(3)

bars.forEach((bar: any) => {
expect(typeof bar.min).toBe('number')
expect(typeof bar.first_quartile).toBe('number')
expect(typeof bar.median).toBe('number')
expect(typeof bar.third_quartile).toBe('number')
expect(typeof bar.max).toBe('number')
expect(typeof bar.label).toBe('string')
})
expect(bars.map((bar) => bar.label)).toEqual(['Class A', 'Class B', 'Class C'])
expect(bars.map((bar) => bar.outliers)).toEqual([[], [76], []])
expect(bars.map((bar) => bar.min)).toEqual([78, 84, 75])
expect(bars.map((bar) => bar.first_quartile)).toEqual([85, 84.75, 79])
expect(bars.map((bar) => bar.median)).toEqual([88, 88, 82])
expect(bars.map((bar) => bar.third_quartile)).toEqual([90, 90.5, 86])
expect(bars.map((bar) => bar.max)).toEqual([92, 95, 88])
})
1 change: 1 addition & 0 deletions js/tests/displayData.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ sandboxTest('display data', async ({ sandbox }) => {
const image = result.results[0]
expect(image.png).toBeDefined()
expect(image.text).toBeDefined()
expect(image.extra).toEqual({})
})
2 changes: 1 addition & 1 deletion js/tests/runtimes/bun/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ test('Bun test', async () => {
} finally {
await sbx.kill()
}
})
}, { timeout: 30_000 })
2 changes: 1 addition & 1 deletion js/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default defineConfig({
include: ['tests/**/*.test.ts'],
exclude: ['tests/runtimes/**'],
globals: false,
testTimeout: 20000,
testTimeout: 30000,
environment: 'node',
bail: 1,
server: {},
Expand Down
Loading

0 comments on commit ddf7f28

Please sign in to comment.