Skip to content

Latest commit

 

History

History
413 lines (256 loc) · 25.7 KB

quickstart-python.md

File metadata and controls

413 lines (256 loc) · 25.7 KB
title description ms.topic ms.date ms.author author ms.devlang ms.custom
Quickstart: Deploy a Python (Django, Flask, or FastAPI) web app to Azure
Get started with Azure App Service by deploying your first Python app to Azure App Service.
quickstart
06/28/2024
msangapu
msangapu-msft
python
devx-azure-cli, devx-azure-portal, devx-vscode-azure-extension, devdivchpfy22, vscode-azure-extension-update-completed, devx-track-azurecli, devx-track-python

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

[!INCLUDE regionalization-note]

In this quickstart, you deploy a Python web app (Django, Flask, or FastAPI) 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:

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, Django, or FastAPI. 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.

git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart
git clone https://github.com/Azure-Samples/msdocs-python-django-webapp-quickstart
git clone https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart.git

To run the application locally:

  1. Go to the application folder:

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

    [!INCLUDE Virtual environment setup]

  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.

    :::image type="content" source="./media/quickstart-python/run-flask-app-localhost.png" alt-text="Screenshot of the Flask app running locally in a browser":::

Having issues? Let us know.

  1. Go to the application folder:

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

    [!INCLUDE Virtual environment setup]

  3. Install the dependencies:

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

    python manage.py runserver
  5. Browse to the sample application at http://localhost:8000 in a web browser.

    :::image type="content" source="./media/quickstart-python/run-django-app-localhost.png" alt-text="Screenshot of the Django app running locally in a browser":::

Having issues? Let us know.

  1. Go to the application folder:

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

    [!INCLUDE Virtual environment setup]

  3. Install the dependencies:

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

    uvicorn main:app --reload
  5. Browse to the sample application at http://localhost:8000 in a web browser.

    :::image type="content" source="./media/quickstart-python/run-django-app-localhost.png" alt-text="Screenshot of the FastAPI app running locally in a browser.":::

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.

[!INCLUDE Create CLI]

To create Azure resources in VS Code, you must have the Azure Tools extension pack installed and be signed into Azure from VS Code.

[!div class="nextstepaction"] Download Azure Tools extension pack

In the application folder, open VS Code:

code .
Instructions Screenshot
[!INCLUDE Create app service step 1] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-1-240-px.png" alt-text="A Screenshot of the Azure Tools icon in the left toolbar of VS Code." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-1.png":::
[!INCLUDE Create app service step 2] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-2-240-px.png" alt-text="A screenshot of the App Service section of Azure Tools extension and the context menu used to create a new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-2.png":::
[!INCLUDE Create app service step 3] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-4-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to select Create a new Web App." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-4.png":::
[!INCLUDE Create app service step 4] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-5-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to enter the globally unique name for the new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-5.png":::
[!INCLUDE Create app service step 5] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-6-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to select the runtime stack for the new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-6.png":::
[!INCLUDE Create app service step 6] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-7-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to a pricing tier for the new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-7.png":::
[!INCLUDE Create app service step 7] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-7b-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to start deploy to new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-7b.png":::
[!INCLUDE Create app service step 8] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-7c-240-px.png" alt-text="A screenshot of the dialog box in VS Code used to select the folder to deploy as the new web app." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-7c.png":::
[!INCLUDE Create app service step 9] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-8-240-px.png" alt-text="A screenshot of a dialog box in VS Code asking if you want to update your workspace to run build commands." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-8.png":::
[!INCLUDE Create app service step 10] :::image type="content" source="./media/quickstart-python/create-app-service-visual-studio-code-9-240-px.png" alt-text="A screenshot showing the confirmation dialog when the app code has been deployed to Azure." lightbox="./media/quickstart-python/create-app-service-visual-studio-code-9.png":::

Sign in to the Azure portal and follow these steps to create your Azure App Service resources.

Instructions Screenshot
[!INCLUDE Create app service step 1] :::image type="content" source="./media/quickstart-python/create-app-service-azure-portal-1-240px.png" alt-text="A screenshot of how to use the search box in the top tool bar to find App Services in Azure." lightbox="./media/quickstart-python/create-app-service-azure-portal-1.png":::
[!INCLUDE Create app service step 2] :::image type="content" source="./media/quickstart-python/create-app-service-azure-portal-2-240px.png" alt-text="A screenshot of the location of the Create button on the App Services page in the Azure portal." lightbox="./media/quickstart-python/create-app-service-azure-portal-2.png":::
[!INCLUDE Create app service step 3] :::image type="content" source="./media/quickstart-python/create-app-service-azure-portal-3-240px.png" alt-text="A screenshot of how to fill out the form to create a new App Service in the Azure portal." lightbox="./media/quickstart-python/create-app-service-azure-portal-3.png":::
[!INCLUDE Create app service step 4] :::image type="content" source="./media/quickstart-python/create-app-service-azure-portal-4-240px.png" alt-text="A screenshot of how to select the basic app service plan in the Azure portal." lightbox="./media/quickstart-python/create-app-service-azure-portal-4.png":::
[!INCLUDE Create app service step 5] :::image type="content" source="./media/quickstart-python/create-app-service-azure-portal-5-240px.png" alt-text="A screenshot of the location of the Review plus Create button in the Azure portal." lightbox="./media/quickstart-python/create-app-service-azure-portal-5.png":::

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.

[!INCLUDE Deploy Azure CLI]

[!INCLUDE Deploy VS Code]

[!INCLUDE Deploy Local Git]

[!INCLUDE Deploy using ZIP file]


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

4 - Configure startup script

Based on the presence of certain files in a deployment, App Service automatically detects whether an app is a Django or Flask app and performs default steps to run your app. For apps based on other web frameworks like FastAPI, you need to configure a startup script for App Service to run your app; otherwise, App Service runs a default read-only app located in the opt/defaultsite folder.

To learn more about how App Service runs Python apps and how you can configure and customize its behavior with your app, see Configure a Linux Python app for Azure App Service.

App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.

App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.

For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app.

First, configure the startup command using the az webapp config set command.

az webapp config set \
    --startup-file "gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app" \
    --name $APP_SERVICE_NAME \
    --resource-group $RESOURCE_GROUP_NAME

Next, restart the web app using the az webapp restart command.

az webapp restart \
    --name $APP_SERVICE_NAME \
    --resource-group $RESOURCE_GROUP_NAME

App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.

App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.

Use Azure CLI or the Azure portal to configure the startup command.

App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.

App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.

For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app.

Instructions Screenshot
[!INCLUDE Configure startup command from Azure portal 1] :::image type="content" source="./media/quickstart-python/configure-startup-azure-portal-1-240px.png" alt-text="A screenshot of the location in the Azure portal where to configure the startup command." lightbox="./media/quickstart-python/configure-startup-azure-portal-1.png":::
[!INCLUDE Configure startup command from Azure portal 2] :::image type="content" source="./media/quickstart-python/configure-startup-azure-portal-2-240px.png" alt-text="A screenshot of how to reset the web app in the Azure portal." lightbox="./media/quickstart-python/configure-startup-azure-portal-2.png":::

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

:::image type="content" source="./media/quickstart-python/run-app-azure.png" alt-text="Screenshot of the app running in Azure":::

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

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

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

:::code language="python" source="~/msdocs-python-flask-webapp-quickstart/app.py" range="9-29" highlight="3,16,19":::

:::code language="python" source="~/msdocs-python-django-webapp-quickstart/hello_azure/views.py" range="5-21" highlight="2,11,14":::

:::code language="python" source="~/msdocs-python-fastapi-webapp-quickstart/main.py" range="12-30" highlight="3,15,18":::


The contents of the App Service diagnostic logs can be reviewed using the Azure CLI, VS Code, or 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.

[!INCLUDE CLI stream logs configure]

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

[!INCLUDE CLI stream logs tail]

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 received
2021-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=David
2021-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"
Instructions Screenshot
[!INCLUDE Stream logs from VS Code 1] :::image type="content" source="./media/quickstart-python/stream-logs-vs-code-1-240px.png" alt-text="A screenshot of how to start streaming logs with the VS Code extension." lightbox="./media/quickstart-python/stream-logs-vs-code-1.png":::
[!INCLUDE Stream logs from VS Code 2] :::image type="content" source="./media/quickstart-python/stream-logs-vs-code-2-240px.png" alt-text="A screenshot of an example of streaming logs in the VS Code Output window." lightbox="./media/quickstart-python/stream-logs-vs-code-2.png":::
Instructions Screenshot
[!INCLUDE Stream logs from Azure portal 1] :::image type="content" source="./media/quickstart-python/stream-logs-azure-portal-1-240px.png" alt-text="A screenshot of the location in the Azure portal where to enable streaming logs." lightbox="./media/quickstart-python/stream-logs-azure-portal-1.png":::
[!INCLUDE Stream logs from Azure portal 2] :::image type="content" source="./media/quickstart-python/stream-logs-azure-portal-2-240px.png" alt-text="A screenshot of how to view logs in the Azure portal." lightbox="./media/quickstart-python/stream-logs-azure-portal-2.png":::

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.

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.

Instructions Screenshot
[!INCLUDE Remove resource group VS Code 1] :::image type="content" source="./media/quickstart-python/remove-resource-group-visual-studio-code-1-240px.png" alt-text="A screenshot of how to delete a resource group in VS Code using the Azure Tools extension." lightbox="./media/quickstart-python/remove-resource-group-visual-studio-code-1.png":::
[!INCLUDE Remove resource group VS Code 2] :::image type="content" source="./media/quickstart-python/remove-resource-group-visual-studio-code-2-240px.png" alt-text="A screenshot of the confirmation dialog for deleting a resource group from VS Code." lightbox="./media/quickstart-python/remove-resource-group-visual-studio-code-2.png":::

Follow these steps while signed-in to the Azure portal to delete a resource group.

Instructions Screenshot
[!INCLUDE Remove resource group Azure portal 1] :::image type="content" source="./media/quickstart-python/remove-resource-group-azure-portal-1-240px.png" alt-text="A screenshot of how to search for and navigate to a resource group in the Azure portal." lightbox="./media/quickstart-python/remove-resource-group-azure-portal-1.png":::
[!INCLUDE Remove resource group Azure portal 2] :::image type="content" source="./media/quickstart-python/remove-resource-group-azure-portal-2-240px.png" alt-text="A screenshot of the location of the Delete Resource Group button in the Azure portal." lightbox="./media/quickstart-python/remove-resource-group-azure-portal-2.png":::
[!INCLUDE Remove resource group Azure portal 3] :::image type="content" source="./media/quickstart-python/remove-resource-group-azure-portal-3-240px.png" alt-text="A screenshot of the confirmation dialog for deleting a resource group in the Azure portal." lightbox="./media/quickstart-python/remove-resource-group-azure-portal-3.png":::

Having issues? Let us know.

Next steps

[!div class="nextstepaction"] Tutorial: Python (Django or Flask) web app with PostgreSQL

[!div class="nextstepaction"] Configure Python app

[!div class="nextstepaction"] Add user sign-in to a Python web app

[!div class="nextstepaction"] Tutorial: Run Python app in custom container

[!div class="nextstepaction"]  Secure with custom domain and certificate