Semantic Link for Microsoft Fabric (SemPy)
Semantic link connects Microsoft Fabric's Power BI semantic models with data science tools. SemPy is the Python library that is part of semantic link, providing functionalities to analyze and manipulate data from Power BI models directly within notebooks. It integrates deeply with Fabric DataFrames and allows for tasks like listing Power BI items, reading tables, and calculating measures. The current version is 0.14.0, and it's actively developed by Microsoft.
Warnings
- gotcha SemPy is primarily designed for and best utilized within Microsoft Fabric notebooks. Running its Fabric-specific functionalities (e.g., `sempy.fabric` functions) outside of this environment will typically fail due to authentication or environment dependencies.
- gotcha The library primarily interacts with Power BI semantic models and their underlying data. It's not a general-purpose data manipulation library for arbitrary data sources, although it can work with pandas DataFrames as an entry point.
- breaking As a pre-1.0 library (version 0.x.x), SemPy's API might experience breaking changes between minor versions. Always check the official documentation or release notes when upgrading.
- gotcha SemPy requires Python 3.10 or newer. Using older Python versions will result in installation or runtime errors.
Install
-
pip install semantic-link-sempy
Imports
- FabricDataFrame
from sempy.fabric import FabricDataFrame
- list_workspaces
from sempy.fabric import list_workspaces
- read_table
from sempy.fabric import read_table
Quickstart
import pandas as pd
from sempy.fabric import FabricDataFrame, list_workspaces
# Create a sample pandas DataFrame
df_pd = pd.DataFrame({
'ID': [1, 2, 3],
'Value': [100, 200, 150]
})
# Convert a pandas DataFrame to a FabricDataFrame
df_sempy = FabricDataFrame(df_pd)
print("--- FabricDataFrame created ---")
print(df_sempy.head())
print(f"Type: {type(df_sempy)}\n")
# Attempt to list Fabric workspaces
# This function (and most of sempy.fabric) requires an active Microsoft Fabric notebook environment
# and an authenticated session to execute successfully.
print("--- Attempting to list Fabric workspaces ---")
try:
workspaces = list_workspaces()
print(f"Successfully listed {len(workspaces)} workspaces.")
if workspaces:
print(f"First workspace: {workspaces[0]['display_name']}")
except Exception as e:
print(f"Failed to list workspaces. This is expected if not running in a Fabric notebook or without proper authentication.\nError: {e}")