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
Debugging your code is important to identify issues and mitigate them when you're working with user data functions in Microsoft Fabric. You want to make sure everything works as it should and that’s where local debugging lets you catch problems in your code without messing with the live environment. In this blog post, I will walk you through the steps to make local debugging easier and faster.
Before you dive in, make sure you have:
To open a user data function item in VS Code from the portal, start by navigating to the Microsoft Fabric portal and locating the specific user data functions item you want to work with. Select Open in VS Code to open the item in VS Code.
How_to_debug_user_data_functions_locally_in_VS_Code
You will be notified to create a Python virtual environment for user data functions project that was opened in VS Code. This will be used to set up the libraries used by user data functions project.
How_to_debug_user_data_functions_locally_in_VS_Code
Select Python 3.11.9 runtime version when creating the virtual environment.
How_to_debug_user_data_functions_locally_in_VS_Code
Once the virtual environment is created , you can start writing your own functions.
In the Fabric explorer you will find all the connections and libraries added to this user data functions item.
How_to_debug_user_data_functions_locally_in_VS_Code
With a database connection, add this function to function_app.py to create an employee table and insert data.
@udf.connection(argName="sqlDB",alias="<alias for sql database>")
@udf.function()
def write_one_to_sql_db(sqlDB: fn.FabricSqlConnection, employeeId: int, employeeName: str, deptId: int) -> str:
# Replace with the data you want to insert
data = (employeeId, employeeName, deptId)
# Establish a connection to the SQL database
connection = sqlDB.connect()
cursor = connection.cursor()
# Create the table if it doesn't exist
create_table_query = """
IF OBJECT_ID(N'dbo.Employee', N'U') IS NULL
CREATE TABLE dbo.Employee (
EmpID INT PRIMARY KEY,
EmpName nvarchar(50),
DepID INT
);
"""
cursor.execute(create_table_query)
# Insert data into the table
insert_query = "INSERT INTO Employee (EmpID, EmpName, DepID) VALUES (?, ?, ?);"
cursor.execute(insert_query, data)
# Commit the transaction
connection.commit()
# Close the connection
cursor.close()
connection.close()
return "Employee table was created (if necessary) and data was added to this table"
Select F5 to debug your Fabric functions. You can add a breakpoint anywhere in your code. In debug mode, your breakpoints are hit as expected and test your code as you would test a deployed function.
How_to_debug_user_data_functions_locally_in_VS_Code
Run the function step by step, observing variable values and understanding the behavior. You can use the following to help debug issues:
You may see this since the libraries are not installed in your virtual environment. When you start debugging the function the first time, the libraries will be installed as pip install from the requirements.txt is executed during debugging.
Today, you can publish the functions code changes, but new libraries added in requirements.txt file will not be added to the user data functions item during publish. After publishing the changes, you would need to add the new libraries in the portal using library management and publish it again. We understand this is not ideal and are working on improving the experience with libraries.
You can use local.settings.json to store connection strings and use them in your code.
Not at this time. All data connections need to be added to the user data functions item in the portal. Sync the changes locally to run these functions in VS Code.
By running your function in a controlled environment locally in VS Code, you can find and fix issues faster, and make sure your code works as expected. Explore the Fabric User data functions documentation that guide you through connecting data sources, building pipelines, integrating with Notebooks and Power BI. 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.