Code Sandboxes (code_sandboxes) is a Python package for running code in isolated sandbox variants through a unified API.
Canonical variant names:
cloudflarecoreweavedatalayerdaytonadockere2bevalgoogle-colabjupyter-serverkagglemodalmonty
The full documentation is the single source of truth:
- Docs home: https://code-sandboxes.datalayer.tech
- Providers and variant setup: https://code-sandboxes.datalayer.tech/providers
- Installation: https://code-sandboxes.datalayer.tech/install
- CLI usage: https://code-sandboxes.datalayer.tech/cli
- API reference: https://code-sandboxes.datalayer.tech/api-reference
- Examples: https://code-sandboxes.datalayer.tech/examples
- Guide: https://code-sandboxes.datalayer.tech/guide
Published site:
pip install code-sandboxesFor provider-specific extras and credentials, see https://code-sandboxes.datalayer.tech/install and https://code-sandboxes.datalayer.tech/providers.
from code_sandboxes import Sandbox
# Option 1: manage a local Jupyter server automatically
with Sandbox.create(variant="jupyter-server") as sandbox:
print(sandbox.run_code("1 + 1").text) # 2
# Option 2: connect to an existing Jupyter server
with Sandbox.create(
variant="jupyter-server",
server_url="http://localhost:8888",
token="MY_TOKEN",
) as sandbox:
sandbox.run_code("x = 40")
print(sandbox.run_code("x + 2").text) # 42Daytona, E2B, and Modal sandboxes can prepare a real Jupyter Server and return the provider HTTPS/WebSocket ingress needed to reach it:
from code_sandboxes import JupyterServerOptions, Sandbox
sandbox = Sandbox.create(variant="daytona") # also: e2b, modal
sandbox.start()
endpoint = sandbox.prepare_jupyter_server(
JupyterServerOptions(port=8888, install_if_missing=True)
)Preparation first checks that jupyter-server and ipykernel are both
importable, installs the pair if that check fails, launches Jupyter in the
background, and waits for its port to accept connections. Calling the method again on the same sandbox returns
the cached endpoint. Provider-ingress credentials are in endpoint.headers;
the separate Jupyter token is in endpoint.query. Do not send either to the
browser: a server-side gateway should apply them while proxying HTTP and
WebSocket traffic.
install_if_missing=False makes a prebuilt template or snapshot mandatory.
Templates and snapshots are the intended cold-start optimization; the
conditional installation is the preliminary path for ordinary provider base
images.
Kaggle supports both batch execution and interactive connections through the
kaggle sandbox. Install its optional dependency first:
pip install "code-sandboxes[kaggle]"Required credentials for batch mode:
~/.kaggle/kaggle.json, orKAGGLE_API_KEY
# Install Kaggle support
pip install code-sandboxes[kaggle]
# Optional: env-based credentials (if not using ~/.kaggle/kaggle.json)
export KAGGLE_API_KEY="<your-kaggle-api-key>"
# Launch the REPL
code-sandboxes repl --variant kaggleFor batch execution, configure Kaggle credentials and create the sandbox without a runtime URL:
from code_sandboxes import Sandbox
with Sandbox.create(variant="kaggle") as sandbox:
result = sandbox.run_code("print('hello from kaggle')")
print(result.stdout)The lower-level batch API is also available directly:
from code_sandboxes import KaggleKernelExecutor
executor = KaggleKernelExecutor()
result = executor.execute(
"print('hello from kaggle')",
title="code-sandboxes-demo",
accelerator="NvidiaTeslaT4",
wait=True,
)
print(result.status, result.stdout)
print(result.to_kernel_reply())For interactive execution, copy the WebSocket channels URL from an active Kaggle notebook session and pass it to the sandbox or client:
from code_sandboxes import KaggleKernelClient
with KaggleKernelClient.from_channels_url(channels_url, token=None) as kernel:
print(kernel.execute("x = 1 + 1; print(x)"))See the complete Kaggle guide for authentication, accelerators, channels URL retrieval, and execution options.
Google Colab exposes an already-running kernel through an authenticating proxy. Copy its WebSocket channels URL from the browser's 网络 tools, then pass it directly to the sandbox:
from code_sandboxes import Sandbox
with Sandbox.create(variant="google-colab", channels_url=channels_url) as sandbox:
print(sandbox.run_code("x = 1 + 1; print(x)").stdout)The lower-level client and parser are owned by Code Sandboxes as well:
from code_sandboxes import GoogleColabKernelClient, parse_google_colab_channels_url
server_url, kernel_id, proxy_token = parse_google_colab_channels_url(channels_url)
with GoogleColabKernelClient.from_channels_url(channels_url) as kernel:
print(kernel.execute("print('hello from colab')"))See the complete Google Colab guide for proxy authentication, explicit connection values, and channels URL retrieval.
Every variant answers the same verbs — create, list, get, update, delete — from Python or from the CLI, rendered as rich tables:
code-sandboxes list # every variant that answers, one table
code-sandboxes list -v kaggle # one variant
code-sandboxes get <id> -v modal # one sandbox, live status
code-sandboxes create -v modal # create detached, leave it running
code-sandboxes update <id> -v modal --tag team=ai # tags (modal), --name
# (docker), --capability (datalayer),
# --code (kaggle: a new version)
code-sandboxes delete <id> -v modal --yes
code-sandboxes environments # what sandboxes can be created infrom code_sandboxes import get_manager
manager = get_manager("modal")
for info in manager.list():
print(info.id, info.status)
manager.delete("sb-...")See the management guide for what each variant maps to and its connection settings.
BSD 3-Clause License