Create a Python application on Web App

This quickstart tutorial walks through how to develop and deploy a Python app to Azure. We’ll run the app using Azure App Service, and create and configure a new web app within it using the Azure CLI. We’ll then use git to deploy our Python app to Azure.

hello-world-in-browser

You can follow the steps below using a Mac, Windows, or Linux machine. It should take you only about 5 minutes to complete all of the steps below.

Prerequisites

Before running this sample, install the following prerequisites locally:

  1. Download and install git
  2. Download and install Python
  3. Download and install the Azure CLI 2.0

Download the sample

Clone the Hello World sample app repository to your local machine.

git clone https://github.com/Azure-Samples/python-docs-hello-world
Tip

Alternatively, you can download the sample as a zip file and extract it.

Change to the directory that contains the sample code.

cd Python-docs-hello-world

Run the app locally

Run the application locally by opening a terminal window an using Python command line for the sample to launch the built in Python web server.

python main.py

Open a web browser, and navigate to the sample.

http://localhost:5000

You can see the Hello World message from the sample app displayed in the page.

localhost-hello-world-in-browser

In your terminal window, press Ctrl+C to exit the web server.

Log in to Azure

We are now going to use the Azure CLI 2.0 in a terminal window to create the resources needed to host our Python app in Azure. Log in to your Azure subscription with the az login command and follow the on-screen directions.

az login

Configure a Deployment User

For FTP and local Git it is necessary to have a deployment user configured on the server to authenicate your deployment. Creating a deployment user is a one time configuration, take a note of the username and password as they will be used in a step below.

Note

A deployment user is required for FTP and Local Git deployment to a web app. The username and password are account-level, and as such are different from your Azure Subscription credentials. These credentials are only required to be created once.

Use the az appservice web deployment user set command to create your account-level credentials.

az appservice web deployment user set --user-name <username> --password <password>

Create a resource group

Create a resource group with the az group create. An Azure resource group is a logical container into which Azure resources like web apps, databases and storage accounts are deployed and managed.

az group create --name myResourceGroup --location westeurope

Create an Azure App Service

Create an App Service plan with the az appservice plan create command.

An App Service plan specifies the location, size, and features of the web server farm that hosts your app. You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

App Service plans define:

  • Region (for example: North Europe, East US, or Southeast Asia)
  • Instance Size (Small, Medium, or Large)
  • Scale Count (1 to 20 instances)
  • SKU (Free, Shared, Basic, Standard, or Premium)

The following example creates an App Service Plan named quickStartPlan using the FREE pricing tier.

az appservice plan create --name quickStartPlan --resource-group myResourceGroup --sku FREE

When the App Service Plan has been created, the Azure CLI shows information similar to the following example.

{
"appServicePlanName": "quickStartPlan",
"geoRegion": "North Europe",
"id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/quickStartPlan",
"kind": "app",
"location": "North Europe",
"maximumNumberOfWorkers": 1,
"name": "quickStartPlan",
"provisioningState": "Succeeded",
"resourceGroup": "myResourceGroup",
"sku": {
  "capacity": 0,
  "family": "F",
  "name": "F1",
  "size": "F1",
  "tier": "Free"
},
"status": "Ready",
"type": "Microsoft.Web/serverfarms",
}

Create a web app

Now that an App Service plan has been created, create a web app within the quickStartPlan App Service plan. The web app gives us a hosting space to deploy our code as well as provides a URL for us to view the deployed application. Use the az appservice web create command to create the web app.

In the command below please substitute your own unique app name where you see the <app_name> placeholder. The <app_name> will be used as the default DNS site for the web app, and so the name needs to be unique across all apps in Azure. You can later map any custom DNS entry to the web app before you expose it to your users.

az appservice web create --name <app_name> --resource-group myResourceGroup --plan quickStartPlan

When the web app has been created, the Azure CLI shows information similar to the following example.

{
  "clientAffinityEnabled": true,
  "defaultHostName": "<app_name>.azurewebsites.net",
  "enabled": true,
  "enabledHostNames": [
    "<app_name>.azurewebsites.net",
    "<app_name>.scm.azurewebsites.net"
  ],
  "hostNames": [
    "<app_name>.azurewebsites.net"
  ],
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/<app_name>",
  "kind": "app",
  "location": "North Europe",
  "outboundIpAddresses": "13.69.190.80,13.69.191.239,13.69.186.193,13.69.187.34",
  "resourceGroup": "myResourceGroup",
  "serverFarmId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/quickStartPlan",
  "state": "Running",
  "type": "Microsoft.Web/sites",
}

Browse to the site to see your newly created web app.

http://<app_name>.azurewebsites.net

app-service-web-service-created

We’ve now created an empty new web app in Azure. Let’s now configure our web app to use Python and deploy our app to it.

Configure to use Python

Use the az appservice web config update command to configure the web app to use Python version 3.4.

Tip

Setting the Python version this way uses a default container provided by the platform, if you would like to use your own container refer to the CLI reference for the az appservice web config container update command.

az appservice web config update --python-version 3.4 --name <app-name> --resource-group myResourceGroup

Configure local git deployment

You can deploy to your web app in a variety of ways including FTP, local Git as well as GitHub, Visual Studio Team Services and Bitbucket.

Use the az appservice web source-control config-local-git command to configure local git access to the web app.

az appservice web source-control config-local-git --name <app_name> --resource-group myResourceGroup --query url --output tsv

Copy the output from the terminal as it will be used in the next step.

https://<username>@<app_name>.scm.azurewebsites.net:443/<app_name>.git

Push to Azure from Git

Add an Azure remote to your local Git repository.

git remote add azure <paste-previous-command-output-here>

Push to the Azure remote to deploy your application. You will be prompted for the password you supplied earlier as part of the creation of the deployment user.

git push azure master

During deployment, Azure App Service will communicate it's progress with Git.

Counting objects: 18, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (18/18), 4.31 KiB | 0 bytes/s, done.
Total 18 (delta 4), reused 0 (delta 0)
remote: Updating branch 'master'.
remote: Updating submodules.
remote: Preparing deployment for commit id '44e74fe7dd'.
remote: Generating deployment script.
remote: Generating deployment script for python Web Site
remote: Generated deployment script files
remote: Running deployment command...
remote: Handling python deployment.
remote: KuduSync.NET from: 'D:\home\site\repository' to: 'D:\home\site\wwwroot'
remote: Deleting file: 'hostingstart.html'
remote: Copying file: '.gitignore'
remote: Copying file: 'LICENSE'
remote: Copying file: 'main.py'
remote: Copying file: 'README.md'
remote: Copying file: 'requirements.txt'
remote: Copying file: 'virtualenv_proxy.py'
remote: Copying file: 'web.2.7.config'
remote: Copying file: 'web.3.4.config'
remote: Detected requirements.txt.  You can skip Python specific steps with a .skipPythonDeployment file.
remote: Detecting Python runtime from site configuration
remote: Detected python-3.4
remote: Creating python-3.4 virtual environment.
remote: .................................
remote: Pip install requirements.
remote: Successfully installed Flask click itsdangerous Jinja2 Werkzeug MarkupSafe
remote: Cleaning up...
remote: .
remote: Overwriting web.config with web.3.4.config
remote:         1 file(s) copied.
remote: Finished successfully.
remote: Running post deployment command(s)...
remote: Deployment successful.
To https://<app_name>.scm.azurewebsites.net/<app_name>.git
 * [new branch]      master -> master

Browse to the app

Browse to the deployed application using your web browser.

http://<app_name>.azurewebsites.net

This time, the page that displays the Hello World message is running using our Python code running as an Azure App Service web app.

hello-world-in-browser

Updating and Deploying the Code

Using a local text editor, open the main.py file within the Python app, and make a small change to the text within the string next to return statement:

return 'Hello, Azure!'

Commit your changes in git, then push the code changes to Azure.

git commit -am "updated output"
git push azure master

Once deployment has completed, switch back to the browser window that opened in the Browse to the app step, and hit refresh.

hello-azure-in-browser

Manage your new Azure web app

Go to the Azure portal to take a look at the web app you just created.

To do this, sign in to https://portal.azure.com.

From the left menu, click App Services, then click the name of your Azure web app.

Portal navigation to Azure web app

You have landed in your web app's blade (a portal page that opens horizontally).

By default, your web app's blade shows the Overview page. This page gives you a view of how your app is doing. Here, you can also perform basic management tasks like browse, stop, start, restart, and delete. The tabs on the left side of the blade shows the different configuration pages you can open.

App Service blade in Azure portal

These tabs in the blade show the many great features you can add to your web app. The following list gives you just a few of the possibilities:

  • Map a custom DNS name
  • Bind a custom SSL certificate
  • Configure continuous deployment
  • Scale up and out
  • Add user authentication

Congratulations! You've deployed your first Python app to App Service.

Clean up resources

To clean up your resources, run the following command:

az group delete --name myResourceGroup

Next steps