Quickstart: Deploy a Python (Django or Flask) web app to Azure - Azure App Service (2024)

  • Article

In this quickstart, you'll deploy a Python web app (Django or Flask) to Azure App Service. Azure App Service is a fully managed web hosting service that supports Python apps hosted in a Linux server environment.

To complete this quickstart, you need:

  • An Azure account with an active subscription. Create an account for free.
  • Python 3.9 or higher installed locally.

Note

This article contains current instructions on deploying a Python web app using Azure App Service. Python on Windows is no longer supported.

1 - Sample application

This quickstart can be completed using either Flask or Django. A sample application in each framework is provided to help you follow along with this quickstart. Download or clone the sample application to your local workstation.

  • Flask
  • Django
git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart

To run the application locally:

  • Flask
  • Django
  1. Go to the application folder:

    cd msdocs-python-flask-webapp-quickstart
  2. Create a virtual environment for the app:

    • Windows
    • macOS/Linux
    py -m venv .venv.venv\scripts\activate
  3. Install the dependencies:

    pip install -r requirements.txt
  4. Run the app:

    flask run
  5. Browse to the sample application at http://localhost:5000 in a web browser.

    Quickstart: Deploy a Python (Django or Flask) web app to Azure - Azure App Service (1)

Having issues? Let us know.

2 - Create a web app in Azure

To host your application in Azure, you need to create Azure App Service web app in Azure. You can create a web app using the Azure CLI, VS Code, Azure Tools extension pack, or Azure portal.

  • Azure CLI
  • VS Code
  • Azure portal

Azure CLI commands can be run on a computer with the Azure CLI installed.

Azure CLI has a command az webapp up that will create the necessary resources and deploy your application in a single step.

If necessary, login to Azure using az login.

az login

Create the webapp and other resources, then deploy your code to Azure using az webapp up.

az webapp up --runtime PYTHON:3.9 --sku B1 --logs
  • The --runtime parameter specifies what version of Python your app is running. This example uses Python 3.9. To list all available runtimes, use the command az webapp list-runtimes --os linux --output table.
  • The --sku parameter defines the size (CPU, memory) and cost of the app service plan. This example uses the B1 (Basic) service plan, which will incur a small cost in your Azure subscription. For a full list of App Service plans, view the App Service pricing page.
  • The --logs flag configures default logging required to enable viewing the log stream immediately after launching the webapp.
  • You can optionally specify a name with the argument --name <app-name>. If you don't provide one, then a name will be automatically generated.
  • 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 command.

The command may take a few minutes to complete. While the command is running, it provides messages about creating the resource group, the App Service plan, and the app resource, configuring logging, and doing 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.

The webapp '<app-name>' doesn't existCreating Resource group '<group-name>' ...Resource group creation completeCreating AppServicePlan '<app-service-plan-name>' ...Creating webapp '<app-name>' ...Configuring default logging for the app, if not already enabledCreating zip with contents of dir /home/cephas/myExpressApp ...Getting scm site credentials for zip deploymentStarting zip deployment. This operation can take a while to complete ...Deployment endpoint responded with status code 202You can launch the app at http://<app-name>.azurewebsites.net{ "URL": "http://<app-name>.azurewebsites.net", "appserviceplan": "<app-service-plan-name>", "location": "centralus", "name": "<app-name>", "os": "<os-type>", "resourcegroup": "<group-name>", "runtime_version": "python|3.9", "runtime_version_detected": "0.0", "sku": "FREE", "src_path": "<your-folder-location>"}

Note

The az webapp up command does the following actions:

  • Create a default resource group.

  • Create a default App Service plan.

  • Create an app with the specified name.

  • Zip deploy all files from the current working directory, with build automation enabled.

  • Cache the parameters locally in the .azure/config file so that you don't need to specify them again when deploying later with az webapp up or other az webapp commands from the project folder. The cached values are used automatically by default.

Having issues? Let us know.

3 - Deploy your application code to Azure

Azure App service supports multiple methods to deploy your application code to Azure including support for GitHub Actions and all major CI/CD tools. This article focuses on how to deploy your code from your local workstation to Azure.

  • Deploy using Azure CLI
  • Deploy using VS Code
  • Deploy using Local Git
  • Deploy using a ZIP file

Since the az webapp up command created the necessary resources and deployed your application in a single step, you can move on to 4 - Browse to the app.

Having issues? Refer first to the Troubleshooting guide, otherwise, let us know.

4 - Browse to the app

Browse to the deployed application in your web browser at the URL http://<app-name>.azurewebsites.net. If you see a default app page, wait a minute and refresh the browser.

The Python sample code is running a Linux container in App Service using a built-in image.

Quickstart: Deploy a Python (Django or Flask) web app to Azure - Azure App Service (2)

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

Having issues? Refer first to the Troubleshooting guide, otherwise, let us know.

5 - Stream logs

Azure App Service captures all messages output to the console to assist you in diagnosing issues with your application. The sample apps include print() statements to demonstrate this capability.

  • Flask
  • Django
app = Flask(__name__)@app.route('/')def index(): print('Request for index page received') return render_template('index.html')@app.route('/favicon.ico')def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')@app.route('/hello', methods=['POST'])def hello(): name = request.form.get('name')

The contents of the App Service diagnostic logs can be reviewed using the Azure CLI, VS Code, or Azure portal.

  • Azure CLI
  • VS Code
  • Azure portal

First, you need to configure Azure App Service to output logs to the App Service filesystem using the az webapp log config command.

  • bash
  • PowerShell terminal
az webapp log config \ --web-server-logging filesystem \ --name $APP_SERVICE_NAME \ --resource-group $RESOURCE_GROUP_NAME

To stream logs, use the az webapp log tail command.

  • bash
  • PowerShell terminal
az webapp log tail \ --name $APP_SERVICE_NAME \ --resource-group $RESOURCE_GROUP_NAME

Refresh the home page in the app or attempt other requests to generate some log messages. The output should look similar to the following.

Starting Live Log Stream ---2021-12-23T02:15:52.740703322Z Request for index page received2021-12-23T02:15:52.740740222Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET / HTTP/1.1" 200 1360 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:15:52.841043070Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 200 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:15:52.884541951Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET /static/images/azure-icon.svg HTTP/1.1" 200 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:15:53.043211176Z 169.254.130.1 - - [23/Dec/2021:02:15:53 +0000] "GET /favicon.ico HTTP/1.1" 404 232 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:16:01.304306845Z Request for hello page received with name=David2021-12-23T02:16:01.304335945Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "POST /hello HTTP/1.1" 200 695 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:16:01.398399251Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"2021-12-23T02:16:01.430740060Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "GET /static/images/azure-icon.svg HTTP/1.1" 304 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"

Having issues? Refer first to the Troubleshooting guide, otherwise, let us know.

Clean up resources

When you're finished with the sample app, you can remove all of the resources for the app from Azure. It will not incur extra charges and keep your Azure subscription uncluttered. Removing the resource group also removes all resources in the resource group and is the fastest way to remove all Azure resources for your app.

  • Azure CLI
  • VS Code
  • Azure portal

Delete the resource group by using the az group delete command.

az group delete \ --name msdocs-python-webapp-quickstart \ --no-wait

The --no-wait argument allows the command to return before the operation is complete.

Having issues? Let us know.

Next steps

Tutorial: Python (Django) web app with PostgreSQL

Configure Python app

Add user sign-in to a Python web app

Tutorial: Run Python app in custom container

Secure with custom domain and certificate

Quickstart: Deploy a Python (Django or Flask) web app to Azure - Azure App Service (2024)

FAQs

How to deploy Django to Azure App Service? ›

Using Azure to Deploy Django App
  1. Start by installing Psycopg, a PostgreSQL database adapter, first. ...
  2. Make changes to settings.py before deployment. ...
  3. Update your requirements. ...
  4. Create a file, . ...
  5. Create the . ...
  6. Once you are done modifying your codebase, go to the Azure portal and choose the app service you just built.
Oct 16, 2023

How to deploy Python application in Azure App Service? ›

Azure DevOps Services
  1. Create a web app in Azure App Service.
  2. Create a project in Azure DevOps.
  3. Connect your DevOps project to Azure.
  4. Create a Python-specific pipeline.
  5. Run the pipeline to build and deploy your app to your web app in App Service.
Apr 1, 2024

How do I deploy a web app in Azure app Service? ›

Example: deploy using Web Deploy
  1. Select the Tasks tab, then select Deploy Azure App Service. ...
  2. In the dialog, make sure that Connection type is set to Azure Resource Manager.
  3. In the dialog, expand Additional Deployment Options and select Select deployment method. ...
  4. Save the release pipeline.
Dec 13, 2023

Can I deploy Flask app on Azure? ›

In this tutorial, you'll deploy a data-driven Python web app (Django or Flask) to Azure App Service with the Azure Database for PostgreSQL relational database service. Azure App Service supports Python in a Linux server environment. To complete this tutorial, you'll need: An Azure account with an active subscription.

What is the best way to deploy Django app? ›

  1. Step 1: Create a Render account. ...
  2. Step 2: Create a new web service. ...
  3. Step 3: Configure the web service. ...
  4. Step 4: Add environment variables. ...
  5. Step 5: Configure the database. ...
  6. Step 6: Deploy the web service. ...
  7. Step 7: Access your Django application.
May 15, 2023

How to deploy Python flask web application? ›

To deploy the Python Flask app to Koyeb using the control panel (opens in a new tab), follow the steps below:
  1. Click Create Web Service on the Overview tab of the Koyeb control panel.
  2. Select GitHub as the deployment option.
  3. Name your App and Service, for example example-python .
  4. Click the Deploy button.

How to quickly deploy Python apps to Azure VMs? ›

Update your server
  1. Connect to your VM in your SSH session. Stop the server by typing Ctrl+C.
  2. Enter the following commands: Bash Copy. cd azure-stack-hub-flask-hello-world git pull.
  3. Activate the virtual environment and start the app: Bash Copy. export FLASK_APP=application.py flask run -h 0.0.0.0.
Feb 1, 2024

Does Azure App Service support Python? ›

You can deploy your Python project to Azure App Service as part of your continuous deployment (CD) workflows.

What is the way that you can deploy your code into Azure App Service? ›

You have four different options for deploying code to an Azure App Services account:
  • Visual Studio Web Deploy.
  • FTP.
  • Kudu.
  • CI/CD via Source Control.
Jan 20, 2022

What are the three deployment modes that can be used for Azure? ›

Several different cloud computing models, types, and services have evolved to meet the rapidly changing technology needs of organizations. There are three different ways to deploy cloud services: on a public cloud, private cloud, or hybrid cloud.

How do I deploy files to Azure App Service? ›

Deploy files to Azure

Copy your files and their respective directory structure to the /site/wwwroot directory in Azure (or the /site/wwwroot/App_Data/Jobs/ directory for WebJobs). Browse to your app's URL to verify the app is running properly.

How does Azure App Service simple web application development and deployment? ›

Azure App Service is a fully managed platform for creating and deploying cloud applications. It lets you define a set of compute resources for a web app to run, deploy web apps, and configure deployment slots. Deployment slots lets you stage a deployment and then swap it with the production deployment.

How do I deploy my Python application? ›

How do I deploy a Python script?
  1. Download the latest version of Python from the website. ...
  2. Go to the Prerequisites page in your Advanced Installer project.
  3. Select the Pre-install section so that your prerequisite will install during this step.
  4. Press “New Package Prerequisite” from the top-bar.

Is Flask better than Django? ›

Django is generally used for larger, more complex projects that can benefit from its “batteries included” approach and numerous built-in features. Flask is a good choice for simple applications or microservices. It's a minimalistic framework that offers developers the flexibility to add the functionality they need.

How to install Python packages in Azure App Service? ›

The output of our sample application.
  1. Step 0: Environment configuration. ...
  2. Step 1: Create the project folder. ...
  3. Step 2: Create a Virtual Environment and Install Dependencies. ...
  4. Step 3: Create requirements. ...
  5. Step 4: Create & run the app. ...
  6. Step 5: Enhance the application. ...
  7. Step 6: Create an app service instance in Azure.
Mar 22, 2023

Where do I deploy my Django app? ›

Therefore, for demanding projects built on Django, servers from Amazon, Microsoft, and Google are often the best Django hosts even with high prices.
  • Amazon Web Services (AWS)
  • Azure (Microsoft)
  • Google Cloud Platform.
  • Hetzner.
  • DigitalOcean.
  • Heroku.
Mar 26, 2024

Can you deploy Django app? ›

There are many options for deploying your Django application, based on your architecture or your particular business needs, but that discussion is outside the scope of what Django can give you as guidance. Django, being a web framework, needs a web server in order to operate.

How to deploy Django on cloud? ›

  1. On this page.
  2. Objectives.
  3. Costs.
  4. Before you begin.
  5. Prepare your environment. Clone a sample app. Confirm your Python setup. ...
  6. Create backing services. Set up a Cloud SQL for PostgreSQL instance. Set up a Cloud Storage bucket. ...
  7. Run the app on your local computer.
  8. Deploy the app to Cloud Run.

Top Articles
Latest Posts
Article information

Author: Dean Jakubowski Ret

Last Updated:

Views: 6353

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dean Jakubowski Ret

Birthday: 1996-05-10

Address: Apt. 425 4346 Santiago Islands, Shariside, AK 38830-1874

Phone: +96313309894162

Job: Legacy Sales Designer

Hobby: Baseball, Wood carving, Candle making, Jigsaw puzzles, Lacemaking, Parkour, Drawing

Introduction: My name is Dean Jakubowski Ret, I am a enthusiastic, friendly, homely, handsome, zealous, brainy, elegant person who loves writing and wants to share my knowledge and understanding with you.