cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I programmatically get my notebook path?

User16752241457
New Contributor II

I'm writing some code that trains a ML model using MLflow and a given set of hyperparameters. This code is going to be run by several folks on my team and I want to make sure that the experiment that get's created is created in the same directory as the notebook - i.e. if someone clones the notebook into their own user folder, the MLflow experiment should be pointed to their notebooks new location.

2 REPLIES 2

Ryan_Chynoweth
Honored Contributor III

In Scala you can use the "dbutils.notebook.getContext.notebookPath" function. If needed for Python or R, the only way to share this value would be to use widgets. Run the code with %scala, pass the value into a widget, then read the widget in Python or R.

User16857281974
Contributor

In Scala the call is

dbutils.notebook.getContext.notebookPath.get

In Python the call is

dbutils.entry_point.getDbutils().notebook().getContext().notebookPath().getOrElse(None)

If you need it in another language, a common practice would be to pass it through spark config.

Ignoring that we can get the value in Python (as seen above), if you start with a Scala cell like this:

%scala
val path = dbutils.notebook.getContext.notebookPath.get
spark.conf.set("com.whaterver.notebook-path")

And then in the next Python cell (the same concepts applies to R and SQL)

%python
path = spark.conf.get("com.whaterver.notebook-path")

You just want to adjust the configuration parameter's name to be a unique value so as not to accidentally clash with something. For example, we often use "com.databricks.training"

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.