How to Get the Current Ipython/Jupyter Notebook Name

How do I get the current IPython / Jupyter Notebook name

As already mentioned you probably aren't really supposed to be able to do this, but I did find a way. It's a flaming hack though so don't rely on this at all:

import json
import os
import urllib2
import IPython
from IPython.lib import kernel
connection_file_path = kernel.get_connection_file()
connection_file = os.path.basename(connection_file_path)
kernel_id = connection_file.split('-', 1)[1].split('.')[0]

# Updated answer with semi-solutions for both IPython 2.x and IPython < 2.x
if IPython.version_info[0] < 2:
## Not sure if it's even possible to get the port for the
## notebook app; so just using the default...
notebooks = json.load(urllib2.urlopen('http://127.0.0.1:8888/notebooks'))
for nb in notebooks:
if nb['kernel_id'] == kernel_id:
print nb['name']
break
else:
sessions = json.load(urllib2.urlopen('http://127.0.0.1:8888/api/sessions'))
for sess in sessions:
if sess['kernel']['id'] == kernel_id:
print sess['notebook']['name']
break

I updated my answer to include a solution that "works" in IPython 2.0 at least with a simple test. It probably isn't guaranteed to give the correct answer if there are multiple notebooks connected to the same kernel, etc.

Get current jupyter-lab notebook name [for Jupyter-lab version 2.1 and 3.0.1 and notebook version >6.0.3)

As an alternative you can use the following library: ipynbname

#! pip install ipynbname

import ipynbname
nb_fname = ipynbname.name()
nb_path = ipynbname.path()

This worked for me and the solution is quite straightforward.

How do you get the currently active notebook name in JupyterLab?

There are two ways to fix it and one way to improve it. I recommend using (2) and (3).

  1. Your order of arguments in activate is wrong. There is no magic matching of argument types to signature function; instead arguments are passed in the order given in requires and then optional. This means that you will receive:

    ...[JupyterFrontEnd, ICommandPalette, ILabShell, ILauncher]

    but what you are expecting is:

    ...[JupyterFrontEnd, ICommandPalette, ILauncher, ILabShell]

    In other words, optionals are always at the end. There is no static type check so this is a common source of mistakes - just make sure you double check the order next time (or debug/console.log to see what you are getting).

  2. Actually, don't require ILabShell as a token. Use the ILabShell that comes in app.shell instead. This way your extension will be also compatible with other frontends built using JupyterLab components.

    shell = app.shell as ILabShell
  3. (optional improvement) install RetroLab as a development-only requirement and use import type (this way it is not a runtime requirement) to ensure compatibility with RetroLab:

    import type { IRetroShell } from '@retrolab/application';
    // ... and then in `activate()`:
    shell = app.shell as ILabShell | IRetroShell

    to be clear: not doing so would not make your extension incompatible; what it does is ensures you do not make it incompatible by depending on lab-specific behaviour of the ILabShell in the future.

So in total it would look like:

import {
ILabShell,
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';
import type { IRetroShell } from '@retrolab/application';

// ...

const extension: JupyterFrontEndPlugin<void> = {
id: 'server-extension-example',
autoStart: true,
optional: [ILauncher],
requires: [ICommandPalette],
activate: async (
app: JupyterFrontEnd,
palette: ICommandPalette,
launcher: ILauncher | null
) => {
let shell = app.shell as ILabShell | IRetroShell ;
shell.currentChanged.connect((_, change) => {
console.log(change);
// ...
});
}
};

export default extension;

Can a jupyter notebook find its own filename?

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

How to get the ipython notebook title associated with the currently running ipython kernel

Answer has not changed, by design the kernel does not know what is speaking to it. Like TV news presenter do not have a camera pointed on every person that listen to TV at night.

And as usual the question is what are you trying to accomplish, you might be trying to do something the hard way.

How to obtain Jupyter Notebook's path?

TLDR: You can't

It is not possible to consistently get the path of a Jupyter notebook. See ipython issue #10123 for more information. I'll quote Carreau:

Here are some reasons why the kernel (in this case IPython):

  • may not be running from single file
  • even if one file, the file may not be a notebook.
  • even if notebook, the notebook may not be on a filesystem.
  • even if on a file system, it may not be on the same machine.
  • even if on the same machine the path to the file may not make sens in the IPython context.
  • even if it make sens the Jupyter Protocol has not been designed to do so. And we have no plan to change this abstraction in short or long term.

Your hack works in most cases and is not too bad depending on the situation.



Related Topics



Leave a reply



Submit