Skip to main content
Templates let you create reusable sandbox images from Dockerfiles. Pre-install languages, tools, and dependencies so your sandboxes start ready to go.

Building a Template

from opencomputer import Template

templates = Template(api_url, api_key)

template = await templates.build('python-ml', """
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install numpy pandas scikit-learn
""")

print(template.template_id)  # tpl-abc123
print(template.status)       # building → ready

await templates.build(name, dockerfile)

Builds a new template from a Dockerfile.
name
str
required
A unique name for the template.
dockerfile
str
required
The Dockerfile content to build from.
Returns: TemplateInfo

Listing Templates

all_templates = await templates.list()

for t in all_templates:
    print(f"{t.name} ({t.tag}) — {t.status}")

await templates.list()

Returns all templates. Returns: list[TemplateInfo]

Getting a Template

template = await templates.get('python-ml')
print(template)

await templates.get(name)

Gets a template by name.
name
str
required
Template name.
Returns: TemplateInfo

Deleting a Template

await templates.delete('python-ml')

await templates.delete(name)

Deletes a template by name.
name
str
required
Template name to delete.
Returns: None

TemplateInfo

TemplateInfo is a dataclass:
FieldTypeDescription
template_idstrUnique template identifier
namestrTemplate name
tagstrImage tag
statusstrBuild status: "building", "ready", "error"

Using a Template

Once built, pass the template name when creating a sandbox:
sandbox = await Sandbox.create(template='python-ml')
result = await sandbox.commands.run(
    'python3 -c "import numpy; print(numpy.__version__)"'
)
print(result.stdout)  # 1.24.0
await sandbox.kill()