This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreDid you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now
Fabric User data functions is a platform that allows you to host and run serverless functions on Fabric. This empowers data developers to write custom logic and embed it into their Fabric ecosystem. This feature supports the Python 3.11.9 runtime and allows you to use public libraries from PyPI. In this blog post, we will walk through the process of creating, testing, and deploying a Fabric user data functions item using Visual Studio Code locally.
Before you begin, ensure you have the following:
Open Visual Studio Code and select the new item button in Fabric Workspace explorer.
Develop_test_and_deploy_a_user_data_functions_in_Microsoft_Fabric_using_Visual_S
Provide the following details to complete creation of the user data functions item:
User data functions project will open on your local machine. You will be prompted to set a Python virtual environment to debug and test locally. Continue through the next steps to set this up.
Create_virtual_environment_for_user_data_functions_item
Now you are ready to start adding your own functions function_app.py.
Imagine a scenario where you want to use validation functions that you can use in your notebooks such as validate email, phone, date for customer order data that needs to be validated before being written to a database.
Here are three such functions:
Replace functions_app.py with the following code:
import fabric.functions as fn
import logging
udf = fn.UserDataFunctions()
import re
from datetime import datetime
"""Validate an email address."""
@udf.function()
def is_valid_email(email: str) -> bool:
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
logging.info(f'validating the email address {email}.')
return re.match(pattern, email) is not None
"""Validate a phone number (basic international & local format)."""
@udf.function()
def is_valid_phone(phone: str) -> bool:
pattern = r'^\+?[\d\s\-().]{7,15}$'
logging.info(f'validating the phone number {phone}.')
return re.match(pattern, phone) is not None
"""Validate a datetime string against a format."""
@udf.function()
def is_valid_datetime(datestring: str ) -> bool:
try:
dateformat = "%Y-%m-%d %H:%M:%S"
logging.info(f'validating the date {datestring}.')
datetime.strptime(datestring, dateformat)
return True
except ValueError:
return False
IMPORTANT NOTE
You can open Fabric explorer in the local folder and see the User data function item you are editing and reflect all your local changes. You can view all the functions listed for this item. Select Run and debug to start debugging or press F5.
Select_Run_and_debug_to_test_a_function
Run and debug page will allow you to test any function within this item. Example of testing the is_valid_email() function.
Test_your_local_function_changes
In the local folder, select the user data function item. Click on publish to initiate deploying your changes to the workspace this function belongs to.
Publish_your_changes_to_fabric
Develop_test_and_deploy_a_user_data_functions_in_Microsoft_Fabric_using_Visual_S
Manage_connections_and_libraries_for_a_user_data_functions_item
Insert_sample_code_snippet_for_new_function
By following these steps, you will have successfully developed, debugged, and deployed a user data function to Microsoft Fabric. This is a simple validation example, but you can do more with User data functions and VS Code provides a local development experiences to develop and test these functions before making them ready for production use. Feel free to request features or report issues on VS Code fabric extension issues. Submit your feedback on Fabric Ideas and join the conversation on the Fabric Community.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.