Get
Get an existing sandbox using its ID.
Sandbox.get(sandbox_id: str) -> SandboxLoad an existing sandbox using a stored ID from your database, file, or session.
Parameters
sandbox_idstrrequiredThe UUID of an existing sandbox from sbx.id.
Returns
Returns a Sandbox instance connected to the existing sandbox. You can then use all methods like execute(), run(), status(), etc.
Examples
Basic Usage
from concave import Sandbox
# Create a sandbox and save its ID
sbx = Sandbox.create()
sandbox_id = sbx.id
print(f"Created: {sandbox_id}")
# ... later, get it back using the ID
sbx = Sandbox.get(sandbox_id)
result = sbx.execute("echo 'still here!'")
print(result.stdout) # still here!
sbx.delete()Store ID in Database
from concave import Sandbox
import sqlite3
# Create sandbox and store ID
sbx = Sandbox.create()
sandbox_id = sbx.id
# Save to database
conn = sqlite3.connect('app.db')
conn.execute(
"INSERT INTO tasks (user_id, sandbox_id) VALUES (?, ?)",
(user_id, sandbox_id)
)
conn.commit()
# ... later, retrieve and reconnect
cursor = conn.execute(
"SELECT sandbox_id FROM tasks WHERE user_id = ?",
(user_id,)
)
sandbox_id = cursor.fetchone()[0]
# Get the sandbox
sbx = Sandbox.get(sandbox_id)
result = sbx.run("print('Hello!')")
print(result.stdout)Persistent Workflow
from concave import Sandbox
import json
# Day 1: Start long-running task
sbx = Sandbox.create()
sbx.run("""
# Install dependencies
import subprocess
subprocess.run(['pip', 'install', 'pandas'])
""")
# Save sandbox ID for later
with open('sandbox.json', 'w') as f:
json.dump({'sandbox_id': sbx.id}, f)
# Day 2: Continue work
with open('sandbox.json', 'r') as f:
data = json.load(f)
sbx = Sandbox.get(data['sandbox_id'])
result = sbx.run("""
import pandas as pd
# Continue processing...
print('Ready to work!')
""")
print(result.stdout)Check Status Before Using
from concave import Sandbox, SandboxNotFoundError
sandbox_id = "stored-id-here"
try:
sbx = Sandbox.get(sandbox_id)
status = sbx.status()
if status['state'] == 'running':
result = sbx.execute("uptime")
print(f"Sandbox is running: {result.stdout}")
else:
print(f"Sandbox in {status['state']} state")
except SandboxNotFoundError:
print("Sandbox no longer exists")
# Create a new one
sbx = Sandbox.create()Exceptions
SandboxNotFoundErrorExceptionRaised when the sandbox ID doesn't exist, was deleted, or belongs to another account.
SandboxAuthenticationErrorExceptionRaised when API key is invalid or authentication fails.
SandboxTimeoutErrorExceptionRaised when the request times out.
ValueErrorExceptionRaised when API key environment variable is not set.
Error Handling
from concave import (
Sandbox,
SandboxNotFoundError,
SandboxAuthenticationError
)
sandbox_id = "your-sandbox-id"
try:
sbx = Sandbox.get(sandbox_id)
print(f"Connected to {sbx.id}")
result = sbx.execute("hostname")
print(result.stdout)
except SandboxNotFoundError:
print(f"Sandbox {sandbox_id} not found")
print("It may have been deleted or doesn't exist")
except SandboxAuthenticationError as e:
print(f"Authentication failed: {e}")
except Exception as e:
print(f"Unexpected error: {e}")