by Audrey M. Roy Greenfeld | Tue, Apr 8, 2025
When working in notebooks, you'll occasionally need to access the notebook's own filename. Here's code to do it, which I've verified works in Jupyter Notebook Classic.
import IPython
You can't get the notebook's name from Python. But you can get it from JavaScript, and then execute Python code to store it into a Python variable:
%%javascript IPython.notebook.kernel.execute('nbname = "' + IPython.notebook.notebook_name + '"')
IPython.notebook.kernel.execute()
is a JS method that tells the Jupyter kernel to execute a string of Python.
Let's print the Python variable we set via JS:
nbname