Can a jupyter notebook find its own filename?

断了今生、忘了曾经 提交于 2020-07-20 07:33:07

问题


Is it possible for a jupyter notebook to get the name of its own file, similarly to what we would do from a python script?

os.path.basename(__file__) doesn't seem to work, at least for me on jupyterlab

sys.argv[0] returns my_home/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py


回答1:


The only way I've found is through JavaScritp as in this answer.

The compact form is a cell like this:

%%javascript
IPython.notebook.kernel.execute(`notebookName = '${window.document.getElementById("notebook_name").innerHTML}'`);

after that you'll have the variable notebookName with the name that appears at the top of the page.

A better solution may be using IPython.notebook.notebook_name:

%%javascript
IPython.notebook.kernel.execute(`notebookName = '${IPython.notebook.notebook_name}'`);

it gives you the name with the extension .ipynb



来源:https://stackoverflow.com/questions/52691468/can-a-jupyter-notebook-find-its-own-filename

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!