Forum Discussion

chetanhiwale's avatar
chetanhiwale
Resolver I
1 year ago
Solved

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 .  

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

     

  • 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 

7 Replies

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

     

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

     

    • govindarajan_d's avatar
      govindarajan_d
      Super User

      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:

       

      You can check the following documentation as well: 

       

      https://learn.microsoft.com/en-us/fabric/data-engineering/author-execute-notebook#parameterized-session-configuration-from-a-pipeline

       

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

  • 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 

  • Anonymous's avatar
    Anonymous
    Not applicable

    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