Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get workspace name inside a python notebook in databricks

I am trying get the workspace name inside a python notebook. Is there any way we can do this?

Ex: My workspace name is databricks-test. I want to capture this in variable in python notebook

like image 603
sande Avatar asked Mar 30 '26 22:03

sande


2 Answers

To get the workspace name (not Org ID which the other answer gives you) you can do it one of two main ways

spark.conf.get("spark.databricks.workspaceUrl")

which will give you the absolutely URL and you can then split on the first. i.e

spark.conf.get("spark.databricks.workspaceUrl").split('.')[0]

You could also get it these two ways:

dbutils.notebook.entry_point.getDbutils().notebook().getContext() \
.browserHostName().toString()

or

import json

json.loads(dbutils.notebook.entry_point.getDbutils().notebook() \
  .getContext().toJson())['tags']['browserHostName']

Top tip if you're ever wondering what Spark Confs exist you can get most of them in a list like this:

sc.getConf().getAll()
like image 155
Scott Bell Avatar answered Apr 02 '26 13:04

Scott Bell


I'm sure there is a better way, but the following is how I do it:

from databricks.sdk import WorkspaceClient

wksp = WorkspaceClient()

workspace_id = wksp.get_workspace_id()
workspace_name = spark.sql(f"""
SELECT workspace_name 
FROM system.access.workspaces_latest 
WHERE workspace_id = {workspace_id}"""
).first()[0]

At the time I write this, the system.access.workspaces_latest view is in public preview. The databricks SDK for Python is a part of the current LTS Databricks Runtime and serverless.

like image 30
Adam Avatar answered Apr 02 '26 13:04

Adam