Get job run URL in notebook

This article shows you how to programmatically read the job run URL and then output the value within a notebook.

There is not a standard API call for this function, so we will be using a combination of DBUtils and Python.

Note

The job run URL can be found manually by opening the Jobs section in your workspace, clicking the job Name, and the job Run. This is useful when browsing your workspace, but doesn’t help if you want to record the job run URL in your notebook output.

Import notebook into a JSON file

We start out by using DBUtils to call notebook.entry_point and read the job context into a JSON file.

import json
notebook_info = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())
print(notebook_info)

Example output when printing notebook_info:

{'rootRunId': None, 'currentRunId': None, 'jobGroup': 'xxxxxx', 'tags': {'opId': 'ServerBackend-xxxxxx', 'opTarget': 'com.databricks.backend.common.rpc.InternalDriverBackendMessages$StartRepl', 'clusterMemory': 'xxxx', 'serverBackendName': 'com.databricks.backend.daemon.driver.DriverCorral', 'notebookId': 'xxxxxx', 'projectName': 'chauffeur', 'eventWindowTime': 'xxxxxx', 'httpTarget': '/notebook/xxxxx/command/xxxxx', 'buildHash': 'xxxxxxxxxxx', 'browserHash': '#notebook/xxxxx/command/xxxxx', 'host': 'xxxxxxxx', 'notebookLanguage': 'python', 'sparkVersion': '7.3.x-scala2.12', 'hostName': 'xxxxxxx', 'httpMethod': 'POST', 'browserIdleTime': '466', 'jettyRpcJettyVersion': '9', 'browserTabId': 'xxxxxxxx', 'sourceIpAddress': 'xx.xx.xx.xx', 'browserUserAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xxxxx Safari/5xxxx', 'orgId': 'xxxxx', 'userAgent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/xx.0.xx.xx Safari/xx.xx', 'clusterId': 'xxxx-xxxx-xxxxx', 'rootOpId': 'ServiceMain-xxxxx', 'sessionId': 'xxxxxxxx', 'clusterCreator': 'Webapp', 'clientBranchName': 'x.xxx.xxxx', 'clusterType': 'spot', 'browserHasFocus': 'true', 'userId': 'xxxxxxx', 'browserIsHidden': 'false', 'principalIdpId': 'xxxxxxx', 'opType': 'ServerBackend', 'sourcePortNumber': '0', 'user': 'xxxxxxxx@databricks.com', 'principalIdpObjectId': 'xxxxxx', 'browserHostName': 'adb-xxxxxx.12.databricks.net', 'parentOpId': 'RPCClient-xxxx', 'jettyRpcType': 'InternalDriverBackendMessages$DriverBackendRequest'}, 'extraContext': {'aclPathOfAclRoot': '/workspace/xxxx/xxxx/xxxxxx', 'notebook_path': '/Users/xxxxxxx@databricks.com/Print Job URL', 'api_url': 'https://xxxxx.databricks.net';, 'api_token': '[REDACTED]'}, 'credentialKeys': ['xxxxxx', 'xxxxxx']}

Read values from JSON file

All of the information we need is in the JSON file.

Read the values that make up the job run URL from the JSON file.

jobId = notebook_info["tags"]["jobId"]
runid = notebook_info["tags"]["idInJob"]
browserHostName = notebook_info["tags"]["browserHostName"]
print(browserHostName)
orgId = notebook_info["tags"]["orgId"]
print(orgId)

Construct the job run URL

Use the variables created in the previous step to construct the job run URL.

if orgId == "0":
  job_path = f'https://{browserHostName}/#job/{jobId}/run/{runid}'
  print(job_path)
else:
  job_path = f'https://{browserHostName}/?o={orgId}/#job/{jobId}/run/{runid}'
  print(job_path)

The value of job_path is your job run URL. The exact construction depends on the cluster used.

For example, https://test.cloud.databricks.com/#job/6/run/2 and https://6811994319967754.14.databricks.net/?o=6811994319967754#job/6/run/2 are two different ways to format the job run URL.