code-sandboxes 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- code_sandboxes/__init__.py +141 -0
- code_sandboxes/__version__.py +6 -0
- code_sandboxes/base.py +572 -0
- code_sandboxes/commands.py +452 -0
- code_sandboxes/exceptions.py +101 -0
- code_sandboxes/filesystem.py +500 -0
- code_sandboxes/local/__init__.py +9 -0
- code_sandboxes/local/eval_sandbox.py +309 -0
- code_sandboxes/models.py +392 -0
- code_sandboxes/remote/__init__.py +9 -0
- code_sandboxes/remote/datalayer_sandbox.py +627 -0
- code_sandboxes-0.0.2.dist-info/METADATA +299 -0
- code_sandboxes-0.0.2.dist-info/RECORD +15 -0
- code_sandboxes-0.0.2.dist-info/WHEEL +4 -0
- code_sandboxes-0.0.2.dist-info/licenses/LICENSE +29 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: code_sandboxes
|
|
3
|
+
Version: 0.0.2
|
|
4
|
+
Project-URL: Home, https://github.com/datalayer/code-sandboxes
|
|
5
|
+
Author-email: Datalayer <info@datalayer.io>
|
|
6
|
+
License: BSD 3-Clause License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2025, Datalayer
|
|
9
|
+
All rights reserved.
|
|
10
|
+
|
|
11
|
+
Redistribution and use in source and binary forms, with or without
|
|
12
|
+
modification, are permitted provided that the following conditions are met:
|
|
13
|
+
|
|
14
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
15
|
+
list of conditions and the following disclaimer.
|
|
16
|
+
|
|
17
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
18
|
+
this list of conditions and the following disclaimer in the documentation
|
|
19
|
+
and/or other materials provided with the distribution.
|
|
20
|
+
|
|
21
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
22
|
+
contributors may be used to endorse or promote products derived from
|
|
23
|
+
this software without specific prior written permission.
|
|
24
|
+
|
|
25
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
26
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
27
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
28
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
29
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
30
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
31
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
32
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
33
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
34
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
35
|
+
License-File: LICENSE
|
|
36
|
+
Keywords: AI,AI Agent,Code Sandboxes
|
|
37
|
+
Classifier: Intended Audience :: Developers
|
|
38
|
+
Classifier: Intended Audience :: System Administrators
|
|
39
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
40
|
+
Classifier: Programming Language :: Python
|
|
41
|
+
Classifier: Programming Language :: Python :: 3
|
|
42
|
+
Requires-Python: >=3.10
|
|
43
|
+
Provides-Extra: all
|
|
44
|
+
Requires-Dist: datalayer-core; extra == 'all'
|
|
45
|
+
Requires-Dist: docker>=6.0; extra == 'all'
|
|
46
|
+
Provides-Extra: datalayer
|
|
47
|
+
Requires-Dist: datalayer-core; extra == 'datalayer'
|
|
48
|
+
Provides-Extra: docker
|
|
49
|
+
Requires-Dist: docker>=6.0; extra == 'docker'
|
|
50
|
+
Provides-Extra: lint
|
|
51
|
+
Requires-Dist: mdformat-gfm>=0.3.5; extra == 'lint'
|
|
52
|
+
Requires-Dist: mdformat>0.7; extra == 'lint'
|
|
53
|
+
Requires-Dist: ruff; extra == 'lint'
|
|
54
|
+
Provides-Extra: test
|
|
55
|
+
Requires-Dist: ipykernel; extra == 'test'
|
|
56
|
+
Requires-Dist: jupyter-server<3,>=1.6; extra == 'test'
|
|
57
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'test'
|
|
58
|
+
Requires-Dist: pytest>=7.0; extra == 'test'
|
|
59
|
+
Provides-Extra: typing
|
|
60
|
+
Requires-Dist: mypy>=0.990; extra == 'typing'
|
|
61
|
+
Description-Content-Type: text/markdown
|
|
62
|
+
|
|
63
|
+
<!--
|
|
64
|
+
~ Copyright (c) 2025-2026 Datalayer, Inc.
|
|
65
|
+
~
|
|
66
|
+
~ BSD 3-Clause License
|
|
67
|
+
-->
|
|
68
|
+
|
|
69
|
+
[](https://datalayer.io)
|
|
70
|
+
|
|
71
|
+
[](https://github.com/sponsors/datalayer)
|
|
72
|
+
|
|
73
|
+
# { } Code Sandboxes
|
|
74
|
+
|
|
75
|
+
[](https://github.com/datalayer/code-sandboxes/actions/workflows/build.yml)
|
|
76
|
+
[](https://pypi.org/project/code-sandboxes)
|
|
77
|
+
|
|
78
|
+
Code Sandboxes `code_sandboxes` is a Python package for safe, isolated environments where an AI system can write, run, and test code without affecting the real world or the user's device.
|
|
79
|
+
|
|
80
|
+
This package provides a unified API for code execution with features like:
|
|
81
|
+
|
|
82
|
+
- **Code Execution**: Execute Python code with streaming output and rich results
|
|
83
|
+
- **Filesystem Operations**: Read, write, list, upload, and download files
|
|
84
|
+
- **Command Execution**: Run shell commands with streaming support
|
|
85
|
+
- **Context Management**: Maintain state across multiple executions
|
|
86
|
+
- **Snapshots**: Save and restore sandbox state (Datalayer runtime)
|
|
87
|
+
- **GPU Support**: Access GPU compute for ML workloads (Datalayer runtime)
|
|
88
|
+
|
|
89
|
+
## Sandbox Variants
|
|
90
|
+
|
|
91
|
+
Three variants are available:
|
|
92
|
+
|
|
93
|
+
| Variant | Isolation | Use Case |
|
|
94
|
+
|---------|-----------|----------|
|
|
95
|
+
| `local-eval` | None (Python exec) | Development, testing |
|
|
96
|
+
| `local-docker` | Container | Local isolated execution |
|
|
97
|
+
| `datalayer-runtime` | Cloud VM | Production, GPU workloads |
|
|
98
|
+
|
|
99
|
+
## Installation
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
# Basic installation
|
|
103
|
+
pip install code-sandboxes
|
|
104
|
+
|
|
105
|
+
# With Datalayer runtime support
|
|
106
|
+
pip install code-sandboxes[datalayer]
|
|
107
|
+
|
|
108
|
+
# With Docker support
|
|
109
|
+
pip install code-sandboxes[docker]
|
|
110
|
+
|
|
111
|
+
# All features
|
|
112
|
+
pip install code-sandboxes[all]
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Quick Start
|
|
116
|
+
|
|
117
|
+
### Simple Code Execution
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
from code_sandboxes import Sandbox
|
|
121
|
+
|
|
122
|
+
# Create a sandbox with timeout
|
|
123
|
+
with Sandbox.create(variant="local-eval", timeout=60) as sandbox:
|
|
124
|
+
# Execute code
|
|
125
|
+
result = sandbox.run_code("x = 1 + 1")
|
|
126
|
+
result = sandbox.run_code("print(x)") # prints 2
|
|
127
|
+
|
|
128
|
+
# Access results
|
|
129
|
+
print(result.stdout) # "2"
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### Cloud Execution with GPU
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
from code_sandboxes import Sandbox
|
|
136
|
+
|
|
137
|
+
# Create a cloud sandbox with GPU
|
|
138
|
+
with Sandbox.create(
|
|
139
|
+
variant="datalayer-runtime",
|
|
140
|
+
gpu="T4",
|
|
141
|
+
environment="python-gpu-env",
|
|
142
|
+
timeout=300,
|
|
143
|
+
) as sandbox:
|
|
144
|
+
sandbox.run_code("import torch")
|
|
145
|
+
result = sandbox.run_code("print(torch.cuda.is_available())")
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### Filesystem Operations
|
|
149
|
+
|
|
150
|
+
```python
|
|
151
|
+
with Sandbox.create() as sandbox:
|
|
152
|
+
# Write files
|
|
153
|
+
sandbox.files.write("/data/test.txt", "Hello World")
|
|
154
|
+
|
|
155
|
+
# Read files
|
|
156
|
+
content = sandbox.files.read("/data/test.txt")
|
|
157
|
+
|
|
158
|
+
# List directory
|
|
159
|
+
for f in sandbox.files.list("/data"):
|
|
160
|
+
print(f.name, f.size)
|
|
161
|
+
|
|
162
|
+
# Upload/download
|
|
163
|
+
sandbox.files.upload("local_file.txt", "/remote/file.txt")
|
|
164
|
+
sandbox.files.download("/remote/file.txt", "downloaded.txt")
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Command Execution
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
with Sandbox.create() as sandbox:
|
|
171
|
+
# Run a command and wait for completion
|
|
172
|
+
result = sandbox.commands.run("ls -la")
|
|
173
|
+
print(result.stdout)
|
|
174
|
+
|
|
175
|
+
# Execute with streaming output
|
|
176
|
+
process = sandbox.commands.exec("python", "-c", "print('hello')")
|
|
177
|
+
for line in process.stdout:
|
|
178
|
+
print(line, end="")
|
|
179
|
+
|
|
180
|
+
# Install system packages
|
|
181
|
+
sandbox.commands.install_system_packages(["curl", "wget"])
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Snapshots (Datalayer Runtime)
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
with Sandbox.create(variant="datalayer-runtime") as sandbox:
|
|
188
|
+
# Set up environment
|
|
189
|
+
sandbox.install_packages(["pandas", "numpy"])
|
|
190
|
+
sandbox.run_code("import pandas as pd; df = pd.DataFrame({'a': [1,2,3]})")
|
|
191
|
+
|
|
192
|
+
# Create snapshot
|
|
193
|
+
snapshot = sandbox.create_snapshot("my-setup")
|
|
194
|
+
print(f"Snapshot created: {snapshot.id}")
|
|
195
|
+
|
|
196
|
+
# Later: restore from snapshot
|
|
197
|
+
with Sandbox.create(variant="datalayer-runtime", snapshot_name="my-setup") as sandbox:
|
|
198
|
+
# State is restored
|
|
199
|
+
result = sandbox.run_code("print(df)")
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Streaming Output
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from code_sandboxes import Sandbox, OutputMessage
|
|
206
|
+
|
|
207
|
+
def handle_stdout(msg: OutputMessage):
|
|
208
|
+
print(f"[stdout] {msg.line}")
|
|
209
|
+
|
|
210
|
+
def handle_stderr(msg: OutputMessage):
|
|
211
|
+
print(f"[stderr] {msg.line}")
|
|
212
|
+
|
|
213
|
+
with Sandbox.create() as sandbox:
|
|
214
|
+
result = sandbox.run_code(
|
|
215
|
+
"for i in range(5): print(f'Step {i}')",
|
|
216
|
+
on_stdout=handle_stdout,
|
|
217
|
+
on_stderr=handle_stderr,
|
|
218
|
+
)
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
## API Reference
|
|
222
|
+
|
|
223
|
+
### Sandbox.create()
|
|
224
|
+
|
|
225
|
+
Factory method to create sandboxes:
|
|
226
|
+
|
|
227
|
+
```python
|
|
228
|
+
sandbox = Sandbox.create(
|
|
229
|
+
variant="datalayer-runtime", # Sandbox type
|
|
230
|
+
timeout=60, # Execution timeout (seconds)
|
|
231
|
+
environment="python-simple-env", # Runtime environment
|
|
232
|
+
gpu="T4", # GPU type (T4, A100, H100, etc.)
|
|
233
|
+
cpu=2.0, # CPU cores
|
|
234
|
+
memory=4096, # Memory in MB
|
|
235
|
+
env={"MY_VAR": "value"}, # Environment variables
|
|
236
|
+
tags={"project": "demo"}, # Metadata tags
|
|
237
|
+
)
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### Execution Result
|
|
241
|
+
|
|
242
|
+
```python
|
|
243
|
+
result = sandbox.run_code("print('hello')")
|
|
244
|
+
|
|
245
|
+
result.success # bool: Whether execution succeeded
|
|
246
|
+
result.stdout # str: Standard output
|
|
247
|
+
result.stderr # str: Standard error
|
|
248
|
+
result.text # str: Main result text
|
|
249
|
+
result.results # list[Result]: Rich results (HTML, images, etc.)
|
|
250
|
+
result.error # ExecutionError: Error details if failed
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### Core Methods
|
|
254
|
+
|
|
255
|
+
| Method | Description |
|
|
256
|
+
|--------|-------------|
|
|
257
|
+
| `Sandbox.create()` | Create a new sandbox |
|
|
258
|
+
| `Sandbox.from_id(id)` | Reconnect to an existing sandbox |
|
|
259
|
+
| `Sandbox.list()` | List all sandboxes |
|
|
260
|
+
| `sandbox.run_code(code)` | Execute Python code |
|
|
261
|
+
| `sandbox.files.read(path)` | Read file contents |
|
|
262
|
+
| `sandbox.files.write(path, content)` | Write file contents |
|
|
263
|
+
| `sandbox.files.list(path)` | List directory contents |
|
|
264
|
+
| `sandbox.commands.run(cmd)` | Run shell command |
|
|
265
|
+
| `sandbox.commands.exec(*args)` | Execute with streaming output |
|
|
266
|
+
| `sandbox.set_timeout(seconds)` | Update timeout |
|
|
267
|
+
| `sandbox.create_snapshot(name)` | Save sandbox state |
|
|
268
|
+
| `sandbox.terminate()` / `sandbox.kill()` | Stop sandbox |
|
|
269
|
+
|
|
270
|
+
## Configuration
|
|
271
|
+
|
|
272
|
+
### Environment Variables
|
|
273
|
+
|
|
274
|
+
- `DATALAYER_API_KEY`: API key for Datalayer runtime authentication
|
|
275
|
+
|
|
276
|
+
### SandboxConfig
|
|
277
|
+
|
|
278
|
+
```python
|
|
279
|
+
from code_sandboxes import SandboxConfig
|
|
280
|
+
|
|
281
|
+
config = SandboxConfig(
|
|
282
|
+
timeout=30.0, # Default execution timeout
|
|
283
|
+
environment="python-simple-env",
|
|
284
|
+
memory_limit=4 * 1024**3, # 4GB
|
|
285
|
+
cpu_limit=2.0,
|
|
286
|
+
gpu="T4",
|
|
287
|
+
working_dir="/workspace",
|
|
288
|
+
env_vars={"DEBUG": "1"},
|
|
289
|
+
max_lifetime=3600, # 1 hour
|
|
290
|
+
)
|
|
291
|
+
|
|
292
|
+
sandbox = Sandbox.create(config=config)
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## License
|
|
296
|
+
|
|
297
|
+
Copyright (c) 2025-2026 Datalayer, Inc.
|
|
298
|
+
|
|
299
|
+
BSD 3-Clause License
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
code_sandboxes/__init__.py,sha256=EOTezFV0i_LjMNPQToEXBIxMjTvt81GHcec96VF5CaM,3643
|
|
2
|
+
code_sandboxes/__version__.py,sha256=6xxbUamNY9MkCJdDf9ToVcvXsaNhGLQeJgGS_M1Vwks,71
|
|
3
|
+
code_sandboxes/base.py,sha256=VWim-WTwga_dLZHnezUAoR59BGdHprZz-ygwVsqtLm8,19116
|
|
4
|
+
code_sandboxes/commands.py,sha256=AHXLMv8aCDZjs_meI8PSptLo_u9QBZlwfziUP35LvVc,12278
|
|
5
|
+
code_sandboxes/exceptions.py,sha256=ztF2d5IdEwX0E974_8mWf4iVrII8rZJewolfFV_6sLo,3130
|
|
6
|
+
code_sandboxes/filesystem.py,sha256=e2aeyg471b9Xv2AXhabuUEEATy_1_sInNorGmhPv6rU,12931
|
|
7
|
+
code_sandboxes/models.py,sha256=dZBk_AztlHTGR9xsilzjD0guEFvdbxCMduUTdrkhJlA,11542
|
|
8
|
+
code_sandboxes/local/__init__.py,sha256=ilanyZR7xhuPyUPHrnjwxRAZiiGFIl9SdS0fYthXFYo,181
|
|
9
|
+
code_sandboxes/local/eval_sandbox.py,sha256=C5yzBT2Q1URGMhvJM85yyAISfrnjUDwP_flRGkgGhwo,10157
|
|
10
|
+
code_sandboxes/remote/__init__.py,sha256=BQzFMhNuXjOnyUNTUmE5dxoq2xVAiw6YsHKtLBXjT-c,187
|
|
11
|
+
code_sandboxes/remote/datalayer_sandbox.py,sha256=7EdvBgbMXx5nyBj3d8ndRoIOirGKf8yH_2YLRM5_76I,20672
|
|
12
|
+
code_sandboxes-0.0.2.dist-info/METADATA,sha256=XDn-nsCDPNdW1R4YUuUC6FvEqePj6eZD8QUrc5HbrKw,9804
|
|
13
|
+
code_sandboxes-0.0.2.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
14
|
+
code_sandboxes-0.0.2.dist-info/licenses/LICENSE,sha256=kW62dan-1HEplL1Cmfooudf8dRqCPGQZGD6APtrlZa8,1516
|
|
15
|
+
code_sandboxes-0.0.2.dist-info/RECORD,,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025, Datalayer
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|