

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 將資訊傳遞至筆記本步驟以及從中傳遞資訊
<a name="create-notebook-auto-run-dag-seq"></a>

下列各節描述將資訊作為環境變數和參數傳遞至筆記本的方法。

## 傳遞環境變數
<a name="create-notebook-auto-run-dag-seq-env-var"></a>

將環境變數做為字典傳遞至 `NotebookJobStep` 的 `environment_variable` 引數，如下列範例所示：

```
environment_variables = {"RATE": 0.0001, "BATCH_SIZE": 1000}

notebook_job_step = NotebookJobStep(
    ...
    environment_variables=environment_variables,
    ...
)
```

您可以在使用 `os.getenv()` 的筆記本中使用環境變數，如下列範例所示：

```
# inside your notebook
import os
print(f"ParentNotebook: env_key={os.getenv('env_key')}")
```

## 傳遞參數
<a name="create-notebook-auto-run-dag-seq-param"></a>

當您將參數傳遞至 `NotebookJobStep` 執行個體中的第一個筆記本任務步驟時，您可能會選擇性地想要在 Jupyter 筆記本中標記儲存格，以指出要套用新參數或參數覆寫的位置。如需如何在 Jupyter 筆記本中標記儲存格的指示，請參閱[對筆記本進行參數化](notebook-auto-run-troubleshoot-override.md)。

您可以透過筆記本任務步驟的 `parameters` 參數傳遞參數，如下列程式碼片段所示：

```
notebook_job_parameters = {
    "company": "Amazon",
}

notebook_job_step = NotebookJobStep(
    ...
    parameters=notebook_job_parameters,
    ...
)
```

在輸入筆記本內，如果您沒有標記的儲存格，則會在標記有 `parameters` 的儲存格之後或在筆記本的開頭套用您的參數。

```
# this cell is in your input notebook and is tagged with 'parameters'
# your parameters and parameter overrides are applied after this cell
company='default'
```

```
# in this cell, your parameters are applied
# prints "company is Amazon"
print(f'company is {company}')
```

## 從上一個步驟擷取資訊
<a name="create-notebook-auto-run-dag-seq-interstep"></a>

下列討論說明如何從上一個步驟擷取要傳遞至筆記本任務步驟的資訊。

**使用 `properties` 屬性**

您可以使用下列屬性搭配上一個步驟的 `properties` 屬性：
+ `ComputingJobName` - 訓練任務名稱
+ `ComputingJobStatus` - 訓練任務狀態
+ `NotebookJobInputLocation` - 輸入 Amazon S3 位置
+ `NotebookJobOutputLocationPrefix` - 訓練任務輸出的路徑，特別是 `{NotebookJobOutputLocationPrefix}/{training-job-name}/output/output.tar.gz`。包含輸出
+ `InputNotebookName` - 輸入筆記本檔案名稱
+ `OutputNotebookName` - 輸出筆記本檔案名稱 (如果任務失敗，該名稱可能不存在於訓練任務輸出資料夾中)

下列程式碼片段展示如何從屬性擷取參數。

```
notebook_job_step2 = NotebookJobStep(
    ....
    parameters={
        "step1_JobName": notebook_job_step1.properties.ComputingJobName,
        "step1_JobStatus": notebook_job_step1.properties.ComputingJobStatus,
        "step1_NotebookJobInput": notebook_job_step1.properties.NotebookJobInputLocation,
        "step1_NotebookJobOutput": notebook_job_step1.properties.NotebookJobOutputLocationPrefix,
    }
```

**使用 JsonGet**

如果您想要傳遞上述以外的參數，且上一個步驟的 JSON 輸出位於 Amazon S3 中，請使用 `JsonGet`。`JsonGet` 是一般機制，可以直接從 Amazon S3 中的 JSON 檔案擷取資料。

若要使用 `JsonGet` 擷取 Amazon S3 中的 JSON 檔案，請完成下列步驟：

1. 將您的 JSON 檔案上傳至 Amazon S3。如果您的資料已上傳至 Amazon S3，請略過此步驟。下列範例示範如何將 JSON 檔案上傳至 Amazon S3。

   ```
   import json
   from sagemaker.s3 import S3Uploader
   
   output = {
       "key1": "value1", 
       "key2": [0,5,10]
   }
               
   json_output = json.dumps(output)
   
   with open("notebook_job_params.json", "w") as file:
       file.write(json_output)
   
   S3Uploader.upload(
       local_path="notebook_job_params.json",
       desired_s3_uri="s3://path/to/bucket"
   )
   ```

1. 提供您要擷取之值的 S3 URI 和 JSON 路徑。在下列範例中，`JsonGet` 會傳回物件，代表與索引鍵 `key2` (`10`) 相關聯值的索引 2。

   ```
   NotebookJobStep(
       ....
       parameters={
           # the key job_key1 returns an object representing the value 10
           "job_key1": JsonGet(
               s3_uri=Join(on="/", values=["s3:/", ..]),
               json_path="key2[2]" # value to reference in that json file
           ), 
           "job_key2": "Amazon" 
       }
   )
   ```