summaryrefslogtreecommitdiff
path: root/content/blog/jupyterlab_notebook_file_oneliner
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2025-06-30 14:48:34 +0200
committerjaseg <git@jaseg.de>2025-06-30 14:48:34 +0200
commita324ba7b649840c16d365b757b19db2849991f11 (patch)
treea9cd16a30404f22a6094b0fe664d392af1cf2ad0 /content/blog/jupyterlab_notebook_file_oneliner
parentbce789de7b4ab2d0d2c58e14b8e221ed5817c09d (diff)
downloadblog-a324ba7b649840c16d365b757b19db2849991f11.tar.gz
blog-a324ba7b649840c16d365b757b19db2849991f11.tar.bz2
blog-a324ba7b649840c16d365b757b19db2849991f11.zip
WIP
Diffstat (limited to 'content/blog/jupyterlab_notebook_file_oneliner')
-rw-r--r--content/blog/jupyterlab_notebook_file_oneliner/index.rst21
1 files changed, 21 insertions, 0 deletions
diff --git a/content/blog/jupyterlab_notebook_file_oneliner/index.rst b/content/blog/jupyterlab_notebook_file_oneliner/index.rst
new file mode 100644
index 0000000..8885c4c
--- /dev/null
+++ b/content/blog/jupyterlab_notebook_file_oneliner/index.rst
@@ -0,0 +1,21 @@
+---
+title: "Getting the .ipynb Notebook File Location From a Running Jupyter Lab Notebook"
+date: 2025-06-30T23:42:00+01:00
+summary: >
+ If you need to get the path of the ipynb file in a running #Jupyter notebook, this one-liner will do the trick. It
+ seems chatgpt is confused, and a bunch of other approaches on the web look fragile and/or unnecessarily complex to
+ me.
+---
+
+If you need to get the path of the ipynb file in a running #Jupyter notebook, this one-liner will do the trick. It seems
+chatgpt is confused, and a bunch of other approaches on the web look fragile and/or unnecessarily complex to me.
+
+.. code:: python
+
+ import sys
+ Path(json.loads(Path(sys.argv[-1]).read_bytes())['jupyter_session'])
+
+The way this works is that for each notebook, jupyter starts a python "kernel" process that actually runs the notebook's
+code. That kernel gets a json file with info on the notebook's location on the disk passed through its command line.
+Since we're running code in that exact python process, we can just grab that json file from sys.argv, and read it
+ourselves.