-
Notifications
You must be signed in to change notification settings - Fork 32
Description
Is your feature request related to a problem? Please describe.
After reading this thread: https://www.reddit.com/r/MicrosoftFabric/comments/1p5oduh/mssqlpython_with_pandas_or_polars_warnings_and/, I think we have an opportunity to improve the experience.
The current experience using mssql-python with Fabric Notebooks has a ton of friction. Here's an example:
`import struct
import mssql_python
import pandas as pd
connection_string = (
f"Server={server};"
f"Database={database};"
"Encrypt=yes;"
)
access_token = notebookutils.credentials.getToken('pbi')
token = access_token.encode("UTF-16-LE")
token_struct = struct.pack(f'<I{len(token)}s', len(token), token)
SQL_COPT_SS_ACCESS_TOKEN = 1256
connection = mssql_python.connect(connection_string, attrs_before={SQL_COPT_SS_ACCESS_TOKEN: token_struct})
tables = ["Customers", "Invoices", "Orders"]
for table in tables:
query = f"SELECT TOP 5 * FROM Sales.{table}"
pd_data = pd.read_sql_query(query, connection)
print(pd_data.dtypes)
print(pd_data.info())
print(pd_data)
display(pd_data)
Close the connection
connection.close()`
Describe the solution you'd like
We could improve on this quite a bit by adding a Fabric Notebook connection option and allowing worskpace and database name to be passed like it is with tsql magic in Fabric Notebooks. Here's an example:
`import struct
import mssql_python
import pandas as pd
connection_string = (
f"Workspace={workspace};"
f"Database={database};"
"Authentication=FabricNotebook;"
"Encrypt=yes;"
)
connection = mssql_python.connect(connection_string)
tables = ["Customers", "Invoices", "Orders"]
for table in tables:
query = f"SELECT TOP 5 * FROM Sales.{table}"
pd_data = pd.read_sql_query(query, connection)
print(pd_data.dtypes)
print(pd_data.info())
print(pd_data)
display(pd_data)
Close the connection
connection.close()`
Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.
Additional context
This seems to be a fairly standard pattern: https://www.reddit.com/r/MicrosoftFabric/comments/1p6mkn3/connect_to_fabric_warehouse_from_python_notebook/