Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

Using Flask Framework with Azure Functions

Azure Functions supports WSGI and ASGI-compatible frameworks with HTTP-triggered Python functions. This can be helpful if you are familiar with a particular framework, or if you have existing code you would like to reuse to create the Function app. The following is an example of creating an Azure Function app using Flask.

Prerequisites

Here are some of the prerequisites to get this sample to work for you.

Install Python

A Python version that is supported by Azure Functions is required. Run python --version (Linux/MacOS) or py --version (Windows) to check your Python version reports to a supported version. For more information on installing Python, see How to install Python.

Install Azure Functions Core Tools

Azure Functions Core Tools provides commands to create functions, connect to Azure, and deploy function projects. For more information, see Install Azure Functions Core Tools.

Create a new Azure Function App in VS Code

To create an Azure Function app in VSCode, please go through the Microsoft Docs tutorial on creating your first Azure Function using Visual Studio Code. This sample uses the Python v2 programming model, where functions are defined with decorators in a single function_app.py file and no function.json files are required. The Flask application lives in the FlaskApp package and is wired into the Functions host from function_app.py.

Setup

Clone or download this sample repository, and open the sample folder in Visual Studio Code or your IDE of choice.

Flask Framework in an Azure Function App

The file requirements.txt is updated to include the following depdendencies.

azure-functions
Flask

Note that azure-functions-worker should not be included in this file as the Python worker is manager by Azure Functions platform and manually managing it may cause unexpected issues. The v2 programming model requires azure-functions version 1.11.1 or later; leaving the dependency unpinned installs the latest version.

In the v2 programming model, the function_app.py file at the project root uses WsgiFunctionApp to redirect every invocation to the Flask (WSGI) handler. No function.json file is needed.

import azure.functions as func

# Import the Flask app defined in the FlaskApp package.
from FlaskApp import app as flask_app

# WsgiFunctionApp wires the WSGI-compatible Flask app directly into the
# Functions host. No function.json is required.
app = func.WsgiFunctionApp(
 app=flask_app.wsgi_app,
 http_auth_level=func.AuthLevel.FUNCTION,
)

The HTTP route and authorization level are configured directly on WsgiFunctionApp (via http_auth_level), so the previous function.json binding file has been removed. WsgiFunctionApp automatically handles all routes and forwards them to Flask.

The file host.json uses extension bundle v4 and sets an empty HTTP routePrefix so the Flask routes are served from the root path.

{
 "version": "2.0",
 "extensionBundle": {
 "id": "Microsoft.Azure.Functions.ExtensionBundle",
 "version": "[4.*, 5.0.0)"
 },
 "extensions": {
 "http": {
 "routePrefix": ""
 }
 }
}

Running the sample

Testing locally

To run Function Apps using Core Tools, see Run functions locally with Core Tools.

To test locally, run the below to install Flask.

pip install -r requirements.txt

Then, start debug mode and test the function using the HTTP endpoint exposed after the host and the worker load up the function.

Http Functions:
http_app_func: [GET,POST,DELETE,HEAD,PATCH,PUT,OPTIONS] http://localhost:7071/<route>

Testing in Azure

You can publish the function app directly from VSCode using the “Publish to Azure option” in the Azure extension. For more information, please refer the guide to publish the project to Azure using Visual Studio Code.

You can use one of these HTTP test tools to see the API in action locally, and on Azure:

Caution

For scenarios where you have sensitive data, such as credentials, secrets, access tokens, API keys, and other similar information, make sure to use a tool that protects your data with the necessary security features, works offline or locally, doesn't sync your data to the cloud, and doesn't require that you sign in to an online account. This way, you reduce the risk around exposing sensitive data to the public.

Running locally helps you to verify the credentials, configuration and business logic.

Calling the URL with Path Parameters

When running this sample, you can try a different URL route as well as parameterize it. For instance, http://<HOST>:7071/hello/Foo to call the Flask app with path param Foo. Another option is have the route as module to provide import guidance, which can be done through changing the url to http://<HOST>:7071/module.

When done locally, please try the following URL in your browser -

http://localhost:7071/hello/Foo

When done in Azure, please try the following URL in your browser -

http://<FunctionAppName>.azurewebsites.net/hello/Foo

Conclusion and Next Steps

This sample helps you setup an app with the Flask framework and can help you get started using web frameworks in Azure Functions.

To learn more about altering Python functions to leverage WSGI and ASGI-compatible frameworks, see Web frameworks.