cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

dbutils or other magic way to get notebook name or cell title inside notebook cell

Hubert-Dudek
Esteemed Contributor III

Not sure it exists but maybe there is some trick to get directly from python code:

  • NotebookName
  • CellTitle

just working on some logger script shared between notebooks and it could make my life a bit easier 🙂

1 ACCEPTED SOLUTION

Accepted Solutions

jose_gonzalez
Moderator
Moderator

Hi @Hubert Dudek​ ,

The following command will print the path where your notebook is located. If you split the returned string and only select the last string, then you will be able to get the notebook name

notebook_name = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()

View solution in original post

11 REPLIES 11

Anonymous
Not applicable

Hey there! Thanks for your question. We'll see what the community comes up with first. Thanks in advance for your patience!

-werners-
Esteemed Contributor III

I am sure there is a trick but I doubt it is the best way:

the actual notebooks store each cell as # COMMAND ------ and markdown as # MAGIC %md.

So you could fetch the actual notebook code and start rebuilding it in python with a loop/count over COMMAND or MAGIC.

But as I said: seems overkill. Databricks can parse it nicely so there has to be a way.

-werners-
Esteemed Contributor III

Maybe you can use displayHTML instead of markdown? Like that you actually have it as code:

displayHTML("insert text here").

It is not as clean as markdown though.

Hubert-Dudek
Esteemed Contributor III

@Werner Stinckens​  I will experiment on that in few weeks using your ideas. I need it only when exception occur in notebook so maybe it will not be so big overkill. I will back to you.

-werners-
Esteemed Contributor III

you could also use the log analytics of your cloud provider.

For Azure f.e. we send all databricks logs to Log Analytics. I suppose on AWS/GCP there is also a similar thing.

(https://docs.microsoft.com/en-us/azure/databricks/administration-guide/account-settings/azure-diagnostic-logs)

Or send logs to some location which you can then parse with python

(https://docs.microsoft.com/en-us/azure/databricks/clusters/configure#cluster-log-delivery)

jose_gonzalez
Moderator
Moderator

Hi @Hubert Dudek​ ,

The following command will print the path where your notebook is located. If you split the returned string and only select the last string, then you will be able to get the notebook name

notebook_name = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()

Hubert-Dudek
Esteemed Contributor III

wow will test it tomorrow 🙂 haven't known about dbutils.notebook.entry_point

Did this helped you? if it did, could you mark it as best response?

Hubert-Dudek
Esteemed Contributor III

haven't tested yet (you now tomorrow in dev world can be quite long time) but I've selected your answer as the best

Anonymous
Not applicable

@Hubert Dudek​ - If I haven't told you, you are awesome!

Hubert-Dudek
Esteemed Contributor III

Thank you. You too 🙂

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group