> ## Documentation Index
> Fetch the complete documentation index at: https://ppio.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 示例

以下示例展示了 Template 工作流中推荐使用的 `Template` 与 `Sandbox` 资源入口。

## 构建 Python 镜像并运行 Sandbox

<CodeGroup>
  ```python Python theme={null}
  from ppio_sandbox import PPIO

  ppio = PPIO()

  template = ppio.template.new().from_python_image("3.12")
  build = ppio.template.build(template, "example-python-template")

  sandbox = ppio.sandbox.create(build.template_id)
  result = sandbox.commands.run("python --version")
  print(result.stdout)

  sandbox.kill()
  ```

  ```typescript JavaScript & TypeScript theme={null}
  import { PPIO } from 'ppio-sandbox'

  const ppio = new PPIO()

  const template = ppio.template.new().fromPythonImage('3.12')
  const build = await ppio.template.build(template, 'example-python-template')

  const sandbox = await ppio.sandbox.create(build.templateId)
  const result = await sandbox.commands.run('python --version')
  console.log(result.stdout)

  await sandbox.kill()
  ```

  ```bash CLI theme={null}
  # 从 Dockerfile 构建 Template，然后创建 Sandbox 并运行命令
  ppio template create example-python-template --dockerfile ./ppio.Dockerfile
  sandbox_id=$(ppio sandbox create example-python-template -d)
  ppio sandbox exec "$sandbox_id" -- python --version
  ppio sandbox kill "$sandbox_id"
  ```
</CodeGroup>

## 为发布版本打标签

<CodeGroup>
  ```python Python theme={null}
  from ppio_sandbox import PPIO

  ppio = PPIO()

  template = ppio.template.new().from_python_image("3.12")
  build = ppio.template.build(template, "my-template", tags=["latest", "stable"])

  ppio.template.assign_tags("my-template:v1.0", "production")
  ```

  ```typescript JavaScript & TypeScript theme={null}
  import { PPIO } from 'ppio-sandbox'

  const ppio = new PPIO()

  const template = ppio.template.new().fromPythonImage('3.12')
  const build = await ppio.template.build(template, 'my-template', {
    tags: ['latest', 'stable'],
  })

  await ppio.template.assignTags('my-template:v1.0', 'production')
  ```
</CodeGroup>

## 查看 Template

<CodeGroup>
  ```python Python theme={null}
  from ppio_sandbox import PPIO

  ppio = PPIO()

  res = ppio.template.list(template_type="template_build", page=1, limit=5)

  for item in res.items:
      print(item.template_id, item.aliases, item.cpu_count, item.memory_mb)
  ```

  ```typescript JavaScript & TypeScript theme={null}
  import { PPIO } from 'ppio-sandbox'

  const ppio = new PPIO()

  const paginator = ppio.template.list({ templateType: 'template_build', limit: 5 })
  const page = await paginator.nextPage()

  for (const item of page.templates) {
    console.log(item.templateId, item.aliases, item.cpuCount, item.memoryMB)
  }
  ```
</CodeGroup>
