Search code examples
apache-sparkpysparkazure-synapseazure-synapse-analytics

Get a list of all Synapse notebook names in Azure Synapse Analytics


In Azure Synapse Analytics, I'm trying to create a code that returns a list with all notebook names.

In databricks I verified that it is possible by these two solutions:

  1. https://docs.databricks.com/dev-tools/api/latest/workspace.html#example
  2. Get list of all notebooks in my databricks workspace

, but I can't get a solution for synapse notebooks.

Can anyone please help me in achieving this?

Thank you!


Solution

    • You can use the Azure synapse REST API to get the list of notebooks available in the synapse workspace. The following is an image showing the available notebooks in my synapse workspace along with the code that you can use to achieve your requirement.

    enter image description here

    #install msal using !pip install msal for getting bearer token
    import msal
    
    client_id = "<client_id>"
    authority = "https://login.microsoftonline.com/<tenant_id>"
    client_secret = "<client_secret>"
    
    # Create a ConfidentialClientApplication instance
    app = msal.ConfidentialClientApplication(client_id=client_id, authority=authority, client_credential=client_secret)
    
    # Get the token
    scopes = ["https://dev.azuresynapse.net/.default"]
    result = app.acquire_token_for_client(scopes=scopes)
    print(result)
    

    enter image description here

    • Call the synapse Rest API using python's requests library (GET method).
    import requests
    
    response = requests.get("https://synapse3003.dev.azuresynapse.net/notebooks?api-version=2020-12-01", headers = {"Authorization":f"Bearer {result['access_token']}"}).json()
    
    print(len(response['value']))
    
    for i in response['value']:
        print(i)
    

    enter image description here

    NOTE: You need to create a service principle by navigating to azure AD-> App registration. After that go to your synapse studio->manage tab->Access control and then add your service principle with appropriate role to create token as in above procedure.