README.md
  1  # Code Interpreter Sandbox
  2  
  3  Complete demonstration of running Python code using the Code Interpreter SDK.
  4  
  5  ## Getting Code Interpreter image
  6  
  7  Pull the prebuilt image from a registry:
  8  
  9  ```shell
 10  docker pull sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2
 11  
 12  # use docker hub
 13  # docker pull opensandbox/code-interpreter:v1.0.2
 14  ```
 15  
 16  ## Start OpenSandbox server [local]
 17  
 18  Start the local OpenSandbox server:
 19  
 20  ```shell
 21  uv pip install opensandbox-server
 22  opensandbox-server init-config ~/.sandbox.toml --example docker
 23  opensandbox-server
 24  ```
 25  
 26  ## Create and access the Code Interpreter Sandbox
 27  
 28  ```shell
 29  # Install OpenSandbox packages
 30  uv pip install opensandbox opensandbox-code-interpreter
 31  
 32  # Run the example (requires SANDBOX_DOMAIN / SANDBOX_API_KEY)
 33  uv run python examples/code-interpreter/main.py
 34  ```
 35  
 36  The script creates a Sandbox + CodeInterpreter, runs a Python code snippet and prints stdout/result, then terminates the remote instance.
 37  
 38  ## Environment variables
 39  
 40  - `SANDBOX_DOMAIN`: Sandbox service address (default: `localhost:8080`)
 41  - `SANDBOX_API_KEY`: API key if your server requires authentication
 42  - `SANDBOX_IMAGE`: Sandbox image to use (default: `sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2`)
 43  
 44  ## Example output
 45  
 46  ```text
 47  === Python example ===
 48  [Python stdout] Hello from Python!
 49  
 50  [Python result] {'py': '3.14.2', 'sum': 4}
 51  
 52  === Java example ===
 53  [Java stdout] Hello from Java!
 54  
 55  [Java stdout] 2 + 3 = 5
 56  
 57  [Java result] 5
 58  
 59  === Go example ===
 60  [Go stdout] Hello from Go!
 61  3 + 4 = 7
 62  
 63  
 64  === TypeScript example ===
 65  [TypeScript stdout] Hello from TypeScript!
 66  
 67  [TypeScript stdout] sum = 6
 68  ```
 69  
 70  # Code Interpreter Sandbox from pool
 71  
 72  ## Start OpenSandbox server [k8s]
 73  
 74  Install the k8s OpenSandbox operator, and create a pool:
 75  ```yaml
 76  apiVersion: sandbox.opensandbox.io/v1alpha1
 77  kind: Pool
 78  metadata:
 79    labels:
 80      app.kubernetes.io/name: sandbox-k8s
 81      app.kubernetes.io/managed-by: kustomize
 82    name: pool-sample
 83    namespace: opensandbox
 84  spec:
 85    template:
 86      metadata:
 87        labels:
 88          app: example
 89      spec:
 90        volumes:
 91          - name: sandbox-storage
 92            emptyDir: { }
 93          - name: opensandbox-bin
 94            emptyDir: { }
 95        initContainers:
 96          - name: task-executor-installer
 97            image: sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/task-executor:v0.1.0
 98            command: [ "/bin/sh", "-c" ]
 99            args:
100              - |
101                cp /workspace/server /opt/opensandbox/bin/task-executor && 
102                chmod +x /opt/opensandbox/bin/task-executor
103            volumeMounts:
104              - name: opensandbox-bin
105                mountPath: /opt/opensandbox/bin
106          - name: execd-installer
107            image: sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/execd:v1.0.13
108            command: [ "/bin/sh", "-c" ]
109            args:
110              - |
111                cp ./execd /opt/opensandbox/bin/execd && 
112                cp ./bootstrap.sh /opt/opensandbox/bin/bootstrap.sh &&
113                chmod +x /opt/opensandbox/bin/execd &&
114                chmod +x /opt/opensandbox/bin/bootstrap.sh
115            volumeMounts:
116              - name: opensandbox-bin
117                mountPath: /opt/opensandbox/bin
118        containers:
119          - name: sandbox
120            image: sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2
121            command:
122            - "/bin/sh"
123            - "-c"
124            - |
125              /opt/opensandbox/bin/task-executor -listen-addr=0.0.0.0:5758 >/tmp/task-executor.log 2>&1
126            env:
127            - name: SANDBOX_MAIN_CONTAINER
128              value: main
129            - name: EXECD_ENVS
130              value: /opt/opensandbox/.env
131            - name: EXECD
132              value: /opt/opensandbox/bin/execd
133            volumeMounts:
134              - name: sandbox-storage
135                mountPath: /var/lib/sandbox
136              - name: opensandbox-bin
137                mountPath: /opt/opensandbox/bin
138        tolerations:
139          - operator: "Exists"
140    capacitySpec:
141      bufferMax: 3
142      bufferMin: 1
143      poolMax: 5
144      poolMin: 0
145  ```
146  
147  Start the k8s OpenSandbox server:
148  
149  ```shell
150  uv pip install opensandbox-server
151  
152  # replace with your k8s cluster config, kubeconfig etc.
153  opensandbox-server init-config ~/.sandbox.toml --example k8s
154  curl -o ~/batchsandbox-template.yaml https://raw.githubusercontent.com/alibaba/OpenSandbox/main/server/example.batchsandbox-template.yaml
155  
156  opensandbox-server
157  ```
158  
159  ## Create and access the Code Interpreter Sandbox
160  
161  ```shell
162  # Install OpenSandbox packages
163  uv pip install opensandbox opensandbox-code-interpreter
164  
165  # Run the example (requires SANDBOX_DOMAIN / SANDBOX_API_KEY)
166  uv run python examples/code-interpreter/main_use_pool.py
167  ```
168  
169  The script creates a Sandbox + CodeInterpreter, runs a Python code snippet and prints stdout/result, then terminates the remote instance.
170  
171  ## Environment variables
172  
173  - `SANDBOX_DOMAIN`: Sandbox service address (default: `localhost:8080`)
174  - `SANDBOX_API_KEY`: API key if your server requires authentication
175  - `SANDBOX_IMAGE`: Sandbox image to use (default: `sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:v1.0.2`)
176  
177  ## Example output
178  
179  ```text
180  === Verify Environment Variable ===
181  [ENV Check] TEST_ENV value: test
182  
183  [ENV Result] 'test'
184  
185  === Java example ===
186  [Java stdout] Hello from Java!
187  
188  [Java stdout] 2 + 3 = 5
189  
190  [Java result] 5
191  
192  === Go example ===
193  [Go stdout] Hello from Go!
194  3 + 4 = 7
195  
196  
197  === TypeScript example ===
198  [TypeScript stdout] Hello from TypeScript!
199  
200  [TypeScript stdout] sum = 6
201  ```