Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us at FabCon Vienna from September 15-18, 2025, for the ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM. Get registered

Reply
chetanhiwale
Helper II
Helper II

Update Notebook attached lakehouse using code

I need to attach a new lakehouse to notebook using rest end point. 

Scenario :

I have a notebook which has default lakehouse attached. I need to update to different lakehouse. 
Apart from restendpoint , is there any way we can acheive it .  

2 ACCEPTED SOLUTIONS
govindarajan_d
Super User
Super User

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"
    }
}

 

View solution in original post

ObungiNiels
Resolver III
Resolver III

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 

View solution in original post

7 REPLIES 7
chetanhiwale
Helper II
Helper II

Thanks @govindarajan_d  for helping me out here. The solution you provided really worked for me. Thanks again !!

v-vpabbu
Community Support
Community Support

Hi @chetanhiwale,

 

Thankyou @govindarajan_d and @ObungiNiels  for taking time and providing solution.

we would like to follow up to see if the solution provided by the super user resolved your issue. Please let us know if you need any further assistance.
If our super user response resolved your issue, please mark it as "Accept as solution" and click "Yes" if you found it helpful.

 

Regards,

Vinay Pabbu

ObungiNiels
Resolver III
Resolver III

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 

chetanhiwale
Helper II
Helper II

Thanks @govindarajan_d  for answer the question . Is there any other way we can achieve this , by update notebook defination or something else. 

 

Hi @chetanhiwale,

 

If you are looking to parameterize the configuration, so that you can dynamically pass the lakehouse name, following should work for you. 

%%configure -f 
{
    "defaultLakehouse": { 
        "name": {
			"parameterName": "DefaultLakehouseName", 
			"defaultValue": "Raw_LH"
		},
        "id": {
			"parameterName": "LakehouseGUID", 
			"defaultValue": "23adfdsf-2safsdf-wwer"
		},
        "workspaceId": {
			"parameterName": "WorkspaceGUID", 
			"defaultValue": "adfase-2safsdf-wwer"
		}
    }
}

And then you can pass parameters in notebook activity in Data pipeline:

govindarajan_d_0-1737728647303.png

 

You can check the following documentation as well: 

 

https://learn.microsoft.com/en-us/fabric/data-engineering/author-execute-notebook#parameterized-sess...

 

Updating notebook definition for changing lakehouse configuration dynamically would be too much work. 

Hi @chetanhiwale,

 

I didn't get your requirement on this. Can you elaborate on what you are trying to do?

govindarajan_d
Super User
Super User

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"
    }
}

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June FBC25 Carousel

Fabric Monthly Update - June 2025

Check out the June 2025 Fabric update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.