4

Please don't give solution for IPython/Jupyter notebooks. The technology is different.

I want to get the path of my Databricks notebook dynamically. Which is something I can get from the UI "Copy File Path" as shown below. I am not able to use __file__ as that is giving me error.

NameError: name '__file__' is not defined

enter image description here

T.C. Proctor
  • 6,096
  • 6
  • 27
  • 37
Anirban Nag 'tintinmj'
  • 5,572
  • 6
  • 39
  • 59

2 Answers2

2

Each notebook has so-called "context" that contains a lot of information about environment, including the HTTP Path, hostname, etc. It could be retrieved as Python dict with something like this (it's simpler to convert to JSON & back than to fetch fields by .get functions):

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

inside the ctx dictionary there is a subdictionary named extraContext, that contains field notebook_path that is what you need:

nb_path = ctx['extraContext']['notebook_path']
Alex Ott
  • 80,552
  • 8
  • 87
  • 132
1

If there is anything like that available for you, it can be found by inspecting globals() and locals(), e.g.

print(globals())

In my case, the closest to this I found was:

_dh

e.g.:

print(_dh)
# ['/media/win10/Users/norok2/Downloads']
norok2
  • 25,683
  • 4
  • 73
  • 99
  • For Databricks it is giving `['/databricks/driver']` , which is not my expected output. – Anirban Nag 'tintinmj' Jun 29 '19 at 22:02
  • Have you asked DataBricks customer care? It may be your best shot. – norok2 Jun 29 '19 at 22:04
  • I am using community version of DBR. I don't think they will help. – Anirban Nag 'tintinmj' Jun 29 '19 at 22:05
  • 2
    Hi @AnirbanNag'tintinmj' you can retrieve the notebook paths using their REST API: https://docs.databricks.com/api/latest/workspace.html#list However, you have mentioned that you are using the community version. So please confirm first by trying API call if you have access to the REST API or not. – cody Jun 30 '19 at 11:51
  • I want to get the notebook path where I am writing the code. So I can use that for logging purpose. – Anirban Nag 'tintinmj' Jun 30 '19 at 18:01
  • This can be done in Scala: dbutils.notebook.getContext.notebookPath. If needed for Python or R, you will need to retrieve the path using %Scala cmd then pass the value to a widget where it can be retrieved using Python or R. – R7L208 Jan 14 '21 at 18:10