Create a Python app in Azure App Service on Linux

make sure you have python3 and Azure CLI installed on Windows:

C:\Users\zhuby>python --version
Python 3.8.3
C:\Users\zhuby>az --version
azure-cli                         2.12.0

Then sign in to Azure through the CLI: az login
Clone the sample code, setup venv and deploy to local/Azure:

C:\Users\zhuby>git clone https://github.com/Azure-Samples/python-docs-hello-world
C:\Users\zhuby>cd python-docs-hello-world
C:\Users\zhuby\python-docs-hello-world>python -m venv venv
C:\Users\zhuby\python-docs-hello-world>venv\Scripts\activate.bat
(venv) C:\Users\zhuby\python-docs-hello-world>pip install -r requirements.txt
(venv) C:\Users\zhuby\python-docs-hello-world>flask run

(venv) C:\Users\zhuby\python-docs-hello-world>az webapp up --sku F1 -n python0508
The webapp 'python0508' doesn't exist
Creating Resource group 'zhuby1973_rg_Linux_centralus' ...
Resource group creation complete
Creating AppServicePlan 'zhuby1973_asp_Linux_centralus_0' ...
Creating webapp 'python0508' ...
Configuring default logging for the app, if not already enabled
Creating zip with contents of dir C:\Users\zhuby\python-docs-hello-world ...
Getting scm site credentials for zip deployment
Starting zip deployment. This operation can take a while to complete ...
Deployment endpoint responded with status code 202
You can launch the app at http://python0508.azurewebsites.net
{
  "URL": "http://python0508.azurewebsites.net",
  "appserviceplan": "zhuby1973_asp_Linux_centralus_0",
  "location": "centralus",
  "name": "python0508",
  "os": "Linux",
  "resourcegroup": "zhuby1973_rg_Linux_centralus",
  "runtime_version": "python|3.7",
  "runtime_version_detected": "-",
  "sku": "FREE",
  "src_path": "C:\\Users\\zhuby\\python-docs-hello-world"
}

you can redeploy updates, edit the app.py with below:

def hello():
    print("Handling request to home page.")
    return "Hello, Azure!"

Redeploy the app using the az webapp up command again! you will get update the webpage.
To stream logs, run the az webapp log tail command.

Leave a Reply

Your email address will not be published. Required fields are marked *