Skip to content

Commit a724825

Browse files
author
Mangesh Sangapu
committed
experiment
1 parent 805c285 commit a724825

File tree

2 files changed

+141
-73
lines changed

2 files changed

+141
-73
lines changed
Lines changed: 139 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Create Python app on Linux - Azure App Service | Microsoft Docs
2+
title: 'Quickstart: Create Python app on Linux - Azure App Service'
33
description: Deploy your first Python hello world app in Azure App Service on Linux in minutes.
44
services: app-service\web
55
documentationcenter: ''
@@ -12,160 +12,228 @@ ms.service: app-service-web
1212
ms.workload: web
1313
ms.tgt_pltfrm: na
1414
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
1918

2019
experimental: false
21-
experiment_id: 1e304dc9-5add-4b
20+
experiment_id: 01a9132f-eaab-4c
2221
---
23-
# Create a Python app in Azure App Service on Linux
22+
# Quickstart: Create a Python app in Azure App Service on Linux
2423

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.
2625

27-
![Sample app running in Azure](media/quickstart-python/hello-world-in-browser.png)
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).
2827

29-
[!INCLUDE [quickstarts-free-trial-note](../../../includes/quickstarts-free-trial-note.md)]
28+
## Prerequisites
3029

31-
[!INCLUDE [cloud-shell-try-it.md](../../../includes/cloud-shell-try-it.md)]
30+
- 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+
- <a href="https://www.python.org/downloads/" target="_blank">Python 3.7</a> (Python 3.6 is also supported)
32+
- <a href="https://git-scm.com/downloads" target="_blank">Git</a>
33+
- <a href="https://docs.microsoft.com/cli/azure/install-azure-cli" target="_blank">Azure CLI</a>
3234

3335
## Download the sample
3436

35-
In the Cloud Shell, create a quickstart directory and then change to it.
37+
In a terminal window, run the following command to clone the sample application to your local computer.
3638

37-
```bash
38-
mkdir quickstart
39-
40-
cd $HOME/quickstart
39+
```terminal
40+
git clone https://github.com/Azure-Samples/python-docs-hello-world
4141
```
4242

43-
Next, run the following command to clone the sample app repository to your quickstart directory.
43+
Then go into that folder:
4444

45-
```bash
46-
git clone https://github.com/Azure-Samples/python-docs-hello-world
45+
```terminal
46+
cd python-docs-hello-world
4747
```
4848

49-
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).
5050

51-
```bash
52-
Cloning into 'python-docs-hello-world'...
53-
remote: Enumerating objects: 43, done.
54-
remote: Total 43 (delta 0), reused 0 (delta 0), pack-reused 43
55-
Unpacking objects: 100% (43/43), done.
56-
Checking connectivity... done.
51+
## Sign in to Azure
52+
53+
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
5759
```
5860

59-
## Create a web app
61+
## Deploy the sample
6062

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.
6264

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.)
6466

65-
```bash
66-
cd python-docs-hello-world
6767

68-
az webapp up -n <app_name>
68+
```terminal
69+
az webapp up --sku F1 -n <app-name> -l <location-name>
6970
```
7071

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:
7273

73-
```json
74+
```output
7475
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' ...
7677
Resource group creation complete
77-
Creating App service plan 'appsvc_asp_Linux_CentralUS' ...
78+
Creating App service plan 'appsvc_asp_Linux_centralus' ...
7879
App service plan creation complete
79-
Creating app '<app_name>' ....
80+
Creating app '<app-name>' ....
8081
Webapp creation complete
8182
Creating zip with contents of dir /home/username/quickstart/python-docs-hello-world ...
8283
Preparing to deploy contents to app.
8384
All done.
8485
{
85-
"app_url": "https:/<app_name>.azurewebsites.net",
86+
"app_url": "https:/<app-name>.azurewebsites.net",
8687
"location": "Central US",
87-
"name": "<app_name>",
88+
"name": "<app-name>",
8889
"os": "Linux",
89-
"resourcegroup": "appsvc_rg_Linux_CentralUS ",
90-
"serverfarm": "appsvc_asp_Linux_CentralUS",
90+
"resourcegroup": "appsvc_rg_Linux_centralus ",
91+
"serverfarm": "appsvc_asp_Linux_centralus",
9192
"sku": "BASIC",
9293
"src_path": "/home/username/quickstart/python-docs-hello-world ",
9394
"version_detected": "-",
9495
"version_to_create": "python|3.7"
9596
}
9697
```
9798

98-
The `az webapp up` command does the following actions:
99+
[!INCLUDE [AZ Webapp Up Note](../../../includes/app-service-web-az-webapp-up-note.md)]
99100

100-
- Create a default resource group.
101+
## Browse to the app
101102

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`.
103104

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.
105106

106-
- [Zip deploy](https://docs.microsoft.com/azure/app-service/deploy-zip) files from the current working directory to the app.
107+
![Run a sample Python app in Azure](./media/quickstart-python/run-hello-world-sample-python-app-in-browser.png)
107108

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.
109114

110-
Browse to the deployed application using your web browser.
115+
# [Bash](#tab/bash)
111116

112117
```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
114123
```
115124

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+
```
117144

118-
![Sample app running in Azure](media/quickstart-python/hello-world-in-browser.png)
145+
---
119146

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!**.
121148

122-
## Update and redeploy the code
149+
![Run a sample Python app locally](./media/quickstart-python/run-hello-world-sample-python-app-in-browser.png)
123150

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.
125152

126-
![Code application.py](media/quickstart-python/code-applicationpy.png)
153+
## Redeploy updates
127154

128-
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.
129156

130157
```python
158+
print("Handling request to home page.")
131159
return "Hello Azure!"
132160
```
133161

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.
135163

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.
137165

138-
```bash
139-
az webapp up -n <app_name>
166+
```terminal
167+
az webapp up --sku F1 -n <app-name> -l <location-name>
140168
```
141169

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+
![Run an updated sample Python app in Azure](./media/quickstart-python/run-updated-hello-world-sample-python-app-in-browser.png)
143173

144-
![Updated sample app running in Azure](media/quickstart-python/hello-azure-in-browser.png)
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).
145176
146-
## Manage your new Azure app
177+
## Stream logs
147178

148-
Go to the <a href="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+
```
149186

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.
197+
2019-10-23T12:40:03.815602424Z 172.16.0.1 - - [23/Oct/2019:12:40:03 +0000] "GET / HTTP/1.1" 200 12 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.63 Safari/537.36 Edg/78.0.276.19"
198+
```
151199

152-
![Portal navigation to Azure app](./media/quickstart-python/app-service-list.png)
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 <a href="https://portal.azure.com" target="_blank">Azure portal</a> to manage the app you created. Search for and select **App Services**.
207+
208+
![Navigate to App Services in the Azure portal](./media/quickstart-python/navigate-to-app-services-in-the-azure-portal.png)
209+
210+
Select the name of your Azure app.
211+
212+
![Navigate to your Python app in App Services in the Azure portal](./media/quickstart-python/navigate-to-app-in-app-services-in-the-azure-portal.png)
153213

154214
You see your app's Overview page. Here, you can perform basic management tasks like browse, stop, start, restart, and delete.
155215

156-
![App Service page in Azure portal](media/quickstart-python/app-service-detail.png)
216+
![Manage your Python app in the Overview page in the Azure portal](./media/quickstart-python/manage-an-app-in-app-services-in-the-azure-portal.png)
217+
218+
The App Service menu provides different pages for configuring your app.
219+
220+
## Clean up resources
157221

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.
159223

160-
[!INCLUDE [cli-samples-clean-up](../../../includes/cli-samples-clean-up.md)]
224+
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+
```
161229

162230
## Next steps
163231

164232
> [!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)
166234
167235
> [!div class="nextstepaction"]
168236
> [Configure Python app](how-to-configure-python.md)
169237
170238
> [!div class="nextstepaction"]
171-
> [Tutorial: Deploy from private container repository](tutorial-custom-docker-image.md)
239+
> [Tutorial: Run Python app in custom container](tutorial-custom-docker-image.md)

articles/app-service/containers/quickstart-python.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ ms.date: 10/22/2019
1616
ms.author: cephalin
1717
ms.custom: seo-python-october2019
1818

19-
experimental: false
20-
experiment_id: 1e304dc9-5add-4b
19+
experimental: true
20+
experiment_id: 01a9132f-eaab-4c
2121
---
2222
# Quickstart: Create a Python app in Azure App Service on Linux
2323

0 commit comments

Comments
 (0)