summaryrefslogtreecommitdiff
path: root/blog/jupyterlab-notebook-file-oneliner/index.html
blob: 3662214a02ff6f11b9aad598af1576ddd162fe44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 &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 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>