Using custom sandbox template & custom compute with Code Interpreter SDK

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.

Step-by-step guide

  1. Create custom sandbox by following this guide

  2. Use prebuilt E2B Code Interpreter image by replacing the FROM command in your e2b.Dockerfile with following

    FROM e2bdev/code-interpreter:latest
    
  3. Run the following in the same directory where's your e2b.toml

    e2b template build -c "/root/.jupyter/start-up.sh"
    
  4. Use your custom sandbox with Code Interpreter SDK

    Python

    from e2b_code_interpreter import CodeInterpreter
    sandbox = CodeInterpreter(template="your-custom-sandbox-name")
    execution = sandbox.notebook.exec_cell("print('hello')")
    sandbox.close()
    
    # Or you can use `with` which handles closing the sandbox for you
    with CodeInterpreter(template="your-custom-sandbox-name") as sandbox:
        execution = sandbox.notebook.exec_cell("print('hello')")
    

    JavaScript/TypeScript

    import { CodeInterpreter } from '@e2b/code-interpreter'
    const sandbox = await CodeInterpreter.create({ template: 'your-custom-sandbox-name' })
    const execution = await sandbox.notebook.execCell('print("hello")')
    await sandbox.close()
    

Customize CPU & RAM of your sandbox

You can customize number of CPUs and MiB of RAM for your sandbox. To achieve that, specify the --cpu-count and --memory-mb options during the build step:

e2b template build -c "/home/user/.jupyter/start-up.sh" --cpu-count 4 --memory-mb 4096

The above will create a custom sandbox with 4 CPUs a 4 GiB of RAM.

How to install another Python kernels

Jupyter has ability to work with different than Python kernel. It even supports multiple kernels in one notebook. If you want to install another kernels.

You can find list of available kernels here. Each has a little bit different installation process, but in general you need to install kernel package and register it in jupyter.