If you are working with Databricks notebooks, you might want to know the name of the current notebook you are running. This can be useful for logging, debugging, or generating reports. In this blog post, I will show you how to get the current notebook name in Databricks using Python.
The easiest way to get the current notebook name in Databricks is to use the
method. This method returns a string that contains the full path of the current notebook, including the folder and the file name. For example, if your notebook is located in /Folder/Notebook Name, then this method will return ‘/Folder/Notebook Name’.
To use this method, you need to
import dbutils from pyspark.sql.functions. You can then call the method inside your notebook and print or store the result as you wish. Here is an example of how to use this method:
# Import dbutils from pyspark.sql.functions import dbutils # Get the current notebook name notebook_name = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() # Print the result print(notebook_name)
This will print something like
/Folder/Notebook Name in your output cell.
Note that this method only works in Databricks notebooks and not in other environments such as Jupyter or Colab. If you try to run this code in a non-Databricks environment, you will get an error saying that
dbutils is not defined.
Em, excuse me! Have Android 📱 and use Databricks? You might be interested in my totally free (and ad-free) Pocket Bricks . You can get it from Google Play too:
To contact me, send an email anytime or leave a comment below.