summaryrefslogtreecommitdiff
path: root/blog/jupyterlab-notebook-file-oneliner/index.html
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2025-07-26 14:24:27 +0200
committerjaseg <git@jaseg.de>2025-07-26 14:24:27 +0200
commitd91500da884b5dd240de0d35517c4ef4e87b5c2c (patch)
tree6af998b2e1591c87087370e5c4ace701b2cf6160 /blog/jupyterlab-notebook-file-oneliner/index.html
parent338de75fb45b861510e99a8271724fd00f2b023e (diff)
parent3281a5044261cb2d8bcce263f959ab9b0fbc93fb (diff)
downloadblog-d91500da884b5dd240de0d35517c4ef4e87b5c2c.tar.gz
blog-d91500da884b5dd240de0d35517c4ef4e87b5c2c.tar.bz2
blog-d91500da884b5dd240de0d35517c4ef4e87b5c2c.zip
deploy.py auto-commit
Diffstat (limited to 'blog/jupyterlab-notebook-file-oneliner/index.html')
-rw-r--r--blog/jupyterlab-notebook-file-oneliner/index.html85
1 files changed, 85 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..9eb93a2
--- /dev/null
+++ b/blog/jupyterlab-notebook-file-oneliner/index.html
@@ -0,0 +1,85 @@
+<!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">
+
+ <link rel="preload" href="/fonts/roboto_slab/RobotoSlab-VariableFont_wght.ttf" as="font" type="font/ttf" crossorigin />
+ <link rel="preload" href="/fonts/nyght-serif-main/fonts/WEB/NyghtSerif-Regular.woff2" as="font" type="font/woff2" crossorigin />
+ <link rel="preload" href="/fonts/nyght-serif-main/fonts/WEB/NyghtSerif-Bold.woff2" as="font" type="font/woff2" crossorigin />
+ <link rel="preload" href="/fonts/nyght-serif-main/fonts/WEB/NyghtSerif-BoldItalic.woff2" as="font" type="font/woff2" crossorigin />
+</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 &quot;kernel&quot; 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 type="text/javascript" src="/pagefind/pagefind-ui.js" defer></script>
+ <script>
+ window.addEventListener('DOMContentLoaded', (event) => {
+ new PagefindUI({element: "#search", showSubResults: true});
+ });
+ </script>
+ <script type="speculationrules">
+ {
+ "prerender": [
+ {
+ "source": "document",
+ "where": {
+ "and": [
+ {"href_matches": "/*"}
+ ]
+ },
+ "eagerness": "moderate"
+ }
+ ]
+ }
+ </script>
+ </body>
+</html>