diff options
Diffstat (limited to 'blog/jupyterlab-notebook-file-oneliner/index.html')
-rw-r--r-- | blog/jupyterlab-notebook-file-oneliner/index.html | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/blog/jupyterlab-notebook-file-oneliner/index.html b/blog/jupyterlab-notebook-file-oneliner/index.html new file mode 100644 index 0000000..3662214 --- /dev/null +++ b/blog/jupyterlab-notebook-file-oneliner/index.html @@ -0,0 +1,72 @@ +<!DOCTYPE html> +<html><head> + <meta charset="utf-8"> + <title>Getting the .ipynb Notebook File Location From a Running Jupyter Lab Notebook | Home</title> + <meta name="description" content=""> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <meta name="mobile-web-app-capable" content="yes"> + <meta name="color-scheme" content="dark light"> + <link rel="stylesheet" href="/style.css"> +</head> +<body><nav> + <div class="internal"> + + <a href="/" title="Home">Home</a> + <a href="/blog/" title="Blog">Blog</a> + <a href="/projects/" title="Projects">Projects</a> + <a href="/about/" title="About">About</a> + </div> + <div class="search"> + <div id="search"></div> + </div> + <div class="external"> + <a href="https://git.jaseg.de/" title="cgit">cgit</a> + <a href="https://github.com/jaseg" title="Github">Github</a> + <a href="https://gitlab.com/neinseg" title="Gitlab">Gitlab</a> + <a href="https://chaos.social/@jaseg" title="Mastodon">Mastodon</a> + </span> +</nav> + + <header> + <h1>Getting the .ipynb Notebook File Location From a Running Jupyter Lab Notebook</h1> +<ul class="breadcrumbs"> + <li><a href="/">jaseg.de</a></li> + <li><a href="/blog/">Blog</a></li><li><a href="/blog/jupyterlab-notebook-file-oneliner/">Getting the .ipynb Notebook File Location From a Running Jupyter Lab Notebook</a></li> +</ul> + <strong>2025-06-29</strong> + </header> + <main data-pagefind-body> + <div class="document"> + + +<p>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.</p> +<pre class="code python literal-block"> +<span class="lineno"></span><span class="line"><span class="kn">import</span><span class="w"> </span><span class="nn">sys</span><span class="w"></span></span> +<span class="lineno"></span><span class="line"><span class="w"></span><span class="n">Path</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">Path</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">read_bytes</span><span class="p">())[</span><span class="s1">'jupyter_session'</span><span class="p">])</span> +</span></pre> +<p>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.</p> +</div> + </main><footer> + Copyright © 2025 Jan Sebastian Götte + / <a href="/about/">About</a> + / <a href="/imprint/">Imprint</a> +</footer> +<script src="/pagefind/pagefind-ui.js"></script> + <script> + if(navigator.getEnvironmentIntegrity!==undefined)document.querySelector('body').innerHTML=`<h1>Your browser + contains Google DRM</h1>"Web Environment Integrity" is a Google euphemism for a DRM that is designed to + prevent ad-blocking, and which Google has forced into their browsers against widespread public opposition. + In support of an open web, this website does not function with this DRM. Please install a browser such + as <a href="https://www.mozilla.org/en-US/firefox/new/">Firefox</a> that respects your freedom and supports + ad blockers.`; + + window.addEventListener('DOMContentLoaded', (event) => { + new PagefindUI({element: "#search", showSubResults: true}); + }); + </script> + </body> +</html> |