Skip to content

Commit 31e8d47

Browse files
authored
Upgrade Template to new SDK (#166)
* initial commit * cursor suggestions * updated * fix template * fmt * removed unused env vars * updated ci * python build ci test * dependencies * removed dotenv from ci * add capture for template id, build id * fixes java test, template name * lint * added changeset * readded cleanup * updated release CI * updated dockerhub push tag * updated readme * remove poetry from template build ci * generate template alias before test run * updated workflow * updated generated template alias for clarity * updated output * removed unused build_id output * corrected outputs * pin r, java, deno versions * install java from source, keep java 11 * make sure python & js kernels always installed * fixes java home * make kernels set
1 parent 053bb21 commit 31e8d47

File tree

17 files changed

+273
-219
lines changed

17 files changed

+273
-219
lines changed

.changeset/empty-wasps-count.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@e2b/code-interpreter-template': minor
3+
---
4+
5+
updated template to new SDK

.github/workflows/build_test_template.yml

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Build Template
33
on:
44
workflow_call:
55
secrets:
6-
E2B_TESTS_ACCESS_TOKEN:
6+
E2B_API_KEY:
77
required: true
88
inputs:
99
E2B_DOMAIN:
@@ -22,7 +22,7 @@ jobs:
2222
name: Build E2B Template
2323
runs-on: ubuntu-latest
2424
outputs:
25-
template_id: ${{ steps.build-template.outputs.template_id }}
25+
template_id: ${{ steps.generate-template-id.outputs.template_id }}
2626
steps:
2727
- name: Checkout repository
2828
uses: actions/checkout@v4
@@ -34,22 +34,27 @@ jobs:
3434
echo "Version: $VERSION"
3535
sed -i "s/e2b_charts/e2b_charts==${VERSION}/g" requirements.txt
3636
37-
- name: Install E2B CLI
38-
run: npm install -g @e2b/cli
37+
- uses: actions/setup-python@v6
38+
with:
39+
python-version: '3.13'
40+
41+
- name: Install development dependencies
42+
working-directory: ./template
43+
run: pip install -r requirements-dev.txt
44+
45+
- name: Generate Template ID
46+
id: generate-template-id
47+
run: |
48+
E2B_TESTS_TEMPLATE=e2b-code-interpreter-ci-$(uuidgen)
49+
echo "Generated Template ID: $E2B_TESTS_TEMPLATE"
50+
echo "template_id=$E2B_TESTS_TEMPLATE" >> $GITHUB_OUTPUT
3951
4052
- name: Build E2B template
4153
id: build-template
42-
run: |
43-
rm -f e2b.toml
44-
e2b template build --memory-mb 1024 -c "/root/.jupyter/start-up.sh" -d "Dockerfile"
45-
TEMPLATE_ID=$(grep "template_id" e2b.toml | cut -d '"' -f 2)
46-
echo "Captured Template ID: $TEMPLATE_ID"
47-
echo "template_id=$TEMPLATE_ID" >> $GITHUB_OUTPUT
4854
working-directory: ./template
55+
run: |
56+
python build_ci.py
4957
env:
50-
E2B_ACCESS_TOKEN: ${{ secrets.E2B_TESTS_ACCESS_TOKEN }}
58+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
5159
E2B_DOMAIN: ${{ inputs.E2B_DOMAIN }}
52-
53-
- name: Output template ID
54-
run: |
55-
echo "Template ID from step output: ${{ steps.build-template.outputs.template_id }}"
60+
E2B_TESTS_TEMPLATE: ${{ steps.generate-template-id.outputs.template_id }}

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
build-template:
1818
uses: ./.github/workflows/build_test_template.yml
1919
secrets:
20-
E2B_TESTS_ACCESS_TOKEN: ${{ secrets.E2B_TESTS_ACCESS_TOKEN }}
20+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
2121
with:
2222
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
2323
js-sdk:

.github/workflows/release.yml

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ jobs:
200200
201201
sed -i "s/e2b_charts/e2b_charts==${VERSION}/g" requirements.txt
202202
203-
- name: Build Docker image
204-
uses: docker/build-push-action@v6
205-
with:
206-
context: ./template
207-
push: true
208-
platforms: linux/amd64
209-
tags: ${{ secrets.DOCKERHUB_USERNAME }}/code-interpreter:latest
203+
- name: Build and push to DockerHub
204+
working-directory: ./template
205+
run: |
206+
python build_docker.py | docker buildx build \
207+
--platform linux/amd64 \
208+
--push \
209+
--tag ${{ secrets.DOCKERHUB_USERNAME }}/code-interpreter:latest -f - .
210210
211211
build-template:
212212
name: Build E2B template
@@ -219,14 +219,22 @@ jobs:
219219
steps:
220220
- name: Checkout repository
221221
uses: actions/checkout@v4
222-
- name: Install E2B CLI
223-
run: npm install -g @e2b/cli
222+
- uses: actions/setup-python@v6
223+
with:
224+
python-version: '3.13'
224225

225-
- name: Build e2b
226-
run: e2b template build
226+
- name: Install development dependencies
227227
working-directory: ./template
228+
run: pip install -r requirements-dev.txt
229+
230+
- name: Build E2B template
231+
id: build-template
232+
working-directory: ./template
233+
run: |
234+
python build_prod.py
228235
env:
229-
E2B_ACCESS_TOKEN: ${{ secrets.E2B_ACCESS_TOKEN }}
236+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
237+
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
230238

231239
python-tests:
232240
name: Python Tests

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
start-template-server:
2-
docker run --rm -e E2B_LOCAL=true -p 49999:49999 -it $$(docker build . -q -f ./template/test.Dockerfile)
2+
docker run --rm -e E2B_LOCAL=true -p 49999:49999 -it $$(python template/build_docker.py | docker build -q ./template -f -)
33

44
kill-template-server:
55
docker kill $(shell docker ps --filter expose=49999 --format {{.ID}})

template/Dockerfile

Lines changed: 0 additions & 74 deletions
This file was deleted.

template/README.md

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,57 @@
11
# Using custom sandbox with Code Interpreter SDK
22

3-
If you want to customize the Code Interprerter sandbox (e.g.: add a preinstalled package) you can do that by using a [custom sandbox template](https://e2b.dev/docs/sandbox-template).
4-
3+
If you want to customize the Code Interpreter sandbox (e.g.: add a preinstalled package) you can do that by creating a [custom sandbox template](https://e2b.dev/docs/template/quickstart).
54

65
## Step-by-step guide
7-
1. Create custom sandbox by following [this guide](https://e2b.dev/docs/sandbox-template)
86

9-
2. Use prebuilt [E2B Code Interpreter image](https://hub.docker.com/r/e2bdev/code-interpreter) by replacing the `FROM` command in your `e2b.Dockerfile` with following
7+
1. Install E2B SDK
8+
9+
```
10+
pip install e2b dotenv
11+
```
12+
13+
2. Create a custom sandbox template:
14+
15+
**template.py**
16+
17+
```python
18+
from e2b import Template
19+
20+
template = Template().from_template("code-interpreter-v1")
21+
```
22+
23+
3. Create a build script:
1024

11-
```Dockerfile
12-
FROM e2bdev/code-interpreter:latest
13-
```
25+
**build.py**
1426

15-
3. Copy [`start-up.sh`](./start-up.sh) to the same directory where's your `e2b.toml`
27+
```python
28+
from dotenv import load_dotenv
29+
from .template import template
30+
from e2b import Template, default_build_logger
1631

17-
4. Run the following in the same directory where's your `e2b.toml`
18-
```sh
19-
e2b template build -c "/root/.jupyter/start-up.sh"
20-
```
32+
load_dotenv()
2133

22-
5. Use your custom sandbox with Code Interpreter SDK
34+
Template.build(
35+
template,
36+
alias="code-interpreter-custom",
37+
cpu_count=2,
38+
memory_mb=2048,
39+
on_build_logs=default_build_logger(),
40+
)
41+
```
2342

24-
**Python**
25-
```python
26-
from e2b_code_interpreter import Sandbox
27-
sandbox = Sandbox.create(template="your-custom-sandbox-name")
28-
execution = sandbox.run_code("print('hello')")
29-
sandbox.kill()
43+
3. Build the template:
3044

31-
# Or you can use `with` which handles closing the sandbox for you
32-
with Sandbox.create(template="your-custom-sandbox-name") as sandbox:
33-
execution = sandbox.run_code("print('hello')")
34-
```
35-
45+
```
46+
python build.py
47+
```
3648

37-
**JavaScript/TypeScript**
49+
4. Use the custom template:
3850

39-
```js
40-
import {Sandbox} from '@e2b/code-interpreter'
51+
```python
52+
from e2b import Sandbox
4153

42-
const sandbox = await Sandbox.create({template: 'your-custom-sandbox-name'})
43-
const execution = await sandbox.runCode('print("hello")')
44-
await sandbox.kill()
45-
```
54+
sbx = Sandbox.create(template="code-interpreter-custom")
55+
execution = sbx.run_code("print('Hello, World!')")
56+
print(execution.logs.stdout)
57+
```

template/build_ci.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import os
2+
from e2b import Template, default_build_logger
3+
from template import make_template
4+
5+
Template.build(
6+
make_template(set_user_workdir=True),
7+
alias=os.environ["E2B_TESTS_TEMPLATE"],
8+
cpu_count=2,
9+
memory_mb=2048,
10+
on_build_logs=default_build_logger(),
11+
)

template/build_docker.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from template import make_template
2+
from e2b import Template
3+
4+
tmp = make_template(kernels=["python", "javascript"])
5+
print(Template.to_dockerfile(tmp))

template/build_prod.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from dotenv import load_dotenv
2+
from e2b import Template, default_build_logger
3+
from template import make_template
4+
5+
load_dotenv()
6+
7+
Template.build(
8+
make_template(set_user_workdir=True),
9+
alias="code-interpreter-v1",
10+
cpu_count=2,
11+
memory_mb=2048,
12+
on_build_logs=default_build_logger(),
13+
)

0 commit comments

Comments
 (0)