若要从 Python 中的控制台应用程序调用 Fabric 用户数据函数项(预览版),可以将 HTTP 请求发送到需要执行的函数终结点。 本快速入门介绍如何使用 Visual Studio Code 设置 Python 应用。
先决条件
- 安装 Visual Studio Code。
- 在本地计算机上下载 Python 3.1。
- 如果没有,可免费创建 Microsoft Fabric 帐户。
- 创建工作区。
- 创建用户数据函数项并将其发布。 请查看以下文章,了解如何在 VS Code 中创建或如何在门户中创建。 发布更改,以便可以调用用户数据函数项。
创建控制台应用程序以调用函数
函数必须可公开访问。 在 Functions 资源管理器中,将鼠标悬停在函数的名称上,然后选择显示的省略号图标 (...),然后选择“属性”。 在打开的“属性”窗格中,启用“公共访问”。 还应记下要在 Python 应用程序中使用的公共 URL。
为 Python 应用创建新文件夹,例如 my-data-app。 在 VS Code 中打开 文件夹。
在 VS Code 中设置 Python 虚拟环境。 若要在 VS Code 中创建本地环境,请使用 Ctrl+Shift+P 打开命令面板,然后搜索并选择“Python: Create Environment”命令。
- 该命令提供环境类型列表,并选择 Venv。
- 选择 Python 解释器版本 Python 3.11。
运行以下命令,在 VS Code 终端中激活虚拟环境。
venv\Scripts\activate.bat
接下来,运行命令以安装此示例所需的 Python 库。
pip install azure-identity, requests
创建
app.py
文件,并使用代码调用用户数据函数项。from azure.identity import InteractiveBrowserCredential import requests import json # Acquire a token # DO NOT USE IN PRODUCTION. # Below code to acquire token is to test the GraphQL endpoint and is for the purpose of development only. # For production, always register an application in a Microsoft Entra ID tenant and use the appropriate client_id and scopes. # https://learn.microsoft.com/fabric/data-engineering/connect-apps-api-graphql#create-a-microsoft-entra-app app = InteractiveBrowserCredential() scp = 'https://analysis.windows.net/powerbi/api/user_impersonation' result = app.get_token(scp) if not result.token: print('Error:', "Could not get access token") # Prepare headers headers = { 'Authorization': f'Bearer {result.token}', 'Content-Type': 'application/json' } FUNCTION_URL = '<REPLACE WITH USER DATA FUNCTION URL>' # Prepare the request data data = '{"name": "John"}' # JSON payload to send to the Azure Function headers = { # "Authorization": f"Bearer {access_token}", "Content-Type": "application/json" } try: # Call the user data function public URL response = requests.post(FUNCTION_URL, json=data, headers=headers) response.raise_for_status() print(json.dumps(response.json())) except Exception as e: print({"error": str(e)}, 500) if __name__ == "__main__": app.run(debug=True)
注释
此示例仅用于开发。 在将应用程序用于生产用例之前,更新应用程序以使用 Microsoft Entra ID 身份验证。
从外部应用程序调用函数
可以通过向终结点 URL 发出 REST 调用来调用函数。 选择要在 Functions 资源管理器中调用的函数,然后选择“复制函数 URL”。 还可以打开或关闭从“属性”菜单外部使用此 URL 的功能。
然后,在应用程序中使用此 URL 调用函数。 请参阅从应用程序调用用户数据函数
输出架构
从外部应用程序调用用户数据函数时,输出架构的格式如下:
{
"functionName": "hello_fabric",
"invocationId": "1234567890",
"status": "Succeeded | BadRequest | Failed | Timeout | ResponseTooLarge",
"output": /*shows the result of the function dependeing on the output type*/,
"errors": [
{
"name": "Error name",
"message": "Error message",
"properties": {
/*Key value pairs custom to error*/
}
},
]
}
将返回以下属性:
- functionName:已执行的函数的名称。
- invocationId:用于执行函数的调用 ID。
-
status:函数的执行结果。 可以是以下任一值:
Succeeded
、BadRequest
、Failed
、Timeout
和ResponseTooLarge
。 - output:函数返回的输出值。
- errors:如果捕获到任何错误,则会返回所有错误的列表,其中包含每个错误的名称、错误消息和错误属性。
响应代码
由于执行,该函数将返回以下 HTTP 代码。
响应代码 | 说明 |
---|---|
200 正常(成功) | 请求已成功 |
403(已禁止) | 响应太大,调用失败。 |
408(请求超时) | 由于执行时间超过 200 秒,请求失败。 |
409(冲突) | 请求在执行期间引发异常。 |
400(错误请求) | 请求由于输入参数无效或缺失而失败。 |
500(内部服务器错误) | 请求由于内部错误而失败。 |
调试和测试
使用 python 调试器在 VS Code 中调试应用程序。 如果需要,添加断点进行调试(如有任何问题)。 了解详细信息