Azure为功能应用程序使用python flask框架

时间:2019-03-14 03:39:00

标签: python azure flask azure-functions

我看到Azure现在在功能应用程序中支持Python(预览版)。我有一个现有的Flask应用程序,想知道是否可以在不进行重大更改的情况下将该功能部署为功能应用程序?

我已通读了在功能应用程序(https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python)中使用Python的Azure教程,但是没有使用flask框架...

有人有经验吗?

3 个答案:

答案 0 :(得分:3)

Flask现在可以与Python Azure Functions一起使用->参见https://github.com/Azure/azure-functions-python-library/pull/45

答案 1 :(得分:2)

我尝试了多种方法来将Azure Python功能与​​Flask框架集成在一起。最终,我通过TryFlask在名为app.test_client()的HttpTrigger函数中成功实现了这一目标。

这是我的示例代码,如下所示。

import logging
import azure.functions as func
from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/hi')
def hi():
    return 'Hi World!'

@app.route('/hello')
@app.route('/hello/<name>', methods=['POST', 'GET'])
def hello(name=None):
    return name != None and 'Hello, '+name or 'Hello, '+request.args.get('name')

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    uri=req.params['uri']
    with app.test_client() as c:
        doAction = {
            "GET": c.get(uri).data,
            "POST": c.post(uri).data
        }
        resp = doAction.get(req.method).decode()
        return func.HttpResponse(resp, mimetype='text/html')

要在本地和Azure上进行测试,请通过带有查询字符串/,{{1的url /hello访问URL http(s)://<localhost:7071 or azurefunchost>/api/TryFlask,'/ hi'和?uri=/ }}和?uri=/hi在浏览器中,并使用查询字符串?uri=/hello/peter-pan对上面的相同url执行POST方法,所有这些都可以使用。请在下面的本地图中查看结果,在云上也是如此。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

注意:在我的解决方案中,网址必须为?uri=/hello/peter-pan

答案 2 :(得分:2)

Flask应用程序只是WSGI应用程序。 WSGI是一个相当简单的界面(请参见http://ivory.idyll.org/articles/wsgi-intro/what-is-wsgi.html。因此,与其使用test_client()作为连接到Azure函数环境的中间件,不应该使用适当的wsgi包装器实现,该实现调用 app = Flask( )对象。

https://github.com/vtbassmatt/azf-wsgi中提供了一个不错的Azure Python wsgi包装器实现“ azf-wsgi”。

为了在Flask中使用azf-wsgi包装器,我发现使用中间件将URL:s从/ api / app重写为/很有用,因此在开发时,我不需要知道Flask的位置应用被安装。 另一个好处是,我的main.py只是普通的Flask应用程序,无需使用Azure函数环境就可以在本地运行(速度更快)。

我的Azure函数HttpTriggerApp / __ init__.py已附加。 myFlaskApp文件夹位于HttpTriggerApp下。记住要在http-trigger和main.py( from。import myHelperFooBar )中使用rlative导入。

对于host.json和function.json,请遵循azf-wsgi说明。

import logging
import azure.functions as func

# note that the package is "azf-wsgi" but the import is "azf_wsgi"
from azf_wsgi import AzureFunctionsWsgi

# Import the Flask wsgi app (note relative import from the folder under the httpTrigger-folder.
from .myFlaskAppFolder.main import app

# rewrite URL:s to Azure function mount point (you can configure this in host.json and function.json)
from werkzeug.middleware.dispatcher import DispatcherMiddleware
app.config["APPLICATION_ROOT"] = "/api/app"     # Flask app configuration so it knows correct endpoint urls
application = DispatcherMiddleware(None, {
    '/api/app': app,
})

# Wrap the Flask app as WSGI application
def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
    return AzureFunctionsWsgi(application).main(req, context)