Azure App service hosting Python Flask / Dash app
Create an App Service in Azure
Search for App Services and create
As usual, set resource group, app name, code environment (e.g. python 3.9), operation sys (e.g. linux) and region
Select an app service plan as well. Choose the size accordingly.
Then done.
Flask app
For Flask, App Service looks for a file named application.py or app.py and starts Gunicorn as follows:
# If application.py
gunicorn --bind=0.0.0.0 --timeout 600 application:app
# If app.py
gunicorn --bind=0.0.0.0 --timeout 600 app:app
The default port number from flask e.g. 5000 is reset by gunicorn. If not defined, it defaults to 80
If your main app module is contained in a different file, use a different name for the app object, or you want to provide additional arguments to Gunicorn, use a custom startup command.
App deployment
There are a few ways to deploy the app, through VS Code, or Azure CLI, or Git or even uploading zip file
If the app folder has contained the python virtual environment and the required packages etc. then just deploy the whole thing.
If only deploying the script files (e.g. app.py) and the requirement.txt, then it needs to enable the build process on the app service to install packages etc.
On the portal,
go to configuration and under application setting, select New application setting
put in SCM_DO_BUILD_DURING_DEPLOYMENT and set it to true
App Service's build system, called Oryx, calls "Run pip install -r requirements.txt" to install the packages required.
Deployment Center
on the portal, go to deployment center to select a source, e.g. git or azure devops
Clicking the 'sync' button on the page will start a deployment
To deploy through zip file, zip the app.py and requirement.txt. Go to FTPS credentials to get the upload username and password. Use curl command to upload. Check Azure website for the command details. It seems not working for me.
to configure an app (python), https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#flask-app
It is also available for java, node.js, asp.net, etc.
A flask app example:
app.py
from flask import Flask, Response
app = Flask(__name__)
@app.route('/')
def index():
return Response('helloworld'), 200
if __name__ == '__main__':
app.run()
requirements.txt
Flask==2.0.2
A dash app example, which is also based on flask.
Note the app instance is actually the Dash instance's server property.
So it must have the line "app = dash_app.server" otherwise Azure App services won't be able to spin up the service.
app.py
from dash import Dash, html
dash_app = Dash(__name__)
app = dash_app.server
dash_app.layout = html.Div([html.H6("Helloworld!")])
if __name__ == '__main__':
dash_app.run_server()
requirements.txt
dash==2.6.2
Simply point the app service deployment center to the git folder that contains the app.py file and sync