使用 Papermill 建立參數化的筆記本

Azure Data Studio 中的參數化是使用一組不同的參數來執行相同的筆記本。

本文說明如何使用 Python 核心在 Azure Data Studio 中建立並執行參數化筆記本。

注意

目前,您可以使用參數化搭配 Python、PySpark、PowerShell 和 .NET 互動式核心。

必要條件

在 Azure Data Studio 中安裝及設定 Papermill

本節中的所有步驟都會在 Azure Data Studio 筆記本內部執行。

  1. 建立新的 Notebook。 將 [核心] 變更為 [Python 3]:

    Screenshot that shows the New notebook menu option and setting the Kernel value to Python 3.

  2. 如果在您的套件需要更新時,系統提示您升級 Python 套件,則請選取 [是]:

    Screenshot that shows the dialog prompt to update Python packages.

  3. 安裝 Papermill:

    import sys
    !{sys.executable} -m pip install papermill --no-cache-dir --upgrade
    

    確認已安裝 Papermill:

    import sys
    !{sys.executable} -m pip list
    

    Screenshot that shows selecting Papermill in a list of application names.

  4. 若要確認已正確安裝 Papermill,請檢查 Papermill 的版本:

    import papermill
    papermill
    

    Screenshot that shows installation validation for Papermill.

參數化範例

您可以使用範例筆記本檔案來瀏覽本文中的步驟:

  1. 移至 GitHub 中的筆記本檔案。 選取 [Raw]。
  2. 選取 Ctrl+S 或按一下滑鼠右鍵,然後使用 .ipynb 副檔名儲存檔案。
  3. 在 Azure Data Studio 中開啟檔案。

設定參數化筆記本

您可以從 Azure Data Studio 中開啟的範例筆記本開始,或完成下列步驟來建立筆記本。 然後,嘗試使用不同的參數。 所有步驟都會在 Azure Data Studio 筆記本內執行。

  1. 驗證核心已設為 Python 3

    Screenshot that shows the Kernel value to Python 3.

  2. 製作新的程式碼儲存格。 選取 [參數] 以將儲存格標記為參數儲存格。

    x = 2.0
    y = 5.0
    

    Screenshot that shows creating a new parameters cell with Parameters selected.

  3. 新增其他儲存格,以測試不同的參數:

    addition = x + y
    multiply = x * y
    
    print("Addition: " + str(addition))
    print("Multiplication: " + str(multiply))
    

    執行所有儲存格之後,輸出看起來會類似下列範例:

    Screenshot that shows the output of cells added to test new parameters.

  4. 將筆記本儲存為 Input.ipynb:

    Screenshot that shows saving the notebook file.

執行 Papermill 筆記本

您可以透過兩種方式來執行 Papermill:

  • 命令列介面 (CLI)
  • Python API

參數化 CLI 執行

若要使用 CLI 執行筆記本,請在終端機中,使用輸入筆記本來輸入 papermill 命令、輸出筆記本的位置和選項。

注意

若要深入了解,請參閱 Papermill CLI 文件

  1. 使用新的參數執行輸入筆記本:

    papermill Input.ipynb Output.ipynb -p x 10 -p y 20
    

    此命令會使用 xy 參數的新值來執行輸入筆記本。

  2. 標示 # Injected-Parameters 的新儲存格會包含透過 CLI 傳入的新參數值。 新的 # Injected-Parameters 值會用於最後一個儲存格中顯示的新輸出:

    Screenshot that shows the output for new parameters.

參數化 Python API 執行

注意

若要深入了解,請參閱 Papermill Python 文件

  1. 建立新的 Notebook。 將 [核心] 變更為 [Python 3]:

    Screenshot that shows the New notebook menu option and setting the Kernel value to Python 3.

  2. 新增程式碼資料格。 然後,使用 Papermill Python API 來執行及產生輸出參數化筆記本:

    import papermill as pm
    
    pm.execute_notebook(
    '/Users/vasubhog/GitProjects/AzureDataStudio-Notebooks/Demo_Parameterization/Input.ipynb',
    '/Users/vasubhog/GitProjects/AzureDataStudio-Notebooks/Demo_Parameterization/Output.ipynb',
    parameters = dict(x = 10, y = 20)
    )
    

    Screenshot that shows the Python API execution.

  3. 標示 # Injected-Parameters 的新儲存格會包含傳入的新參數值。 新的 # Injected-Parameters 值會用於最後一個儲存格中顯示的新輸出:

    Screenshot that shows the output for new parameters.

下一步

深入了解筆記本和參數化: