Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document JPY_SESSION_NAME environment variable, which contains current notebook's file path #16282

Open
simonrouse9461 opened this issue May 5, 2024 · 6 comments

Comments

@simonrouse9461
Copy link

simonrouse9461 commented May 5, 2024

Problem

Currently there is no official way of retrieving the notebook's file path dynamically during runtime. Although a package called ipynbname is created for this purpose, it is somehow restrictive and doesn't work under all scenarios (for example, it doesn't work if the jupyterlab is running behind a proxy).

Proposed Solution

VisualStudio Code Jupyter extension implemented a mechanism similar to __file__ in pure python script. The file path of current running notebook is saved to a global variable __vsc_ipynb_file__. It would be really helpful if jupyterlab can support a similar mechanism by saving the file path to __ipynb_file__ or something similar.

Additional context

See microsoft/vscode-jupyter#8531

@jupyterlab-probot jupyterlab-probot bot added the status:Needs Triage Applied to new issues that need triage label May 5, 2024
@krassowski
Copy link
Member

If you are using the latest versions of IPython/ipykernel and jupyter-serer, then there is an environment variable called JPY_SESSION_NAME which contains the path to the notebook. You can then assign it to __ipynb_file__ if you like:

import os
__ipynb_file__ = os.environ['JPY_SESSION_NAME']

In general, Jupyter works with a variety of kernels, not only with Python, so injecting a runtime variable is rather hard or even impossible to do for all kernels consistently (for example, this may be invalid variable name in some languages).

@krassowski
Copy link
Member

Currently there is no official way of retrieving the notebook's file path dynamically during runtime

As per above, this is incorrect. The problem is that this is not documented in JupyterLab documentation. I think it should be as a hint given how frequent this question is (and this issue is probably a duplicate).

@simonrouse9461
Copy link
Author

Thanks for the answer!
I tried this approach, os.environ['JPY_SESSION_NAME'] actually gives /path/to/dir/5d2643a2-3a3a-4186-830b-c1d915ca1aad instead of /path/to/dir/notebook.ipynb. It is possible to have the latter supported?

@krassowski
Copy link
Member

I do get /path/to/dir/notebook.ipynb. Did you check that you have all latest versions of dependencies I mentioned?

@simonrouse9461
Copy link
Author

Thanks for pointing out, it seems that my dependencies are outdated. Updating them solves the problem.

@JasonWeill JasonWeill changed the title Support __ipynb_file__ global variable just like __vsc_ipynb_file__ in VisualStudio Code Document JPY_SESSION_NAME environment variable, which contains current notebook's file path May 7, 2024
@JasonWeill JasonWeill removed the status:Needs Triage Applied to new issues that need triage label May 7, 2024
@JasonWeill
Copy link
Contributor

Previous discussion (thanks @krassowski ) — #8012 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants