|
| 1 | +--- |
| 2 | +title: 'Quickstart: Create a Python app' |
| 3 | +description: Get started with Azure App Service by deploying your first Python app to a Linux container in App Service. |
| 4 | +ms.topic: quickstart |
| 5 | +ms.date: 09/22/2020 |
| 6 | +ms.custom: seo-python-october2019, cli-validate, devx-track-python, devx-track-azurecli |
| 7 | +robots: noindex |
| 8 | +--- |
| 9 | + |
| 10 | +# Quickstart: Create a Python app in Azure App Service on Linux |
| 11 | + |
| 12 | +In this quickstart, you deploy a Python web app to [App Service on Linux](/app-service/overview#app-service-on-linux), Azure's highly scalable, self-patching web hosting service. You use the local [Azure command-line interface (CLI)](/cli/azure/install-azure-cli) on a Mac, Linux, or Windows computer to deploy a sample with either the Flask or Django frameworks. The web app you configure uses a free App Service tier, so you incur no costs in the course of this article. |
| 13 | + |
| 14 | +> [!TIP] |
| 15 | +> If you prefer using Visual Studio Code instead, follow our **[Visual Studio Code App Service quickstart](/azure/developer/python/tutorial-deploy-app-service-on-linux-01)**. |
| 16 | +
|
| 17 | +<details> |
| 18 | +<summary>Set up your initial environment</summary> |
| 19 | + |
| 20 | +1. Have an Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio). |
| 21 | +1. Install <a href="https://www.python.org/downloads/" target="_blank">Python 3.6 or higher</a>. |
| 22 | +1. Install the <a href="/cli/azure/install-azure-cli" target="_blank">Azure CLI</a> 2.0.80 or higher, with which you run commands in any shell to provision and configure Azure resources. |
| 23 | + |
| 24 | +Open a terminal window and check your Python version is 3.6 or higher: |
| 25 | + |
| 26 | +# [Bash](#tab/bash) |
| 27 | + |
| 28 | +```bash |
| 29 | +python3 --version |
| 30 | +``` |
| 31 | + |
| 32 | +# [PowerShell](#tab/powershell) |
| 33 | + |
| 34 | +```cmd |
| 35 | +py -3 --version |
| 36 | +``` |
| 37 | + |
| 38 | +# [Cmd](#tab/cmd) |
| 39 | + |
| 40 | +```cmd |
| 41 | +py -3 --version |
| 42 | +``` |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +Check that your Azure CLI version is 2.0.80 or higher: |
| 47 | + |
| 48 | +```azurecli |
| 49 | +az --version |
| 50 | +``` |
| 51 | + |
| 52 | +Then sign in to Azure through the CLI: |
| 53 | + |
| 54 | +```azurecli |
| 55 | +az login |
| 56 | +``` |
| 57 | + |
| 58 | +This command opens a browser to gather your credentials. When the command finishes, it shows JSON output containing information about your subscriptions. |
| 59 | + |
| 60 | +Once signed in, you can run Azure commands with the Azure CLI to work with resources in your subscription. |
| 61 | + |
| 62 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 63 | + |
| 64 | +</details> |
| 65 | + |
| 66 | +## Clone the sample |
| 67 | + |
| 68 | +Clone the sample repository using the following command and navigate into the sample folder. ([Install git](https://git-scm.com/downloads) if you don't have git already.) |
| 69 | + |
| 70 | +```terminal |
| 71 | +git clone https://github.com/Azure-Samples/python-docs-hello-world |
| 72 | +``` |
| 73 | + |
| 74 | +Then navigate into that folder: |
| 75 | + |
| 76 | +```terminal |
| 77 | +cd python-docs-hello-world |
| 78 | +``` |
| 79 | + |
| 80 | +The sample contains framework-specific code that Azure App Service recognizes when starting the app. For more information, see [Container startup process](/app-service/configure-language-python#container-startup-process). |
| 81 | + |
| 82 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 83 | + |
| 84 | +## Run the sample |
| 85 | + |
| 86 | +1. Make sure you're in the *python-docs-hello-world* folder. |
| 87 | + |
| 88 | +1. Create a virtual environment and install dependencies: |
| 89 | + |
| 90 | + [!include [virtual environment setup](includes/app-service-quickstart-python-venv.md)] |
| 91 | + |
| 92 | + If you encounter "[Errno 2] No such file or directory: 'requirements.txt'.", make sure you're in the *python-docs-hello-world* folder. |
| 93 | + |
| 94 | +1. Run the development server. |
| 95 | + |
| 96 | + ```terminal |
| 97 | + flask run |
| 98 | + ``` |
| 99 | + |
| 100 | + By default, the server assumes that the app's entry module is in *app.py*, as used in the sample. (If you use a different module name, set the `FLASK_APP` environment variable to that name.) |
| 101 | +
|
| 102 | +1. Open a web browser and go to the sample app at `http://localhost:5000/`. The app displays the message **Hello, World!**. |
| 103 | +
|
| 104 | +  |
| 105 | + |
| 106 | +1. In your terminal window, press **Ctrl**+**C** to exit the development server. |
| 107 | +
|
| 108 | +
|
| 109 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 110 | +
|
| 111 | +## Deploy the sample |
| 112 | +
|
| 113 | +Deploy the code in your local folder (*python-docs-hello-world*) using the `az webapp up` command: |
| 114 | +
|
| 115 | +```azurecli |
| 116 | +az webapp up --sku F1 --name <app-name> |
| 117 | +``` |
| 118 | + |
| 119 | +- If the `az` command isn't recognized, be sure you have the Azure CLI installed as described in [Set up your initial environment](#set-up-your-initial-environment). |
| 120 | +- If the `webapp` command isn't recognized, because that your Azure CLI version is 2.0.80 or higher. If not, [install the latest version](/cli/azure/install-azure-cli). |
| 121 | +- Replace `<app_name>` with a name that's unique across all of Azure (*valid characters are `a-z`, `0-9`, and `-`*). A good pattern is to use a combination of your company name and an app identifier. |
| 122 | +- The `--sku F1` argument creates the web app on the Free pricing tier. Omit this argument to use a faster premium tier, which incurs an hourly cost. |
| 123 | +- You can optionally include the argument `--location <location-name>` where `<location_name>` is an available Azure region. You can retrieve a list of allowable regions for your Azure account by running the [`az account list-locations`](/cli/azure/appservice#az-appservice-list-locations) command. |
| 124 | +- If you see the error, "Could not auto-detect the runtime stack of your app," make sure you're running the command in the *python-docs-hello-world* folder (Flask) or the *python-docs-hello-django* folder (Django) that contains the *requirements.txt* file. (See [Troubleshooting auto-detect issues with az webapp up](https://github.com/Azure/app-service-linux-docs/blob/master/AzWebAppUP/runtime_detection.md) (GitHub).) |
| 125 | + |
| 126 | +The command may take a few minutes to complete. While running, it provides messages about creating the resource group, the App Service plan and hosting app, configuring logging, then performing ZIP deployment. It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure. |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 131 | + |
| 132 | +[!include [az webapp up command note](includes/app-service-web-az-webapp-up-note.md)] |
| 133 | + |
| 134 | +## Browse to the app |
| 135 | + |
| 136 | +Browse to the deployed application in your web browser at the URL `http://<app-name>.azurewebsites.net`. It takes a few moments to start the app initially. |
| 137 | + |
| 138 | +The Python sample code is running a Linux container in App Service using a built-in image. |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +**Congratulations!** You've deployed your Python app to App Service. |
| 143 | + |
| 144 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 145 | + |
| 146 | +## Redeploy updates |
| 147 | + |
| 148 | +In this section, you make a small code change and then redeploy the code to Azure. The code change includes a `print` statement to generate logging output that you work with in the next section. |
| 149 | + |
| 150 | +Open *app.py* in an editor and update the `hello` function to match the following code. |
| 151 | + |
| 152 | +```python |
| 153 | +def hello(): |
| 154 | + print("Handling request to home page.") |
| 155 | + return "Hello, Azure!" |
| 156 | +``` |
| 157 | + |
| 158 | + |
| 159 | +Save your changes, then redeploy the app using the `az webapp up` command again: |
| 160 | + |
| 161 | +```azurecli |
| 162 | +az webapp up |
| 163 | +``` |
| 164 | + |
| 165 | +This command uses values that are cached locally in the *.azure/config* file, including the app name, resource group, and App Service plan. |
| 166 | + |
| 167 | +Once deployment is complete, switch back to the browser window open to `http://<app-name>.azurewebsites.net`. Refresh the page, which should display the modified message: |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 172 | + |
| 173 | +> [!TIP] |
| 174 | +> Visual Studio Code provides powerful extensions for Python and Azure App Service, which simplify the process of deploying Python web apps to App Service. For more information, see [Deploy Python apps to App Service from Visual Studio Code](/azure/python/tutorial-deploy-app-service-on-linux-01). |
| 175 | +
|
| 176 | +## Stream logs |
| 177 | + |
| 178 | +You can access the console logs generated from inside the app and the container in which it runs. Logs include any output generated using `print` statements. |
| 179 | + |
| 180 | +To stream logs, run the [az webapp log tail](/cli/azure/webapp/log?view=azure-cli-latest&preserve-view=true#az_webapp_log_tail) command: |
| 181 | + |
| 182 | +```azurecli |
| 183 | +az webapp log tail |
| 184 | +``` |
| 185 | + |
| 186 | +You can also include the `--logs` parameter with then `az webapp up` command to automatically open the log stream on deployment. |
| 187 | + |
| 188 | +Refresh the app in the browser to generate console logs, which include messages describing HTTP requests to the app. If no output appears immediately, try again in 30 seconds. |
| 189 | + |
| 190 | +You can also inspect the log files from the browser at `https://<app-name>.scm.azurewebsites.net/api/logs/docker`. |
| 191 | + |
| 192 | +To stop log streaming at any time, press **Ctrl**+**C** in the terminal. |
| 193 | + |
| 194 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 195 | + |
| 196 | +## Manage the Azure app |
| 197 | + |
| 198 | +Go to the <a href="https://portal.azure.com" target="_blank">Azure portal</a> to manage the app you created. Search for and select **App Services**. |
| 199 | + |
| 200 | + |
| 201 | + |
| 202 | +Select the name of your Azure app. |
| 203 | + |
| 204 | + |
| 205 | + |
| 206 | +Selecting the app opens its **Overview** page, where you can perform basic management tasks like browse, stop, start, restart, and delete. |
| 207 | + |
| 208 | + |
| 209 | + |
| 210 | +The App Service menu provides different pages for configuring your app. |
| 211 | + |
| 212 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 213 | + |
| 214 | +## Clean up resources |
| 215 | + |
| 216 | +In the preceding steps, you created Azure resources in a resource group. The resource group has a name like "appsvc_rg_Linux_CentralUS" depending on your location. If you use an App Service SKU other than the free F1 tier, these resources incur ongoing costs (see [App Service pricing](https://azure.microsoft.com/pricing/details/app-service/linux/)). |
| 217 | + |
| 218 | +If you don't expect to need these resources in the future, delete the resource group by running the following command: |
| 219 | + |
| 220 | +```azurecli |
| 221 | +az group delete --no-wait |
| 222 | +``` |
| 223 | + |
| 224 | +The command uses the resource group name cached in the *.azure/config* file. |
| 225 | + |
| 226 | +The `--no-wait` argument allows the command to return before the operation is complete. |
| 227 | + |
| 228 | +[Having issues? Let us know.](https://aka.ms/FlaskCLIQuickstartHelp) |
| 229 | + |
| 230 | +## Next steps |
| 231 | + |
| 232 | +> [!div class="nextstepaction"] |
| 233 | +> [Configure Python app](/app-service/configure-language-python) |
| 234 | +
|
| 235 | +> [!div class="nextstepaction"] |
| 236 | +> [Add user sign-in to a Python web app](/active-directory/develop/quickstart-v2-python-webapp) |
| 237 | +
|
| 238 | +> [!div class="nextstepaction"] |
| 239 | +> [Tutorial: Run Python app in custom container](/app-service/tutorial-custom-container) |
0 commit comments