1

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.

Jelly
  • 23
  • 6
  • Update: I think the issue is how my VSCode is handling the %%javascript magic output. With nodejs in my venv, I can get the code below to display the expected output. %%script node console.log("hello") However, if I try the same with %% javascript, I get as the output, ie: %%javascript console.log("hello") – Jelly Dec 15 '21 at 18:26

2 Answers2

8

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

Anshuman Nayak
  • 146
  • 1
  • 4
0

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

Molly Wang-MSFT
  • 7,943
  • 2
  • 9
  • 22
  • Thanks for the response! This is a reasonable workaround, but I'd still like to get the current notebook name if possible. Mostly I'm curious why the solutions that work for Jupyter Lab don't work for VSCode, it points to VSCode handling javascript/magic commands differently. – Jelly Dec 09 '21 at 16:12