Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
To invoke Fabric User data function items from a console application in Python, you can send HTTP requests to the function endpoint that needs to be executed. In this quickstart, you learn how to set up a Python app using Visual Studio Code.
Prerequisites
- Install Visual Studio Code.
- Download Python 3.1 on your local machine.
- Create a Microsoft Fabric account for free if you don't have one.
- Create a workspace.
- Create a user data functions item and publish it. Check these articles on how to create one in Visual Studio Code or how to create one in the portal. Publish the changes so the user data functions item is ready to be invoked.
Create a Microsoft Entra app
The following steps explain how to configure support for a ReactJS application in Microsoft Entra.
Register an application using the steps described on Quickstart: Register an application with the Microsoft identity platform.
The Microsoft Entra app Application (client) ID and Directory (tenant) ID values appear in the Summary box. Record these values because they're required later.
Under the Manage list, select API permissions, then Add permission.
Add the PowerBI Service, select Delegated permissions, and select UserDataFunction.Execute.All or item.Execute.All permissions. Confirm that admin consent isn't required.
Go back to the Manage setting and select Authentication > Add a platform > Single-page application.
For local development purposes, add
http://localhost:3000under Redirect URIs and confirm that the application is enabled for the authorization code flow with Proof Key for Code Exchange (PKCE). Select the Configure button to save your changes. If the application encounters an error related to cross-origin requests, add the Mobile and desktop applications platform in the previous step with the same redirect URI.Back to Authentication, scroll down to Advanced Settings and, under Allow public client flows, select Yes for Enable the following mobile and desktop flows.
Create a console application
The function must be publicly accessible. In the Functions explorer, hover over the name of the function and select the ellipses icon (...) that appears, then select Properties. In the Properties pane that opens, enable Public access. You should also make a note of the Public URL to use in your Python application.
Create a new folder for your Python app, for example my-data-app. Open the folder in Visual Studio Code.
Set up the Python virtual environment in Visual Studio Code. To create local environments in Visual Studio Code, open the Command palette with Ctrl+Shift+P, then search for and select the Python: Create Environment command.
- The command presents a list of environment types and selects venv.
- Select the Python interpreter version Python 3.11.
Run the following command to activate the virtual environment in the Visual Studio Code terminal.
venv\Scripts\activate.batNext, run the command to install the Python libraries needed for this example.
pip install azure-identity, requestsCreate an
app.pyfile and use the code to invoke the user data functions item.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 User data function 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. 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)Note
The example is for development purposes only. Update the application to use Microsoft Entra ID authentication before using the application for a production use case.
Invoking a function from an external application
Functions can be invoked by issuing a REST call to the endpoint URL. Select the function you want to invoke in the Functions explorer and select Copy Function URL. You can also turn on or off the ability to use this URL externally from the Properties menu.
Then, use this URL in your application to invoke the function. See Invoke user data functions from an application
Output schema
When invoking a User Data Function from an external application, the output schema will have the following format:
{
"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*/
}
},
]
}
The following properties are returned:
- functionName: The name of the function that was executed.
- invocationId: The invocation ID for execution of a function.
- status: The outcome of the function's execution and supported values are
Succeeded,BadRequest,Failed,TimeoutandResponseTooLarge. - output: The output value returned by the function.
- errors: If any errors were captured, this will return a list of each error with their name, error message and error properties.
Response codes
The function will return the following HTTP codes as a result of the execution.
| Response code | Message | Description |
|---|---|---|
| 200 | Success | The request was successful. |
| 400 | Bad Request | The request was not valid. This response could be due to missing or incorrect input parameter values, data types or names. This response could also be caused by public access being turned off for a function. |
| 403 | Forbidden | The response was too large and the invocation failed. |
| 408 | Request Timeout | The request failed due to the execution taking more than 200 seconds. |
| 409 | Conflict | The request could not be completed due to a conflicting state. This could be caused by an unhandled exception or an error with user credentials. |
| 422 | Bad Request | The request failed due to a UserThrownError raised in the function. |
| 500 | Internal Server Error | The request failed due to an internal error in the service. |
Debugging and testing
Debug the application in Visual Studio Code using python debugger. Add breakpoints if needed to debug if any issues. Learn more