|
| 1 | +--- |
| 2 | +title: Parameterization of Notebooks in Azure Data Studio |
| 3 | +description: This tutorial shows how you can create a parameterized notebook in ADS. |
| 4 | +ms.topic: how-to |
| 5 | +ms.prod: azure-data-studio |
| 6 | +ms.technology: azure-data-studio |
| 7 | +author: vasubhog |
| 8 | +ms.author: vasubhog |
| 9 | +ms.reviewer: mikeray, alayu, maghan |
| 10 | +ms.custom: "" |
| 11 | +ms.date: 01/25/2021 |
| 12 | +--- |
| 13 | + |
| 14 | +# Create a Parameterized Notebook |
| 15 | + |
| 16 | +**Parameterization** is the ability to execute the same notebook with different parameters. |
| 17 | + |
| 18 | +This article shows you how to create and run a parameterized notebook in Azure Data Studio using the python kernel. |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +- [Azure Data Studio](../download-azure-data-studio.md) |
| 23 | +- [Python](https://www.python.org/downloads/) |
| 24 | + |
| 25 | +## Install and set up Papermill in Azure Data Studio |
| 26 | + |
| 27 | +The steps in this section all run within an Azure Data Studio notebook. |
| 28 | + |
| 29 | +1. Create a new notebook and change the **Kernel** to *Python 3*. |
| 30 | + |
| 31 | +  |
| 32 | + |
| 33 | +2. You may be prompted to upgrade your Python packages when your packages need updating. |
| 34 | + |
| 35 | +  |
| 36 | + |
| 37 | +3. Install Papermill: |
| 38 | + |
| 39 | + ```python |
| 40 | + import sys |
| 41 | + !{sys.executable} -m pip install papermill --no-cache-dir --upgrade |
| 42 | + ``` |
| 43 | + |
| 44 | + Verify it's installed: |
| 45 | + |
| 46 | + ```python |
| 47 | + import sys |
| 48 | + !{sys.executable} -m pip list |
| 49 | + ``` |
| 50 | + |
| 51 | + :::image type="content" source="media/notebooks-parameterization/install-list-papermill.png" alt-text="List"::: |
| 52 | + |
| 53 | +5. You can test if papermill is loaded properly by checking the version of papermill. |
| 54 | + |
| 55 | + ```python |
| 56 | + import papermill |
| 57 | + papermill |
| 58 | + ``` |
| 59 | + |
| 60 | + :::image type="content" source="media/notebooks-parameterization/install-validation-papermill.png" alt-text="Validation"::: |
| 61 | + |
| 62 | +## Set up a parameterized notebook |
| 63 | + |
| 64 | +1. Verify the **Kernel** is set to *Python3*. |
| 65 | + |
| 66 | +  |
| 67 | + |
| 68 | +2. Create a New Code Cell and Tag as **Parameters Cell**. |
| 69 | + |
| 70 | + ```python |
| 71 | + x = 2.0 |
| 72 | + y = 5.0 |
| 73 | + ``` |
| 74 | + |
| 75 | + :::image type="content" source="media/notebooks-parameterization/make-parameter-cell.png" alt-text="Parameter Cell Notebook"::: |
| 76 | + |
| 77 | +3. Add other cells to test different parameters. |
| 78 | + |
| 79 | + ```python |
| 80 | + addition = x + y |
| 81 | + multiply = x * y |
| 82 | + ``` |
| 83 | + |
| 84 | + ```python |
| 85 | + print("Addition: " + str(addition)) |
| 86 | + print("Multiplication: " + str(multiply)) |
| 87 | + ``` |
| 88 | + |
| 89 | + Cells in Example Input Notebook: |
| 90 | + :::image type="content" source="media/notebooks-parameterization/test-cells.png" alt-text="Additional Input Notebook Cells"::: |
| 91 | + |
| 92 | +4. Save notebook as **Input.ipynb**. |
| 93 | + :::image type="content" source="media/notebooks-parameterization/save-notebook.png" alt-text="Save Notebook"::: |
| 94 | + |
| 95 | +## How to execute Papermill notebook |
| 96 | + |
| 97 | +Papermill can be executed two ways: |
| 98 | + |
| 99 | +- Command Line Interface (CLI) |
| 100 | +- Python API |
| 101 | + |
| 102 | +### Parameterized CLI execution |
| 103 | + |
| 104 | +To execute a notebook using the CLI, enter the papermill command in the terminal with the input notebook, location for output notebook, and options. |
| 105 | + |
| 106 | +> [!Note] |
| 107 | + > Papermill Command Line Interface Documentation can be found [here](https://papermill.readthedocs.io/en/latest/usage-execute.html#execute-via-cli). |
| 108 | +
|
| 109 | +1. Execute Input Notebook with new parameters. |
| 110 | + |
| 111 | + ```shell |
| 112 | + papermill Input.ipynb Output.ipynb -p x 10 -p y 20 |
| 113 | + ``` |
| 114 | + |
| 115 | + This will execute the Input Notebook with new values for parameters **x** and **y**. |
| 116 | + |
| 117 | +2. After Execution view new Output Parameterized Notebook. |
| 118 | + You can note that there's a new cell labeled **# Injected-Parameters** containing the new parameter values passed in via CLI. |
| 119 | + |
| 120 | + :::image type="content" source="media/notebooks-parameterization/output-notebook.png" alt-text="Output Notebook"::: |
| 121 | + |
| 122 | +### Parameterized Python API execution |
| 123 | + |
| 124 | +> [!Note] |
| 125 | + > Papermill Python API Documentation can be found [here](https://papermill.readthedocs.io/en/latest/usage-execute.html#execute-via-the-python-api). |
| 126 | +
|
| 127 | +1. Create a new notebook and change the **Kernel** to *Python 3*. |
| 128 | +  |
| 129 | + |
| 130 | +2. Add a new code cell and use papermill to use the execute method. |
| 131 | + |
| 132 | + ```python |
| 133 | + import papermill as pm |
| 134 | + |
| 135 | + pm.execute_notebook( |
| 136 | + '/Users/vasubhog/GitProjects/AzureDataStudio-Notebooks/Demo_Parameterization/Input.ipynb', |
| 137 | + '/Users/vasubhog/GitProjects/AzureDataStudio-Notebooks/Demo_Parameterization/Output.ipynb', |
| 138 | + parameters = dict(x = 10, y = 20) |
| 139 | + ) |
| 140 | + ``` |
| 141 | + |
| 142 | +  |
| 143 | + |
| 144 | +3. After Execution view new Output Parameterization Notebook. |
| 145 | + |
| 146 | + You can note that there's a new cell labeled **# Injected-Parameters** containing the new parameter values passed in via CLI. |
| 147 | + |
| 148 | + :::image type="content" source="media/notebooks-parameterization/output-notebook.png" alt-text="Output Notebook"::: |
| 149 | + |
| 150 | +## Next steps |
| 151 | + |
| 152 | +Learn more about notebooks and Parameterization: |
| 153 | + |
| 154 | +- [How to use notebooks in Azure Data Studio](./notebooks-guidance.md) |
| 155 | +- [Papermill Parameterization Docs](https://papermill.readthedocs.io/en/latest/index.html) |
0 commit comments