Skip to content

Commit e82e5bc

Browse files
committed
DL autosync
1 parent 5e60692 commit e82e5bc

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/sempy_labs/directlake/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
warm_direct_lake_cache_isresident,
3232
warm_direct_lake_cache_perspective,
3333
)
34+
from ._autosync import set_autosync
3435

3536
__all__ = [
3637
"generate_shared_expression",
@@ -52,4 +53,5 @@
5253
"generate_direct_lake_semantic_model",
5354
"get_direct_lake_source",
5455
"update_direct_lake_model_connection",
56+
"set_autosync",
5557
]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import requests
2+
from sempy_labs._helper_functions import (
3+
_get_url_prefix,
4+
resolve_workspace_name_and_id,
5+
resolve_dataset_name_and_id,
6+
)
7+
from typing import Optional
8+
import sempy_labs._icons as icons
9+
from sempy._utils._log import log
10+
from uuid import UUID
11+
from sempy.fabric.exceptions import FabricHTTPException
12+
13+
14+
@log
15+
def set_autosync(
16+
dataset: str | UUID, workspace: Optional[str | UUID] = None, enable: bool = True
17+
):
18+
"""
19+
Enables or disables AutoSync for a Direct Lake semantic model.
20+
21+
Parameters
22+
----------
23+
dataset : str | uuid.UUID
24+
Name or ID of the semantic model.
25+
workspace : str | uuid.UUID, default=None
26+
The Fabric workspace name or ID.
27+
Defaults to None which resolves to the workspace of the attached lakehouse
28+
or if no lakehouse attached, resolves to the workspace of the notebook.
29+
enable : bool, default=True
30+
Whether to enable (True) or disable (False) AutoSync.
31+
"""
32+
33+
(workspace_name, workspace_id) = resolve_workspace_name_and_id(workspace)
34+
(dataset_name, dataset_id) = resolve_dataset_name_and_id(dataset, workspace_id)
35+
36+
import notebookutils
37+
38+
token = notebookutils.credentials.getToken("pbi")
39+
headers = {"Authorization": f"Bearer {token}"}
40+
41+
prefix = _get_url_prefix()
42+
43+
response = requests.get(
44+
url=f"{prefix}/metadata/models/{dataset_id}", headers=headers
45+
)
46+
id = response.json().get("model", {}).get("id")
47+
48+
payload = {"directLakeAutoSync": enable}
49+
response = requests.post(
50+
url=f"{prefix}/metadata/models/{id}/settings", headers=headers, json=payload
51+
)
52+
53+
if response.status_code != 204:
54+
raise FabricHTTPException(f"Failed to retrieve labels: {response.text}")
55+
56+
print(
57+
f"{icons.green_dot} Direct Lake AutoSync has been {'enabled' if enable else 'disabled'} for the '{dataset_name}' semantic model within the '{workspace_name}' workspace."
58+
)

0 commit comments

Comments
 (0)