Forum Discussion
bruinsmm
9 months agoFrequent Visitor
Access variable library in UDF
Microsoft has published the following code to get the values of the variable library, but in UDF in the connection manager i cann't find a variable library to select them. What's do i'm wrong? @u...
tayloramy
9 months agoSuper User
Hi bruinsmm,
In Fabric, a UDF can read a Variable Library only after you both create a Manage connections entry that points at the Variable Library item and reference that connection’s alias in your @udf.connection(...) decorator. If you skip step 1, the Variable Library won’t appear in the UDF’s connection picker.
- Create the Variable Library item
Workspace > New > Variable library > add variables (e.g., ENV, DEV_FILE_PATH, PROD_FILE_PATH).
Docs: Variable Library overview - Create a workspace connection to that Variable Library
Workspace header > Manage connections > New connection > Fabric item > Variable library > pick your VL item > note the Alias
Docs: Connect to data sources for UDFs - Reference the alias in your UDF code and use the correct type
import fabric.functions as fn
from fabric.functions import udf
@udf.connection(argName="varLib", alias="MyVarLib")
@udf.function()
def get_storage_path(dataset: str, varLib: fn.FabricVariablesClient) -> str:
variables = varLib.getVariables()
env = (variables.get("ENV") or "").lower()
dev_path = variables.get("DEV_FILE_PATH") or ""
prod_path = variables.get("PROD_FILE_PATH") or ""
if env == "dev":
return f"{dev_path}{dataset}/"
elif env == "prod":
return f"{prod_path}{dataset}/"
else:
return "incorrect settings defined for ENV variable"4. Publish the UDF, then open the UDF item and check the Connections pane. You should now see the Variable Library bound via the alias.
If you found this helpful, consider giving some Kudos. If I answered your question or solved your problem, mark this post as the solution.