排定筆記本執行時間

本頁面說明如何在 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. Verify 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. Verify 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. 在「Region」(區域) 選單中,選取包含筆記本的區域。

    3. 在筆記本旁邊,按一下「筆記本動作」選單,然後選取「排定時間」

    4. 在「排程名稱」欄位中,輸入排程名稱。

    5. 按一下「執行階段範本」清單,然後選取執行階段範本。執行階段範本會決定執行筆記本的執行階段規格。

    6. 在「執行排程」下方,選取「一次性」 ,即可在提交筆記本執行作業後立即執行筆記本。

    7. 在「Cloud Storage output location」(Cloud Storage 輸出位置) 欄位旁邊,按一下「Browse」(瀏覽),開啟「Select folder」(選取資料夾) 對話方塊。

    8. 選取 Cloud Storage 值區。如要建立 bucket,請按一下「建立新 bucket」,然後完成對話方塊。

    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 SDK for Python 時,系統會一併安裝 Vertex AI Python 用戶端程式庫。詳情請參閱 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. 在「Region」(區域) 選單中,選取包含筆記本的區域。

    3. 在筆記本旁邊,按一下「筆記本動作」選單,然後選取「排定時間」

    4. 在「排程名稱」欄位中,輸入排程名稱。

    5. 按一下「執行階段範本」清單,然後選取執行階段範本。執行階段範本會決定執行筆記本的執行階段規格。

    6. 在「執行時間表」下方,選取「週期性」 ,即可排定筆記本在特定時間間隔執行。

    7. 填寫排程對話方塊。

    8. 在「Cloud Storage output location」(Cloud Storage 輸出位置) 欄位旁邊,按一下「Browse」(瀏覽),開啟「Select folder」(選取資料夾) 對話方塊。

    9. 選取 Cloud Storage 值區。如要建立 bucket,請按一下「建立新 bucket」,然後完成對話方塊。

    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 SDK for Python 時,系統會一併安裝 Vertex AI Python 用戶端程式庫。詳情請參閱 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 控制台中,您可以在「Schedules」頁面查看排程。您可以在「執行作業」頁面中,查看已完成的筆記本執行作業結果。

    查看結果

    如要查看筆記本執行結果,可以使用 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 SDK for Python 時,系統會一併安裝 Vertex AI Python 用戶端程式庫。詳情請參閱 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. 按一下「Delete」(刪除) 圖示

    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 bucket,藉此分享筆記本執行結果。提供這項存取權時,也會授予使用者存取相同 Cloud Storage bucket 中任何其他資源的權限 (請參閱「安全性考量」)。

    詳情請參閱 Cloud Storage 的共用與協作頁面

    安全性考量

    筆記本執行結果會以筆記本 (IPYNB) 檔案的形式,儲存在 Cloud Storage bucket 中。授予這個值區的存取權時,請考量下列事項:

    • 只要能存取 Bucket,就能查看筆記本檔案的程式碼和筆記本執行結果。

    • 凡是有權變更 Bucket 內容的使用者,都能變更筆記本檔案的內容。

    如果排程設定為使用個人憑證,只有指定使用者可以修改或觸發排程。

    如果排程設定為使用服務帳戶,只有對服務帳戶具有 iam.serviceAccounts.actAs 權限的使用者,才能修改或觸發排程。

    查看時間表詳細資料

    您可以查看時間表的相關資訊,包括:

    • 排程儲存結果的 Cloud Storage bucket。
    • 開始和結束時間。
    • 頻率。

    如要查看排程詳細資料,請使用 Google Cloud 控制台或 gcloud CLI。

    控制台

    1. 前往 Google Cloud 控制台的 Colab Enterprise「Schedules」頁面。

      前往「時間表」

    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「Schedules」頁面。

      前往「時間表」

    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,
      ]
    }
    

    後續步驟