|
1 | 1 | # Using custom sandbox with Code Interpreter SDK |
2 | 2 |
|
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). |
5 | 4 |
|
6 | 5 | ## Step-by-step guide |
7 | | -1. Create custom sandbox by following [this guide](https://e2b.dev/docs/sandbox-template) |
8 | 6 |
|
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: |
10 | 24 |
|
11 | | - ```Dockerfile |
12 | | - FROM e2bdev/code-interpreter:latest |
13 | | - ``` |
| 25 | +**build.py** |
14 | 26 |
|
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 |
16 | 31 |
|
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() |
21 | 33 |
|
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 | +``` |
23 | 42 |
|
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: |
30 | 44 |
|
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 | +``` |
36 | 48 |
|
37 | | - **JavaScript/TypeScript** |
| 49 | +4. Use the custom template: |
38 | 50 |
|
39 | | - ```js |
40 | | - import {Sandbox} from '@e2b/code-interpreter' |
| 51 | +```python |
| 52 | +from e2b import Sandbox |
41 | 53 |
|
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 | +``` |
0 commit comments