You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Create Python app on Linux - Azure App Service | Microsoft Docs
2
+
title: 'Quickstart: Create Python app on Linux - Azure App Service'
3
3
description: Deploy your first Python hello world app in Azure App Service on Linux in minutes.
4
4
services: app-service\web
5
5
documentationcenter: ''
@@ -12,160 +12,228 @@ ms.service: app-service-web
12
12
ms.workload: web
13
13
ms.tgt_pltfrm: na
14
14
ms.topic: quickstart
15
-
ms.date: 08/23/2019
16
-
ms.author: cephalin
17
-
ms.custom: mvc
18
-
ms.custom: seodec18
15
+
ms.date: 11/11/2019
16
+
ms.author: msangapu
17
+
ms.custom: seo-python-october2019
19
18
20
19
experimental: false
21
-
experiment_id: 1e304dc9-5add-4b
20
+
experiment_id: 01a9132f-eaab-4c
22
21
---
23
-
# Create a Python app in Azure App Service on Linux
22
+
# Quickstart: Create a Python app in Azure App Service on Linux
24
23
25
-
In this quickstart, you deploy a simple Python app to [App Service on Linux](app-service-linux-intro.md), which provides a highly scalable, self-patching web hosting service. You use the Azure command-line interface (the [Azure CLI](/cli/azure/install-azure-cli)) through the interactive, browser-based Azure Cloud Shell, so you can follow the steps use a Mac, Linux, or Windows computer.
24
+
In this quickstart, you deploy a Python web app to [App Service on Linux](app-service-linux-intro.md), 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. The web app you configure uses a free App Service tier, so you incur no costs in the course of this article.
26
25
27
-

26
+
If you prefer to deploy apps through an IDE, see [Deploy Python apps to App Service from Visual Studio Code](/azure/python/tutorial-deploy-app-service-on-linux-01).
- Azure subscription - [create one for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio)
31
+
- <ahref="https://www.python.org/downloads/"target="_blank">Python 3.7</a> (Python 3.6 is also supported)
While running, it displays information similar to the following example:
49
+
The repository contains an *application.py* file, which tells App Service that the code contains a Flask app. For more information, see [Container startup process and customizations](how-to-configure-python.md).
The Azure CLI provides you with many convenient commands that you use from a local terminal to provision and manage Azure resources from the command line. You can use commands to complete the same tasks that you would through the Azure portal in a browser. You can also use CLI commands in scripts to automate management processes.
54
+
55
+
To run Azure commands in the Azure CLI, you must first sign in using the `az login` command. This command opens a browser to gather your credentials.
56
+
57
+
```terminal
58
+
az login
57
59
```
58
60
59
-
## Create a web app
61
+
## Deploy the sample
60
62
61
-
Change to the directory that contains the sample code and run the `az webapp up` command.
63
+
The [`az webapp up`](/cli/azure/webapp#az-webapp-up) command creates the web app on App Service and deploys your code.
62
64
63
-
In the following example, replace *\<app_name>* with a globally unique app name (valid characters are `a-z`, `0-9`, and `-`).
65
+
In the *python-docs-hello-world* folder that contains the sample code, run the following `az webapp up` command. Replace `<app-name>` with a globally unique app name (*valid characters are `a-z`, `0-9`, and `-`*). Also replace `<location-name>` with an Azure region such as **centralus**, **eastasia**, **westeurope**, **koreasouth**, **brazilsouth**, **centralindia**, and so on. (You can retrieve a list of allowable regions for your Azure account by running the [`az account locations-list`](/cli/azure/appservice?view=azure-cli-latest.md#az-appservice-list-locations) command.)
64
66
65
-
```bash
66
-
cd python-docs-hello-world
67
67
68
-
az webapp up -n <app_name>
68
+
```terminal
69
+
az webapp up --sku F1 -n <app-name> -l <location-name>
69
70
```
70
71
71
-
This command may take a few minutes to run. While running, it displays information similar to the following example:
72
+
This command may take a few minutes complete run. While running, it displays information similar to the following example:
72
73
73
-
```json
74
+
```output
74
75
The behavior of this command has been altered by the following extension: webapp
75
-
Creating Resource group 'appsvc_rg_Linux_CentralUS' ...
76
+
Creating Resource group 'appsvc_rg_Linux_centralus' ...
76
77
Resource group creation complete
77
-
Creating App service plan 'appsvc_asp_Linux_CentralUS' ...
78
+
Creating App service plan 'appsvc_asp_Linux_centralus' ...
78
79
App service plan creation complete
79
-
Creating app '<app_name>' ....
80
+
Creating app '<app-name>' ....
80
81
Webapp creation complete
81
82
Creating zip with contents of dir /home/username/quickstart/python-docs-hello-world ...
The `az webapp up` command does the following actions:
99
+
[!INCLUDE [AZ Webapp Up Note](../../../includes/app-service-web-az-webapp-up-note.md)]
99
100
100
-
- Create a default resource group.
101
+
## Browse to the app
101
102
102
-
- Create a default app service plan.
103
+
Browse to the deployed application in your web browser at the URL `http://<app-name>.azurewebsites.net`.
103
104
104
-
- Create an app with the specified name.
105
+
The Python sample code is running a Linux container in App Service using a built-in image.
105
106
106
-
-[Zip deploy](https://docs.microsoft.com/azure/app-service/deploy-zip) files from the current working directory to the app.
107
+

107
108
108
-
## Browse to the app
109
+
**Congratulations!** You've deployed your Python app to App Service on Linux.
110
+
111
+
## Run the sample app locally
112
+
113
+
In a terminal window, use the commands below (as appropriate for your operating system) to install the required dependencies and launch the built-in development server.
109
114
110
-
Browse to the deployed application using your web browser.
115
+
# [Bash](#tab/bash)
111
116
112
117
```bash
113
-
http://<app_name>.azurewebsites.net
118
+
python3 -m venv venv
119
+
source venv/bin/activate
120
+
pip install -r requirements.txt
121
+
FLASK_APP=application.py
122
+
flask run
114
123
```
115
124
116
-
The Python sample code is running in App Service on Linux with a built-in image.
125
+
# [PowerShell](#tab/powershell)
126
+
127
+
```powershell
128
+
py -3 -m venv env
129
+
env\scripts\activate
130
+
pip install -r requirements.txt
131
+
Set-Item Env:FLASK_APP ".\application.py"
132
+
flask run
133
+
```
134
+
135
+
# [Cmd](#tab/cmd)
136
+
137
+
```cmd
138
+
py -3 -m venv env
139
+
env\scripts\activate
140
+
pip install -r requirements.txt
141
+
SET FLASK_APP=application.py
142
+
flask run
143
+
```
117
144
118
-

145
+
---
119
146
120
-
**Congratulations!** You've deployed your first Python app to App Service on Linux.
147
+
Open a web browser, and go to the sample app at `http://localhost:5000/`. The app displays the message **Hello World!**.
121
148
122
-
## Update and redeploy the code
149
+

123
150
124
-
In the Cloud Shell, type `code application.py`to open the Cloud Shell editor.
151
+
In your terminal window, press **Ctrl**+**C**to exit the web server.
Make a small change to the text in the call to `return`:
155
+
In your favorite code editor, open *application.py* and change the `return` statement on the last line to match the following code. The `print` statement is included here to generate logging output that you work with in the next section.
129
156
130
157
```python
158
+
print("Handling request to home page.")
131
159
return"Hello Azure!"
132
160
```
133
161
134
-
Save your changes and exit the editor. Use the command `^S` to save and `^Q` to exit.
162
+
Save your changes and exit the editor.
135
163
136
-
You'll now redeploy the app. Substitute `<app_name>` with your app.
164
+
Redeploy the app using the following `az webapp up` command, using the same command you used to deploy the app the first time, replacing `<app-name>`and `<location-name>`with the same names you used before.
137
165
138
-
```bash
139
-
az webapp up -n <app_name>
166
+
```terminal
167
+
az webapp up --sku F1 -n <app-name> -l <location-name>
140
168
```
141
169
142
-
Once deployment has completed, switch back to the browser window that opened in the **Browse to the app** step, and refresh the page.
170
+
Once deployment has completed, switch back to the browser window open to `http://<app-name>.azurewebsites.net` and refresh the page, which should display the modified message:
171
+
172
+

143
173
144
-

174
+
> [!TIP]
175
+
> 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).
145
176
146
-
## Manage your new Azure app
177
+
## Stream logs
147
178
148
-
Go to the <ahref="https://portal.azure.com"target="_blank">Azure portal</a> to manage the app you created.
179
+
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.
180
+
181
+
First, turn on container logging by running the following command in a terminal, replacing `<app-name>` with the name of your app and `<resource-group-name>` with the name of the resource group shown in the output of the `az webapp up` command you used (such as "appsvc_rg_Linux_centralus"):
182
+
183
+
```terminal
184
+
az webapp log config --name <app-name> --resource-group <resource-group-name> --docker-container-logging filesystem
185
+
```
149
186
150
-
From the left menu, click **App Services**, and then click the name of your Azure app.
187
+
Once container logging is turned on, run the following command to show the log stream:
188
+
189
+
```terminal
190
+
az webapp log tail --name <app-name> --resource-group <resource-group-name>
191
+
```
192
+
193
+
Refresh the app in the browser to generate console logs, which should include lines similar to the following text. If you don't see output immediately, try again in 30 seconds.
194
+
195
+
```output
196
+
2019-10-23T12:40:03.815574424Z Handling request to home page.

200
+
You can also inspect the log files from the browser at `https://<app-name>.scm.azurewebsites.net/api/logs/docker`.
201
+
202
+
To stop log streaming at any time, type `Ctrl`+`C`.
203
+
204
+
## Manage the Azure app
205
+
206
+
Go to the <ahref="https://portal.azure.com"target="_blank">Azure portal</a> to manage the app you created. Search for and select **App Services**.
207
+
208
+

209
+
210
+
Select the name of your Azure app.
211
+
212
+

153
213
154
214
You see your app's Overview page. Here, you can perform basic management tasks like browse, stop, start, restart, and delete.
155
215
156
-

216
+

217
+
218
+
The App Service menu provides different pages for configuring your app.
219
+
220
+
## Clean up resources
157
221
158
-
The left menu provides different pages for configuring your app.
222
+
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 will incur ongoing costs.
If you don't expect to need these resources in the future, delete the resource group by running the following command, replacing `<resource-group-name>` with the resource group shown in the output of the `az webapp up` command, such as "appsvc_rg_Linux_centralus". The command may take a minute to complete.
225
+
226
+
```terminal
227
+
az group delete -n <resource-group-name>
228
+
```
161
229
162
230
## Next steps
163
231
164
232
> [!div class="nextstepaction"]
165
-
> [Tutorial: Python app with PostgreSQL](tutorial-python-postgresql-app.md)
233
+
> [Tutorial: Python (Django) web app with PostgreSQL](tutorial-python-postgresql-app.md)
0 commit comments