Jelly
Jelly

Reputation: 23

Get Jupyter notebook name while using VSCode

I'm trying to get the path of the current Jupyter notebook, while using VSCode.

I tried all of the solutions in this post but none of them are working in VSCode. I can get them to work through Jupyterlab.

When I try the solutions that involve a %%javascript magic command, that cell will execute successfully, but the kernel is not updated with the variable containing the notebook name.

Upvotes: 1

Views: 1801

Answers (3)

Cam
Cam

Reputation: 1721

Or if you do not want the extension, the the filename, then try using pathlib.

from pathlib import Path
Path(globals()['__vsc_ipynb_file__']).stem

Upvotes: 0

Anshuman Nayak
Anshuman Nayak

Reputation: 166

Please try with this fix. This fix may vary from OS to OS. Follow me for more GitHub LinkedIn

import os
os.path.basename(globals()['__vsc_ipynb_file__'])

enter image description here

Upvotes: 10

Molly Wang-MSFT
Molly Wang-MSFT

Reputation: 9451

If you know the current opened notebook's filename, how about trying this method:

import os
notebook_path = os.path.abspath("a.ipynb")
print(notebook_path)

enter image description here

Upvotes: 0

Related Questions