How do I get the current IPython / Jupyter Notebook name
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Getting the current Jupyter notebook's filename is not straightforward because the notebook runs in a client-server architecture where the kernel does not directly know which notebook file it belongs to. The most reliable methods are: using ipynbname (a dedicated library), querying the Jupyter server API, or using JavaScript to read the notebook name from the browser. For JupyterLab, the jpserver_extensions API and environment variables provide additional options.
Method 1: ipynbname Library (Simplest)
ipynbname queries the Jupyter server API to find which notebook file is connected to the current kernel. It works in both Jupyter Notebook and JupyterLab.
Method 2: Query the Jupyter Server API
Method 3: JavaScript Bridge (Classic Notebook)
This method uses the browser's JavaScript context to read the notebook name and inject it into the Python kernel. It does not work in JupyterLab or headless execution.
Method 4: Using vsc_ipynb_file (VS Code)
Method 5: Environment Variable Approach
Practical Use Cases
Fallback Pattern
Common Pitfalls
- Using JavaScript injection in JupyterLab: The
IPython.notebook.notebook_nameJavaScript variable exists only in the classic Jupyter Notebook interface. JupyterLab has a different DOM structure, and theIPython.notebookobject does not exist. Useipynbnameor the server API instead, which work in both environments. list_running_servers()import path changed: In newer versions of Jupyter,list_running_serversmoved fromnotebook.notebookapptojupyter_server.serverapp. If you get anImportError, tryfrom jupyter_server.serverapp import list_running_serversas a fallback.- Notebook name not available during non-interactive execution: When a notebook is executed by
papermill,nbconvert, or a CI pipeline, there is no Jupyter server to query.ipynbnameand the server API both fail. Use environment variables or pass the notebook name as a parameter in these cases. - Kernel restart clearing the JavaScript-injected variable: If you use the JavaScript bridge method to set
nb_namein the Python kernel, restarting the kernel clears all Python variables includingnb_name. You must re-run the JavaScript cell after every kernel restart. Library-based methods do not have this issue. - Security token required for server API queries: The Jupyter server API requires an authentication token.
list_running_servers()provides the token from the local server config, but in multi-user environments (JupyterHub), the token may differ or require explicit configuration.ipynbnamehandles this internally.
Summary
- Use
ipynbname(pip install ipynbname) for the simplest, most portable solution - Query the Jupyter server API for environments where you cannot install packages
- Use
__vsc_ipynb_file__when running notebooks in VS Code - Implement a fallback chain that tries multiple methods for maximum compatibility
- For non-interactive execution (papermill, CI), pass the notebook name as a parameter or environment variable
Related reading
- How do I get the row count of a Pandas DataFrame?
- How do I go from Pandas DataFrame to Tensorflow BatchDataset for NLP?
- How do I import Pandas on Apple M1 chip
- How do I increase the cell width of the Jupyter/ipython notebook in my browser?
- How do I get the current time in milliseconds in Python?
- How do I get the current time in milliseconds in Python?
- how do I insert a column at a specific column index in pandas?
- How do I make a single legend for many subplots?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.