Forum Discussion

Nytar's avatar
Nytar
Frequent Visitor
2 years ago
Solved

How to List and Execute Notebooks in Microsoft Fabric?

Hi,

I'm using Microsoft Fabric and need help managing notebooks in my workspace. Specifically:

  1. How to list notebooks stored in various directories?
  2. How to execute these notebooks from a central notebook?

My current function lists objects in lakehouses:

 

# List all objects in the root folder
root = mssparkutils.fs.ls("/")

for item in root:
    print(item.name)  # Print the name of the object

    # List all objects in the subfolder
    subitems = mssparkutils.fs.ls(item.path)
    for subitem in subitems:
        print("  " + subitem.name)  # Print the name of the subitem

        # List all objects in the subsubfolder
        subsubitems = mssparkutils.fs.ls(subitem.path)
        for subsubitem in subsubitems:
            print("    " + subsubitem.name)  # Print the name of the subsubitem

 

However, this code doesn’t list my notebooks. Any guidance would be appreciated!

Thank you!

 

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Nytar ,

     

    Thanks frithjof_v for your reply.

     

    Please execute the following code in notebook:

    import requests
    
    workspaceID = 'your_workspace_id'
    api_url = f'https://api.fabric.microsoft.com/v1/workspaces/{workspaceID}/notebooks'
    headers = {
       "Authorization": "Bearer your_token",
       "Content-Type": "application/json"
    }
    
    res = requests.get(api_url,headers=headers)
    note = res.json().get("value",[])
    
    for note in note:
        if note['type'] == 'Notebook':
            print(note['displayName'])

     

    Replace workspaceid and your_token with your own.

     

    The result of the execution is shown below, which lists all the notebooks in my workspace.

     

    You can click on this official document for more information about it:

    Items - List Notebooks - REST API (Notebook) | Microsoft Learn

     

    If you have any other questions please feel free to contact me.

     

    Best Regards,
    Yang
    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

2 Replies