Get Base URL of Current Jupyter Server IPython is Connected To
Asked Answered
V

3

1

I'd like to be able to know the base URL of the Jupyter Notebook server IPython is presently connected to. I'm aware of the notebook.notebookapp.list_running_servers() function which produces output like:

[
  {
    'base_url': '/',
    'hostname': 'localhost',
    'notebook_dir': '/home/username/dir-notebook-was-spawned-in',
    'password': False,
    'pid': 368094,
    'port': 8888,
    'secure': False,
    'sock': '',
    'token': '4e7e860527d5333306cb06c594aa2167a7d375294f96c2d9',
    'url': 'http://localhost:8888/',
  },
  ...
]

This feels tantalizingly close to what I want since there's a base_url key there, however I don't know how to determine which server in the list is where IPython is actually connected to. The closest approximation I've been able to come up with is to see which server's notebook_dir key most closely matches os.getcwd() but this is obviously imperfect.

Further Findings:

  • I've now realized that notebook.notebookapp.list_running_servers() is not the right way to go about this because the notebook server and the kernel are not guaranteed to be running in the same place and in that case the function would always return an empty list.
Vile answered 28/12, 2020 at 10:3 Comment(0)
A
3

There was some discussion about it in ipyleaflet. You cannot get the base URL only from the back-end, the way we do it in ipyleaflet is to get it in the front-end using window.location.href.

Alloy answered 28/12, 2020 at 14:12 Comment(8)
So I guess the follow up question then is how I can get the base URL on the Javascript side.Vile
window.location.href returns the URL.Alloy
That doesn't give you the base URL of the notebook server though. For example, in Binder (or potentially any other JupyterHub deployemt) the server is not exposed at /, but at some path below the root that may not be deterministic. Knowing the base URL is important when writing a Jupyter server extension so your client can access the extension's resources.Vile
You're right, for instance in xarray-leaflet we try to infer the base URL from the browser's URL, but it's not great. In the case of Binder, it adds a random path to the URL and redirects to the base URL of the server, so you need this random part. Maybe using the browser's URL in conjunction with notebook.notebookapp.list_running_servers()'s base URL would be the best?Alloy
There's got to be a way to do this client-side. Maybe Jason Grout would know?Vile
I'm also realizing list_running_servers is the wrong way to go about this because the kernel and server are not guaranteed to be running in the same place. It all has to be done client-side.Vile
I'll use the xarray-leaflet solution for now though.Vile
I published ipyurl which allows to get the Jupyter Server's base URL if the front-end is JupyterLab.Alloy
P
1

On the command line you can use the following:

jupyter notebook list --json | python3 -c 'import json; import sys; print(json.load(sys.stdin)["base_url"])'

(Remove the ["base_url"] part from that command to see the full dictionary).

In Python, there is base url listed among the output of:

import psutil
psutil.Process().parent().cmdline()

These are derived from discussions on the Jupyter Discourse Forum here and here.

For Binder sessions, it was pointed out here to use the following to get a good listing of details:

env | grep -i jupyter
Placia answered 29/12, 2020 at 20:20 Comment(3)
As noted in an edit to the original question, anything server-side isn't going to work because the kernel and the actual notebook server are not guaranteed to be running on the same instance. Thus checking something like jupyter notebook list (which uses list_running_servers() under the hood) won't work on all scenarios and could yield an empty list. Finding the server's base URL must be done client-side.Vile
Sorry, I thought maybe that gave the part you needed for your extension since window.location.href didn't seem to be what you needed. Given the key words in the title of your post, those two suggestions may help someone down the road.Placia
Yes. Definitely a possible solution in a different situation. The psutil usage is something I hadn't run a across yet, so that could be useful to someone else. In my particular case though, not quite general enough.Vile
R
0

I had a similar problem trying to get the server's base URL within a custom IPython magic command. Not sure if this exactly corresponds to your situation, but it should provide some insight.

Install ipyurl and jupyter_ui_poll:

pip install ipyurl
pip install jupyter_ui_poll

Then, in your cell magic body, you can retrieve the base URL synchronously like so:

from IPython.display import display
from ipyurl import Url
from jupyter_ui_poll import run_ui_poll_loop

...

def magic():
    w = Url()
    display(w)
    func = lambda: None if w.url == "" else w.url
    url = run_ui_poll_loop(func)
    return url
Royo answered 15/3, 2023 at 23:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.