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
Thanks govindarajan_d for answer the question . Is there any other way we can achieve this , by update notebook defination or something else.
- govindarajan_d1 year ago
Super User
Hi chetanhiwale,
I didn't get your requirement on this. Can you elaborate on what you are trying to do?
- govindarajan_d1 year ago
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:
Updating notebook definition for changing lakehouse configuration dynamically would be too much work.