安排笔记本运行

本页面介绍了如何在 Colab Enterprise 中安排笔记本运行。

概览

您可以安排笔记本立即运行一次,也可以按周期性安排运行。

安排笔记本运行时间时,您可以选择运行时模板。 Colab Enterprise 会使用此运行时模板来创建运行笔记本的运行时。

运行时需要特定权限才能运行笔记本的代码并访问 Google Cloud 服务和 API。

  • 如果运行时模板配置启用了最终用户凭据,则运行时会使用与您的用户凭据关联的权限。

  • 如果未启用最终用户凭据,您必须在安排笔记本运行时间时指定服务账号。Colab Enterprise 使用此服务账号的凭据来运行笔记本。

如需了解详情,请参阅运行笔记本所需的角色

Colab Enterprise 完成笔记本运行后,结果会存储在可共享的 Cloud Storage 存储桶中。

限制

Colab Enterprise 运行时使用 Compute Engine 配额。请参阅 Compute Engine 分配配额页面

准备工作

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI, Dataform, and Compute Engine APIs.

    Enable the APIs

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI, Dataform, and Compute Engine APIs.

    Enable the APIs

  8. 安排笔记本运行所需的角色

    如需获得在 Colab Enterprise 中安排笔记本运行所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    您也可以通过自定义角色或其他预定义角色来获取所需的权限。

    运行笔记本所需的角色

    运行笔记本的主账号需要特定权限。如概览中所述,主账号可以是您的用户账号,也可以是您指定的服务账号。

    如需获得在 Colab Enterprise 中运行笔记本所需的权限,请让您的管理员为您授予以下 IAM 角色:

    如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

    这些预定义角色包含在 Colab Enterprise 中运行笔记本所需的权限。如需查看所需的确切权限,请展开所需权限部分:

    所需权限

    如需在 Colab Enterprise 中运行笔记本,您需要具备以下权限:

    • dataform.locations.list 在笔记本上
    • dataform.repositories.computeAccessTokenStatus 在笔记本上
    • dataform.repositories.fetchHistory 在笔记本上
    • dataform.repositories.fetchRemoteBranches 在笔记本上
    • dataform.repositories.get 在笔记本上
    • dataform.repositories.getIamPolicy 在笔记本上
    • dataform.repositories.list 在笔记本上
    • dataform.repositories.queryDirectoryContents 在笔记本上
    • dataform.repositories.readFile 在笔记本上
    • 针对项目的 logging.logEntries.create 权限
    • 针对项目的 logging.logEntries.route 权限
    • 针对项目的 monitoring.metricDescriptors.create 权限
    • 针对项目的 monitoring.metricDescriptors.get 权限
    • 针对项目的 monitoring.metricDescriptors.list 权限
    • 针对项目的 monitoring.monitoredResourceDescriptors.get 权限
    • 针对项目的 monitoring.monitoredResourceDescriptors.list 权限
    • 针对项目的 monitoring.timeSeries.create 权限
    • 针对项目的 resourcemanager.projects.get 权限
    • 针对项目的 resourcemanager.projects.list 权限
    • storage.buckets.get 在笔记本上
    • storage.managedFolders.create 在笔记本上
    • storage.managedFolders.delete 在笔记本上
    • storage.managedFolders.get 在笔记本上
    • storage.managedFolders.list 在笔记本上
    • storage.multipartUploads.abort 在笔记本上
    • storage.multipartUploads.create 在笔记本上
    • storage.multipartUploads.list 在笔记本上
    • storage.multipartUploads.listParts 在笔记本上
    • storage.objects.create 在笔记本上
    • storage.objects.delete 在笔记本上
    • storage.objects.get 在笔记本上
    • storage.objects.list 在笔记本上
    • storage.objects.restore 在笔记本上
    • storage.objects.setRetention 在笔记本上

    您也可以使用自定义角色或其他预定义角色来获取这些权限。

    运行笔记本一次

    如需运行笔记本一次,您可以使用 Google Cloud 控制台、Google Cloud CLI、Vertex AI Python 客户端库或 Terraform。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。

      前往我的笔记本

    2. 区域菜单中,选择包含笔记本的区域。

    3. 点击笔记本旁边的笔记本操作菜单,然后选择安排

    4. 时间表名称字段中,输入时间表的名称。

    5. 点击运行时模板列表,然后选择一个运行时模板。运行时模板用于确定运行笔记本的运行时的规范。

    6. 运行时间表下,选择一次性,以便在您提交笔记本运行后立即运行笔记本。

    7. Cloud Storage 输出位置字段旁边,点击浏览以打开选择文件夹对话框。

    8. 选择一个 Cloud Storage 存储桶。或者,如需创建存储桶,请点击  创建新存储桶,然后完成对话框。

    9. 如果您选择的运行时模板未启用最终用户凭据,则该对话框会包含服务账号字段。在服务账号字段中,输入服务账号的电子邮件地址。

    10. 点击提交

      笔记本运行会立即开始。

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • DISPLAY_NAME:笔记本运行的显示名称。
    • NOTEBOOK_RUNTIME_TEMPLATE:用于指定运行时计算配置的笔记本运行时模板。
    • NOTEBOOK_URI:要运行的笔记本的 Cloud Storage URI。
    • OUTPUT_URI:您希望将结果存储到的 Cloud Storage 位置。
    • USER_EMAIL:指定笔记本运行对 Google Cloud 资源的访问权限的用户账号电子邮件地址。
    • PROJECT_ID:您的项目 ID。
    • REGION:笔记本将运行的区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab executions create --display-name="DISPLAY_NAME" \
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE \
        --gcs-notebook-uri=NOTEBOOK_URI \
        --gcs-output-uri=OUTPUT_URI \
        --user-email=USER_EMAIL \
        --project=PROJECT_ID \
        --region=REGION

    Windows (PowerShell)

    gcloud colab executions create --display-name="DISPLAY_NAME" `
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE `
        --gcs-notebook-uri=NOTEBOOK_URI `
        --gcs-output-uri=OUTPUT_URI `
        --user-email=USER_EMAIL `
        --project=PROJECT_ID `
        --region=REGION

    Windows (cmd.exe)

    gcloud colab executions create --display-name="DISPLAY_NAME" ^
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE ^
        --gcs-notebook-uri=NOTEBOOK_URI ^
        --gcs-output-uri=OUTPUT_URI ^
        --user-email=USER_EMAIL ^
        --project=PROJECT_ID ^
        --region=REGION

    如需详细了解如何从命令行管理 Colab Enterprise 笔记本运行,请参阅 gcloud CLI 文档

    Python

    在尝试此示例之前,请安装 Python 版 Vertex AI SDK。Vertex AI Python 客户端库会在您安装 Python 版 Vertex AI SDK 时安装。如需了解详情,请参阅 Vertex AI SDK for Python API 参考文档

    如需运行以下代码示例,您需要笔记本的 Dataform 代码库 ID。如需获取笔记本的代码库 ID,您可以使用 Dataform 的 list_repositories 方法。

    from google.cloud import aiplatform_v1
    
    PROJECT_ID = "my-project"
    LOCATION = "us-central1"
    REPOSITORY_ID = "b223577f-a3fb-482c-a22c-0658c6602598"
    TEMPLATE_ID = "6524523989455339520"
    
    API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
    PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"
    
    notebook_service_client = aiplatform_v1.NotebookServiceClient(client_options = {
        "api_endpoint": API_ENDPOINT,
    })
    
    operation = notebook_service_client.create_notebook_execution_job(parent=PARENT, notebook_execution_job={
        "display_name": "my-execution-job",
    
        # Specify a NotebookRuntimeTemplate to source compute configuration from
        "notebook_runtime_template_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/notebookRuntimeTemplates/{TEMPLATE_ID}",
    
        # Specify a Colab Enterprise notebook to run
        "dataform_repository_source": {
            "dataform_repository_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/repositories/{REPOSITORY_ID}",
        },
    
        # Specify a Cloud Storage bucket to store output artifacts
        "gcs_output_uri": "gs://my-bucket/",
    
        # Specify the identity that runs the notebook
        "execution_user": "{EMAIL}",
    
        # Run as the service account instead
        # "service_account": "my-service-account",
    })
    print("Waiting for operation to complete...")
    result = operation.result()

    Terraform

    如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令。 如需了解详情,请参阅 Terraform 提供程序参考文档

    以下示例使用 google_colab_notebook_execution Terraform 资源运行 Colab Enterprise 笔记本。

    resource "google_colab_runtime_template" "my_runtime_template" {
      provider = google-beta
      name = "{{index $.Vars "runtime_template_name"}}"
      display_name = "Runtime template"
      location = "us-central1"
    
      machine_spec {
        machine_type     = "e2-standard-4"
      }
    
      network_spec {
        enable_internet_access = true
      }
    }
    
    resource "google_storage_bucket" "output_bucket" {
      provider = google-beta
      name          = "{{index $.Vars "bucket"}}"
      location      = "US"
      force_destroy = true
      uniform_bucket_level_access = true
    }
    
    resource "google_storage_bucket_object" "notebook" {
      provider = google-beta
      name   = "hello_world.ipynb"
      bucket = google_storage_bucket.output_bucket.name
      content = <<EOF
        {
          "cells": [
            {
              "cell_type": "code",
              "execution_count": null,
              "metadata": {},
              "outputs": [],
              "source": [
                "print(\"Hello, World!\")"
              ]
            }
          ],
          "metadata": {
            "kernelspec": {
              "display_name": "Python 3",
              "language": "python",
              "name": "python3"
            },
            "language_info": {
              "codemirror_mode": {
                "name": "ipython",
                "version": 3
              },
              "file_extension": ".py",
              "mimetype": "text/x-python",
              "name": "python",
              "nbconvert_exporter": "python",
              "pygments_lexer": "ipython3",
              "version": "3.8.5"
            }
          },
          "nbformat": 4,
          "nbformat_minor": 4
        }
        EOF
    }
    
    resource "google_colab_notebook_execution" "{{$.PrimaryResourceId}}" {
      provider = google-beta
      notebook_execution_job_id = "{{index $.Vars "notebook_execution_job_id"}}"
      display_name = "Notebook execution full"
      location = "us-central1"
    
      execution_timeout = "86400s"
      gcs_notebook_source {
      uri = "gs://${google_storage_bucket_object.notebook.bucket}/${google_storage_bucket_object.notebook.name}"
      generation = google_storage_bucket_object.notebook.generation
      }
    
      service_account = "{{index $.TestEnvVars "service_account"}}"
    
      gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
      notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"
    
      depends_on = [
        google_storage_bucket_object.notebook,
        google_storage_bucket.output_bucket,
        google_colab_runtime_template.my_runtime_template,
      ]
    
    }
    

    您可以在执行页面上查看已完成的笔记本运行的结果。

    安排笔记本运行

    如需安排笔记本运行时间,您可以使用 Google Cloud 控制台、gcloud CLI、Vertex AI Python 客户端库或 Terraform。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 我的笔记本页面。

      前往我的笔记本

    2. 区域菜单中,选择包含笔记本的区域。

    3. 点击笔记本旁边的笔记本操作菜单,然后选择安排

    4. 时间表名称字段中,输入时间表的名称。

    5. 点击运行时模板列表,然后选择一个运行时模板。运行时模板用于确定运行笔记本的运行时的规范。

    6. 运行时间表下,选择周期性,以安排在特定时间间隔运行笔记本。

    7. 完成调度对话框。

    8. Cloud Storage 输出位置字段旁边,点击浏览以打开选择文件夹对话框。

    9. 选择一个 Cloud Storage 存储桶。或者,如需创建存储桶,请点击  创建新存储桶,然后完成对话框。

    10. 如果您选择的运行时模板未启用最终用户凭据,则该对话框会包含服务账号字段。在服务账号字段中,输入服务账号的电子邮件地址。

    11. 点击提交

      系统会按您设置的时间表自动开始运行已安排的笔记本。

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • DISPLAY_NAME:相应时间表的显示名称。
    • CRON_SCHEDULE:您设置的时间表,采用 unix-cron 格式。 例如,00 19 * * MON 表示格林威治标准时间 (GMT) 每周一的 19:00。
    • NOTEBOOK_RUN_NAME:此时间表生成的笔记本运行的显示名称。
    • NOTEBOOK_RUNTIME_TEMPLATE:用于指定运行时计算配置的笔记本运行时模板。
    • NOTEBOOK_URI:要运行的笔记本的 Cloud Storage URI。
    • OUTPUT_URI:您希望将结果存储到的 Cloud Storage 位置。
    • USER_EMAIL:指定笔记本运行对 Google Cloud 资源的访问权限的用户账号电子邮件地址。
    • PROJECT_ID:您的项目 ID。
    • REGION:您的时间表将运行的区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab schedules create --display-name="DISPLAY_NAME" \
        --cron-schedule=CRON_SCHEDULE \
        --execution-display-name=NOTEBOOK_RUN_NAME \
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE \
        --gcs-notebook-uri=NOTEBOOK_URI \
        --gcs-output-uri=OUTPUT_URI \
        --user-email=USER_EMAIL \
        --project=PROJECT_ID \
        --region=REGION

    Windows (PowerShell)

    gcloud colab schedules create --display-name="DISPLAY_NAME" `
        --cron-schedule=CRON_SCHEDULE `
        --execution-display-name=NOTEBOOK_RUN_NAME `
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE `
        --gcs-notebook-uri=NOTEBOOK_URI `
        --gcs-output-uri=OUTPUT_URI `
        --user-email=USER_EMAIL `
        --project=PROJECT_ID `
        --region=REGION

    Windows (cmd.exe)

    gcloud colab schedules create --display-name="DISPLAY_NAME" ^
        --cron-schedule=CRON_SCHEDULE ^
        --execution-display-name=NOTEBOOK_RUN_NAME ^
        --notebook-runtime-template=NOTEBOOK_RUNTIME_TEMPLATE ^
        --gcs-notebook-uri=NOTEBOOK_URI ^
        --gcs-output-uri=OUTPUT_URI ^
        --user-email=USER_EMAIL ^
        --project=PROJECT_ID ^
        --region=REGION

    如需详细了解如何从命令行创建 Colab Enterprise 笔记本调度,请参阅 gcloud CLI 文档

    Python

    在尝试此示例之前,请安装 Python 版 Vertex AI SDK。Vertex AI Python 客户端库会在您安装 Python 版 Vertex AI SDK 时安装。如需了解详情,请参阅 Vertex AI SDK for Python API 参考文档

    如需运行以下代码示例,您需要笔记本的 Dataform 代码库 ID。如需获取笔记本的代码库 ID,您可以使用 Dataform 的 list_repositories 方法。

    from google.cloud import aiplatform_v1
    
    PROJECT_ID = "my-project"
    LOCATION = "us-central1"
    REPOSITORY_ID = "b223577f-a3fb-482c-a22c-0658c6602598"
    TEMPLATE_ID = "6524523989455339520"
    
    API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
    PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"
    
    schedules_service_client = aiplatform_v1.ScheduleServiceClient(client_options = {
        "api_endpoint": API_ENDPOINT,
    })
    
    schedule = schedules_service_client.create_schedule(parent=PARENT, schedule={
        "display_name": "my-notebook-schedule",
    
        # Time specification. TZ is optional.
        # cron = "* * * * *" to run it in the next minute.
        "cron": "TZ=America/Los_Angeles * * * * *",
    
        # How many runs the schedule will trigger before it becomes COMPLETED.
        # A Schedule in COMPLETED state will not trigger any more runs.
        "max_run_count": 1,
        "max_concurrent_run_count": 1,
    
        "create_notebook_execution_job_request": {
          "parent": PARENT,
          "notebook_execution_job": {
            "display_name": "my-execution-job",
    
            # Specify a NotebookRuntimeTemplate to source compute configuration from
            "notebook_runtime_template_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/notebookRuntimeTemplates/{TEMPLATE_ID}",
    
            # Specify a Colab Enterprise notebook to run
            "dataform_repository_source": {
                "dataform_repository_resource_name": f"projects/{PROJECT_ID}/locations/{LOCATION}/repositories/{REPOSITORY_ID}",
            },
    
            # Specify a Cloud Storage bucket to store output artifacts
            "gcs_output_uri": "gs://my-bucket/",
    
    
            # Specify the identity that runs the notebook
            "execution_user": "{EMAIL}",
    
            # Run as the service account instead
            # "service_account": "my-service-account",
        }
      }
    })

    Terraform

    如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令。 如需了解详情,请参阅 Terraform 提供程序参考文档

    以下示例使用 google_colab_schedule Terraform 资源来安排 Colab Enterprise 笔记本运行。

    resource "google_colab_runtime_template" "my_runtime_template" {
      provider = google-beta
      name = "{{index $.Vars "runtime_template_name"}}"
      display_name = "Runtime template"
      location = "us-central1"
    
      machine_spec {
        machine_type     = "e2-standard-4"
      }
    
      network_spec {
        enable_internet_access = true
      }
    }
    
    resource "google_storage_bucket" "output_bucket" {
      provider = google-beta
      name          = "{{index $.Vars "bucket"}}"
      location      = "US"
      force_destroy = true
      uniform_bucket_level_access = true
    }
    
    resource "google_secret_manager_secret" "secret" {
      provider = google-beta
      secret_id = "{{index $.Vars "secret"}}"
      replication {
        auto {}
      }
    }
    
    resource "google_secret_manager_secret_version" "secret_version" {
      provider = google-beta
      secret = google_secret_manager_secret.secret.id
      secret_data = "secret-data"
    }
    
    resource "google_dataform_repository" "dataform_repository" {
      provider = google-beta
      name = "{{index $.Vars "dataform_repository"}}"
      display_name = "dataform_repository"
      npmrc_environment_variables_secret_version = google_secret_manager_secret_version.secret_version.id
      kms_key_name = "{{index $.Vars "key_name"}}"
    
      labels = {
        label_foo1 = "label-bar1"
      }
    
      git_remote_settings {
          url = "https://github.com/OWNER/REPOSITORY.git"
          default_branch = "main"
          authentication_token_secret_version = google_secret_manager_secret_version.secret_version.id
      }
    
      workspace_compilation_overrides {
        default_database = "database"
        schema_suffix = "_suffix"
        table_prefix = "prefix_"
      }
    
    }
    
    resource "google_colab_schedule" "{{$.PrimaryResourceId}}" {
      provider = google-beta
      display_name = "{{index $.Vars "display_name"}}"
      location = "{{index $.TestEnvVars "location"}}"
      allow_queueing = true
      max_concurrent_run_count = 2
      cron = "TZ=America/Los_Angeles * * * * *"
      max_run_count = 5
      start_time = "{{index $.Vars "start_time"}}"
      end_time = "{{index $.Vars "end_time"}}"
    
      desired_state = "ACTIVE"
    
      create_notebook_execution_job_request {
        notebook_execution_job {
          display_name = "Notebook execution"
          execution_timeout = "86400s"
    
          dataform_repository_source {
            commit_sha = "randomsha123"
            dataform_repository_resource_name = "projects/{{index $.TestEnvVars "project_id"}}/locations/{{index $.TestEnvVars "location"}}/repositories/${google_dataform_repository.dataform_repository.name}"
          }
    
          notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"
    
          gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
          service_account = "{{index $.TestEnvVars "service_account"}}"
        }
      }
    
      depends_on = [
        google_colab_runtime_template.my_runtime_template,
        google_storage_bucket.output_bucket,
        google_secret_manager_secret_version.secret_version,
        google_dataform_repository.dataform_repository,
      ]
    }
    

    在 Google Cloud 控制台中,您可以在“时间表”页面上查看时间表。您可以在执行页面上查看已完成的笔记本运行作业的结果。

    查看结果

    如需查看笔记本运行结果,您可以使用 Google Cloud 控制台、gcloud CLI 或 Vertex AI Python 客户端库。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 执行页面。

      前往“执行”

    2. 在要查看结果的笔记本运行旁边,点击查看结果

      Colab Enterprise 会在新标签页中打开笔记本运行结果。

    3. 如需查看结果,请点击相应标签页。

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • PROJECT_ID:您的项目 ID。
    • REGION:笔记本运行结果所在的区域。
    • SCHEDULE_NAME:要查看结果的时间表的名称。 如需查看所有时间表的运行结果,请省略 --filter 标志。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab executions list --project=PROJECT_ID \
        --region=REGION \
        --filter="scheduleResourceName:SCHEDULE_NAME"

    Windows (PowerShell)

    gcloud colab executions list --project=PROJECT_ID `
        --region=REGION `
        --filter="scheduleResourceName:SCHEDULE_NAME"

    Windows (cmd.exe)

    gcloud colab executions list --project=PROJECT_ID ^
        --region=REGION ^
        --filter="scheduleResourceName:SCHEDULE_NAME"

    如需详细了解如何从命令行列出 Colab Enterprise 笔记本运行,请参阅 gcloud CLI 文档

    Python

    在尝试此示例之前,请安装 Python 版 Vertex AI SDK。Vertex AI Python 客户端库会在您安装 Python 版 Vertex AI SDK 时安装。如需了解详情,请参阅 Vertex AI SDK for Python API 参考文档

    如需运行以下代码示例,您需要笔记本的 Dataform 代码库 ID。如需获取笔记本的代码库 ID,您可以使用 Dataform 的 list_repositories 方法。

    from google.cloud import aiplatform_v1
    
    PROJECT_ID = "my-project"
    LOCATION = "us-central1"
    
    API_ENDPOINT = f"{LOCATION}-aiplatform.googleapis.com"
    PARENT = f"projects/{PROJECT_ID}/locations/{LOCATION}"
    
    notebook_service_client = aiplatform_v1.NotebookServiceClient(client_options = {
        "api_endpoint": API_ENDPOINT,
    })
    
    notebook_execution_jobs = notebook_service_client.list_notebook_execution_jobs(parent=PARENT)
    notebook_execution_jobs

    删除结果

    如需删除笔记本运行中的某个结果,您可以使用 Google Cloud 控制台或 gcloud CLI。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 执行页面。

      前往“执行”

    2. 选择要删除结果的笔记本运行。

    3. 点击  删除

    4. 点击确认以确认删除。

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • NOTEBOOK_RUN_ID:要删除的笔记本运行作业的 ID。
    • PROJECT_ID:您的项目 ID。
    • REGION:笔记本运行所在的区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab executions delete NOTEBOOK_RUN_ID \
        --project=PROJECT_ID \
        --region=REGION

    Windows (PowerShell)

    gcloud colab executions delete NOTEBOOK_RUN_ID `
        --project=PROJECT_ID `
        --region=REGION

    Windows (cmd.exe)

    gcloud colab executions delete NOTEBOOK_RUN_ID ^
        --project=PROJECT_ID ^
        --region=REGION

    如需详细了解如何从命令行删除 Colab Enterprise 笔记本运行,请参阅 gcloud CLI 文档

    分享笔记本运行的结果

    您可以通过提供对笔记本运行所在 Cloud Storage 存储桶的访问权限来共享笔记本运行结果。提供此访问权限也会授予用户对同一 Cloud Storage 存储桶中的其他所有资源的访问权限(请参阅安全注意事项)。

    如需了解详情,请参阅 Cloud Storage 的共享和协作页面

    安全注意事项

    笔记本运行结果以笔记本 (IPYNB) 文件形式存储在 Cloud Storage 存储桶中。授予对相应存储桶的访问权限时,请考虑以下事项:

    • 有权访问相应存储桶的任何人都可以查看笔记本文件的代码和笔记本运行结果。

    • 任何有权更改存储桶内容的用户都可以更改笔记本文件的内容。

    如果您的日程安排配置为使用个人凭据,则只有指定的用户能够修改或触发该日程安排。

    如果您的时间表配置为使用服务账号,则只有对该服务账号拥有 iam.serviceAccounts.actAs 权限的用户才能修改或触发该时间表。

    查看时间表详情

    您可以查看有关时间表的以下信息:

    • 相应时间表将结果存储到的 Cloud Storage 存储桶。
    • 开始时间和结束时间。
    • 频次。

    如需查看时间表详情,您可以使用 Google Cloud 控制台或 gcloud CLI。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 时间安排页面。

      前往“时间表”

    2. 点击相应时间表的名称。

      系统会打开时间表详情页面。

    3. 如需返回时间表页面,请点击  返回上一页

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • SCHEDULE:您的时间表 ID。
    • PROJECT_ID:您的项目 ID。
    • REGION:您的时间表所在的区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab schedules describe SCHEDULE \
        --project=PROJECT_ID \
        --region=REGION

    Windows (PowerShell)

    gcloud colab schedules describe SCHEDULE `
        --project=PROJECT_ID `
        --region=REGION

    Windows (cmd.exe)

    gcloud colab schedules describe SCHEDULE ^
        --project=PROJECT_ID ^
        --region=REGION

    如需详细了解如何从命令行查看 Colab Enterprise 安排,请参阅 gcloud CLI 文档

    暂停、恢复或删除时间表

    如需暂停、恢复或删除时间表,您可以使用 Google Cloud 控制台、gcloud CLI 或 Terraform。

    控制台

    1. 在 Google Cloud 控制台中,前往 Colab Enterprise 时间安排页面。

      前往“时间表”

    2. 选择时间表。

    3. 点击  暂停 继续 删除

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • ACTIONpauseresumedelete 之一。
    • SCHEDULE_ID:您的时间表 ID。
    • PROJECT_ID:您的项目 ID。
    • REGION:您的时间表所在的区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud colab schedules ACTION SCHEDULE_ID \
        --project=PROJECT_ID \
        --region=REGION

    Windows (PowerShell)

    gcloud colab schedules ACTION SCHEDULE_ID `
        --project=PROJECT_ID `
        --region=REGION

    Windows (cmd.exe)

    gcloud colab schedules ACTION SCHEDULE_ID ^
        --project=PROJECT_ID ^
        --region=REGION

    如需详细了解如何从命令行管理 Colab Enterprise 调度,请参阅 gcloud CLI 文档

    Terraform

    如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令。 如需了解详情,请参阅 Terraform 提供程序参考文档

    以下示例使用 google_colab_schedule Terraform 资源暂停或恢复时间表。

    如需使用此示例,请根据以下内容更改 desired_state 的值:

    • PAUSED 暂停时间表
    • ACTIVE 即可恢复时间表
    resource "google_colab_runtime_template" "my_runtime_template" {
      name = "{{index $.Vars "runtime_template_name"}}"
      display_name = "Runtime template"
      location = "us-central1"
    
      machine_spec {
        machine_type     = "e2-standard-4"
      }
    
      network_spec {
        enable_internet_access = true
      }
    }
    
    resource "google_storage_bucket" "output_bucket" {
      name          = "{{index $.Vars "bucket"}}"
      location      = "US"
      force_destroy = true
      uniform_bucket_level_access = true
    }
    
    resource "google_storage_bucket_object" "notebook" {
      name   = "hello_world.ipynb"
      bucket = google_storage_bucket.output_bucket.name
      content = <<EOF
        {
          "cells": [
            {
              "cell_type": "code",
              "execution_count": null,
              "metadata": {},
              "outputs": [],
              "source": [
                "print(\"Hello, World!\")"
              ]
            }
          ],
          "metadata": {
            "kernelspec": {
              "display_name": "Python 3",
              "language": "python",
              "name": "python3"
            },
            "language_info": {
              "codemirror_mode": {
                "name": "ipython",
                "version": 3
              },
              "file_extension": ".py",
              "mimetype": "text/x-python",
              "name": "python",
              "nbconvert_exporter": "python",
              "pygments_lexer": "ipython3",
              "version": "3.8.5"
            }
          },
          "nbformat": 4,
          "nbformat_minor": 4
        }
        EOF
    }
    
    resource "google_colab_schedule" "{{$.PrimaryResourceId}}" {
      display_name = "{{index $.Vars "display_name"}}"
      location = "{{index $.TestEnvVars "location"}}"
      max_concurrent_run_count = 2
      cron = "TZ=America/Los_Angeles * * * * *"
    
      desired_state = "PAUSED"
    
      create_notebook_execution_job_request {
        notebook_execution_job {
          display_name = "Notebook execution"
          gcs_notebook_source {
            uri = "gs://${google_storage_bucket_object.notebook.bucket}/${google_storage_bucket_object.notebook.name}"
            generation = google_storage_bucket_object.notebook.generation
          }
    
          notebook_runtime_template_resource_name = "projects/${google_colab_runtime_template.my_runtime_template.project}/locations/${google_colab_runtime_template.my_runtime_template.location}/notebookRuntimeTemplates/${google_colab_runtime_template.my_runtime_template.name}"
          gcs_output_uri = "gs://${google_storage_bucket.output_bucket.name}"
          service_account = "{{index $.TestEnvVars "service_account"}}"
          }
      }
    
      depends_on = [
        google_colab_runtime_template.my_runtime_template,
        google_storage_bucket.output_bucket,
      ]
    }
    

    后续步骤