Forum Discussion
Update Notebook attached lakehouse using code
- 1 year ago
Hi chetanhiwale,
You can use the following code to attach a different lakehouse as the default one. This would make the lakehouse default through out the session. But make sure you run this code in the first cell
%%configure -f { "defaultLakehouse": { "name": "lakehouse_name", "id": "lakehouse_id", "workspaceId": "workspace_id" } } - 1 year ago
Hi chetanhiwale ,
you can use notebookutils to achieve this. When you open a new notebook, you can run this code (filling out the workspace specific IDs according to your workspace) which will change the default lakehouse of your target notebook:
import sempy.fabric as fabric # INSERT YOUR WORKSPACES SPECIFIC INFORMATION HERE workspace_name = "workspace_id" item_name = "notebook_name" replacement_dict = { "lakehouse_id" : { "old" : "LH_ID_old", "new" : "LH_ID_new", }, "lakehouse_name" : { "old" : "LH_old", "new" : "LH_new", }, "workspace_id_of_lakehouse" : { "old" : "workspace_id_old", "new" : "workspace_id_new", }, } workspace_id = fabric.resolve_workspace_id(workspace_name) items = fabric.list_items(workspace=workspace_name) item_id = items.where(items["Display Name"] == f"{item_name}").dropna().Id.item() item_type = items.where(items["Display Name"] == f"{item_name}").dropna().Type.item() definition = notebookutils.notebook.getDefinition(item_name, workspace_id) for replacement in replacement_dict.keys(): definition = definition.replace(replacement_dict[replacement]["old"], replacement_dict[replacement]["new"]) notebookutils.notebook.updateDefinition(name=item_name, content=definition, workspaceId=workspace_id)Feel free to try it out and let me know if it works for you. If this resolves your request, kindly mark this as solution to help other users find it. 🙂
Kind regards,
Niels
Hi chetanhiwale ,
you can use notebookutils to achieve this. When you open a new notebook, you can run this code (filling out the workspace specific IDs according to your workspace) which will change the default lakehouse of your target notebook:
import sempy.fabric as fabric
# INSERT YOUR WORKSPACES SPECIFIC INFORMATION HERE
workspace_name = "workspace_id"
item_name = "notebook_name"
replacement_dict = {
"lakehouse_id" : {
"old" : "LH_ID_old",
"new" : "LH_ID_new",
},
"lakehouse_name" : {
"old" : "LH_old",
"new" : "LH_new",
},
"workspace_id_of_lakehouse" : {
"old" : "workspace_id_old",
"new" : "workspace_id_new",
},
}
workspace_id = fabric.resolve_workspace_id(workspace_name)
items = fabric.list_items(workspace=workspace_name)
item_id = items.where(items["Display Name"] == f"{item_name}").dropna().Id.item()
item_type = items.where(items["Display Name"] == f"{item_name}").dropna().Type.item()
definition = notebookutils.notebook.getDefinition(item_name, workspace_id)
for replacement in replacement_dict.keys():
definition = definition.replace(replacement_dict[replacement]["old"], replacement_dict[replacement]["new"])
notebookutils.notebook.updateDefinition(name=item_name, content=definition, workspaceId=workspace_id)Feel free to try it out and let me know if it works for you. If this resolves your request, kindly mark this as solution to help other users find it. 🙂
Kind regards,
Niels